Wednesday, April 3, 2019

UNIX file system and its structure

The file system in UNIX can be thought of as a inverted tree structure. at its base of 'root' is the directory '/' from which all other directories and files branch out and reside. each of these directories have their special purpose, but we will only cover a few.

You may also note that your directory tree looks different than the one displayed. That is because the directories and files included on install vary from distribution to distribution, but the ones we cover should be included in most if not all distributions of UNIX.

So back to the file system and its structure. we said it represent sort of an inverted tree, so the root directory or ‘/’ is at the top of the tree and the branches which grow down are various system and user defined directories.

(root directory)
     "/"
      O
      O
      O
 /bin-|-/cdrom  
 /etc-|-/lib     
 /mnt-|-/proc  
 /run-|-/srv  
 /tmp-|-/var
 /opt-|-/root 
/boot-|-/dev    
/home-|-/media  
/sbin-|-/sys  
      |-/usr
      v
      v

Directories within directories are called ‘sub’ directories where each of these sub-directories can have their own sub-tree.

(root directory)
    "/"
     o
     o
     o
     ...
     |-/etc
     |-/tmp
     |-/usr
     v   |  (sub-directories)
     v   |-bin
         |-local
         |-etc
         v
         v

To be honest directories are nothing but files in UNIX. It views directories as a file which contains other files. In fact it is said that “Everything in UNIX is a file” which is true for the most part. If its not a file then its more than likely a running process

Files, directories and special files

Files in UNIX are everywhere. But there are six basic file types which UNIX uses and recognizes. The files types of Ordinary, Directories, Special, Pipes, Sockets and Symbolic links. We will only discuss three of the six of the file types available and they are:

Ordinary files
Directories
Special Files

Ordinary files are things like text and images files, programs used to store information. Directories like we discussed earlier are files which contain other files. They simply store the files on the system. Special Files are files which are used in conjunction with physical devices such as hard drives and CD-roms.

Ordinary Files

The ‘ls’ command stands for ‘list’. It is the way to view directories and files on a UNIX system.

sam@asus:~/unix% ls
file1.txt  file1.txt.tar  file2.txt  file2.txt.gz
sam@asus:~/unix%

The ‘ls’ command has options you can use with it such as ‘-l’ which lists files and directories.

sam@asus:~/unix% ls -l
total 16
-rw-rw-r-- 1 sam sam   40 Mar 28 18:28 file1.txt
-rw-rw-r-- 1 sam sam 2048 Mar 28 18:29 file1.txt.tar
-rw-rw-r-- 1 sam sam  164 Mar 28 18:30 file2.txt
-rw-rw-r-- 1 sam sam   30 Mar 28 18:29 file2.txt.gz
sam@asus:~/unix%

The command ‘ls -l’ display a lot of information to the user. Lets dive in and explore the output of the command we just ran.

-rw-rw-r-- 1 sam sam   40 Mar 28 18:28 file1.txt
(0)   (1) (2) (3)  (4)  (5)  (6)     (7)

0) these are the permission that the files has
1) number of links that point to this file
2) displays the user name
3) displays the group name
4) display the file size in bytes
5) displays the date the file was created
6) displays the time the file was created
7) displays the name of the file

You can also use ‘ls -a’ this list ALL files and directories.

sam@asus:~/unix% ls -a
.  ..  file1.txt  file1.txt.tar  file2.txt  file2.txt.gz
sam@asus:~/unix%

Each file has associated with it metadata contained in what is called an 'inode' and each file has a serial number so to speak called an inode number. These inode stores information about the files or directory in question. The information stored in an inode relates to certain attributes of a file such as permissions, type of file and access modes.

You can view the metadata contained in an inode for a file by using the ‘stat’ command.

sam@asus:~/unix% stat file1.txt
  File: 'file1.txt'
  Size: 0          Blocks: 0          IO Block: 4096   regular empty file
Device: b301h/45825d Inode: 784844      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/     sam)   Gid: ( 1000/     sam)
Access: 2019-04-02 11:47:22.053599898 -0600
Modify: 2019-04-02 11:47:22.053599898 -0600
Change: 2019-04-02 11:47:22.053599898 -0600
 Birth: -
sam@asus:~/unix% 

Directories and path names

In order to traverse directories on a UNIX system we use the command ‘cd’. The ‘cd’ command stands for change directory and is a very simple command to use.

One thing to realize is that there are to types of path names on a UNIX system, Absolute and Relative. Absolute path names tell you how far you are from the root directory ‘/’. Relative path names are with respect to your current working directory.

To change directory to an Absolute path with respect to the root ‘/’ directory, you would issue a command like so:

sam@asus:~/unix% cd /home/sam/unix/docs
sam@asus:~/unix/docs% pwd
/home/sam/unix/docs
sam@asus:~/unix/docs% 

To change directory to a Relative path with respect to your current working directory the ‘cd’ command can be used like this:

sam@asus:~/unix% cd docs/
sam@asus:~/unix/docs% pwd
/home/sam/unix/docs
sam@asus:~/unix/docs% 

The important thing to realize here when working with directories and paths is the ‘/’ character represents a directory within a path name. For instance, the path ‘/home/sam/unix’ is an absolute path name. If a path name starts with a leading ‘/’, then its an absolute path. If it is missing the leading ‘/’ then the path is relative to your current working directory.

Since directories are also considered files, they too have their own inode and number. Directories are nothing more than a collection of names used for inodes.

sam@asus:~/unix% stat docs/
  File: 'docs/'
  Size: 4096       Blocks: 8          IO Block: 4096   directory
Device: b301h/45825d Inode: 816253      Links: 2
Access: (0774/drwxrwxr--)  Uid: ( 1000/     sam)   Gid: ( 1000/     sam)
Access: 2019-04-01 19:24:01.874213664 -0600
Modify: 2019-04-01 19:20:33.096379662 -0600
Change: 2019-04-01 19:21:58.415268437 -0600
 Birth: -
sam@asus:~/unix% 

Special Files

Special files or device files are used for input/output in a UNIX system. They consist of two types: block and character devices. The block special files transfers data in large chunks of a fixed sized block. Character devices transfer data or characters one at a time.

Character devices are things like Serial Ports or Parallel ports like that used with printers. Block devices consist of things like hard drives and USB drives. An example of a block device would be your hard drive where your system resides.

You can list all block devices on your system with the ‘lsblk’ command.

sam@asus:~/unix% lsblk
NAME         MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
mmcblk0boot0 179:8    0    4M  1 disk 
zram0        252:0    0  5.7G  0 disk [SWAP]
mmcblk0boot1 179:16   0    4M  1 disk 
mmcblk0      179:0    0 14.7G  0 disk 
└─mmcblk0p1  179:1    0 14.7G  0 part /
sam@asus:~/unix%

No comments:

Post a Comment

Exploiting Weak WEBDAV Configurations

The server we are going to audit has the following fingerprint. 80/tcp open http Apache httpd 2.2.8 ((Ubuntu) DAV/2) Next we need t...