The Filesystem
Commands
- mkdir
- rmdir
- rm -r
- mknod
- grep
- mount
- umount
- fuser
- lsof
- ls -l
- ls -ld
- chmod
- chown
- chgrp
Config Files
What is a filesystem?
- In UNIX it is presented as a single hierarchy that
starts at the directory /
- / is called the root of the filesystem
- A filesytem can also be a small portion of that tree
such as a directory and the files and directories below it
- /homes in this example can be considered a filesystem
- It is mounted as part of the overall UNIX filesystem tree which begins at /
Pathnames
A sequence of file names, separated by slashes (/), that
describes the path, or route, the shell must follow to locate a file in the
file system.
Two Types of Pathnames:
- Absolute (Full) - starts from
the root directory (/)
- Relative - starts from the
current directory.
Fill in the type of path in this table:
| /var/spool |
Absolute |
| mail |
Relative |
| /var/spool/mail |
|
| ../lpd |
Relative |
| ../../log |
|
| /etc |
|
| sysconfig/network-scripts |
|
| ../network |
|
Terminology: file, filename, pathname, and
path
- pathname usually infers the full or absolute
path to a file.
- A pathname cannot be more than 255 character long:
/project/engineering/cad/antenna/sims/aug02/...
|______________________________________________|
255 characters total
Way around this limitation
- you can use the cd command to traverse
to an intermediate directory
- use the relative pathname to access the file with
the long path
Filename restrictions
- limited in length (255 characters)
- can't contain nulls (\0)
- spaces must be quoted
$ cat "my file.txt"
PATH variable
Find out what your current path is
- The path of a regular user is different than that of
the root user
- What are the differences?
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/pattyo/bin
# echo $PATH
/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin/X11:/usr/X11R6/bin:/root/bin
Mounting and Unmounting Filesystems
- A filesystem can be a disk partition
- or a cdrom or floppy device
- or a filesystem on a remote server
The mount command
- Filesystems are attached to the
file tree using the mount command
- mount maps a directory
within the existing file tree
Syntax
mount [options] device
directory
mount [options] server:directory
directory
Note: If there is an entry in the
/etc/fstab file for the filesystem to be mounted, it is not necessary
to provide both the device and mount point.
mount /mnt/cdrom
Mini Lab: Mount a filesystem from
the instructors machine
1. root on the instructor
machine must have /mnt/cdrom exported
2. client must have the directory
/mnt/pub created
# mkdir /mnt/pub
3. now mount the instructors cdrom
filesystem
# mount 10.2.128.30:/var/ftp/pub
/mnt/pub
# cd /mnt/pub
4. From another shell window try to umount /mnt/instructor
# umount
/mnt/pub
umount: /mnt/pub:
device is busy
5. Why are you seeing this error
message?
6. Use the fuser
or lsof command to see which processes are access the filesystem
you are trying to umount.
# fuser -mv
/mnt/pub
- Note the ACCESS column in the
fuser command.
- Your book has a table of activities
that the process may be doing to prevent you from umounting the filesystem.
(page 66)
7. Use the ps command to get information
about the offending processes.
# ps -fp "pid1
pid2 pid3"
We will spend an entire class period
on NFS coming soon...
Filesystem Organization
| /bin |
Commands needed for minimal system operability |
| /boot |
Kernel and files needed to load the kernel |
| /dev |
Device entries for terminals, disks, floppies, modems... |
| /etc |
Startup scripts and configuration files |
| /lib |
Libraries, kernel modules, parts of the the C compiler |
| /proc |
Images of all running processes |
| /root |
Home directory of the root user (superuser) |
| /sbin |
Commands for booting, repairing, or recovering the
system |
| /tmp |
Temporary files |
| /usr |
applications installed under
the hierachy of directories beneath |
| /usr/bin |
commands and executable files |
| /usr/include |
Header files for C programs |
| /usr/lib |
Libraries |
| /usr/local |
Local software that you have
installed yourself |
| /usr/sbin |
additinal system administration
files |
| /usr/share |
man pages, documentation |
| /usr/src |
Source code, kernel source, RedHat SRPM build directories |
| /var |
system data, log files, user mail, printer spool area |
Types of Files
- Regular files
- Directories
- Character device files
- Block device files
- Local domain sockets
- Named popes
- Symbolic links
Regular Files
- text file
- data file
- executable program
- shared libraries
Directories
- Contains named references to other files
- Creating directories
- Removing directories
- rmdir <directory>
- What is the main caveat associated with the rmdir
command?
- recursively remove the directory and all files
and directories beneath it
- Be Careful with this command!
Character and block device files
- Provides a way for an application to interface with
device drivers via the filesystem
- Character and block devices look like regular files
- Device files are not the same as device drivers!
Characteristics of device
nodes
- Block and Character Devices
- b - random access devices
- c - character or stream devices
- Major and minor numbers
- major: which driver, indexed into kernel
- minor: address of the device, the device driver parameter
In Unix everything is a file. Take a look inside the
/dev directory
$ ls -l /dev/hda?
brw-rw----
1 root disk 3, 1 Apr 11 07:25 /dev/hda1
brw-rw----
1 root disk 3, 2 Apr 11 07:25 /dev/hda2
brw-rw----
1 root disk 3, 3 Apr 11 07:25 /dev/hda3
...
$ ls -l /dev/tty[0-9]
crw--w----
1 pattyo sysadmin 4, 0 Aug 22 23:34 /dev/tty0
crw--w----
1 pattyo tty 4, 1 Sep 11 10:49 /dev/tty1
crw-------
1 root root 4, 2 Aug 22 23:35
/dev/tty2
...
- Device files are created with the mknod
command
- Notice that they can go anywhere, not just /dev
- What is the implication of having them in /tmp?
#
ls -l /dev/hda
brw-rw---- 1 root disk 3, 0 Aug 30 /dev/hda
# mknod /tmp/pattyo b 3 0
# fdisk -l /tmp/pattyo
# fdisk -l /dev/hda
# rm /tmp/pattyo
- Remove device files with rm
Much more coming on Character and Block device files soon...
Local domain sockets
- used by processes on local machine to communicate with
each other
- also known as "UNIX domain sockets"
- Used by printing system, X Window System, and syslog
$ ls -l /dev | grep '^s'
srwx------ 1 pattyo
root 0 Aug 22 23:35 gpmctl
srw-rw-rw- 1 root
root 0 Aug 22 23:34 log
Named pipes (FIFO)
Symbolic Links
- Allows a file to have more than one name.
Syntax:
$ ln source_file target_file
Hard Link
The ln comand, without any options is called a hard link.
- One inode referenced by multiple names.
- Share same data blocks!
- The owner of the file remains the same.
- The file permissions remain the same.
- Hard links may not span partitions and may not
refer to directories.
Soft Link
Another type of link is a symbolic link created using the -s option
(soft link).
- Indirect pointer to a file.
- Different inode.
- May span physical partitions since they point to a
pathname and not a disklocation.
Hard link Directory inode entry
| inode |
Name |
| 1505 |
myfile |
| 1505 |
myfile-lnk |
Soft link Directory inode entry
| inode |
Name |
| 1505 |
myfile |
| 1766 |
myfile-lnk |
Hard and symbolic links.
Link Figure from: O'Reilly, Essential System Administration,
Second Edition, page 41
First picture, top left
- index and hlink share the inode N1 and
its associated data blocks.
- The symbolic link slink has a different inode,
N2, and therefore different data blocks.
- The contents of inode N2's data blocks refer to the
pathname to index.
- Accessing slink thus eventually reaches the
data blocks for inode N1.
Second picture, on right
- Wben index is deleted, hlink is associated
with inode N1 by its own directory entry.
- Accessing slink will generate an error since
the pathnmae it references doesn't exist.
Last picture, on bottom
- index is created and gets a new inode, N3
- This new file has no relationship to hlink.
- The new file acts as the target for slink.
How symbolic links used on your system
Make a shortcut to /etc/rc.d/init.d by creating a symbolic
link:
# ln -s /etc/rc.d/init.d /etc/init.d
Take a look at /etc and find your link
# cd /etc
# ls -l | grep init
File Attributes
setuid and setgid bits
- Permission bits with octal values
4000 and 2000
setuid
- Allow programs to access files
and processes they would normally not have access to
# ls -l /usr/bin/sudo
---s--x--x 1 root root
80764 Jul 23 /usr/bin/sudo
- Use chmod to create permissions
like the one above for sudo
# touch /tmp/myprog
# ls -l /tmp/myprog
# chmod 4111 /tmp/myprog
# ls -l !$
---s--x--x 1 pattyo sysadmin 0 Mar
4 14:00 /tmp/myprog
^^^
setgid
- Used with directories
- makes newly created files in
the directory take the same group permissions as the parent directory
- easier to share files within
a group of users
$ mkdir /tmp/myproj
$ ls -ld /tmp/myproj
$ chmod g+s /tmp/myproj
OR
$ chmod 2755 /tmp/myproj
$ ls -ld /tmp/myproj
drwxr-sr-x
2 pattyo sysadmin 4096 Sep 25 13:13 test
^^^
The Infamous Sticky Bit
Files
- has an octal value of 1000
- a long time ago it was used
to keep programs resident in memory, back when memory was small and expensive
- today it is ignored by executables
Directories
- prevent users from deleting
or renaming files unless they are the owner
- having write permissions on
the directory is not enough, you must own the directory or file
- used on /tmp so users
can't remove each others files and directories
$ ls -ld /tmp
drwxrwxrwt
9 root root 4096 Apr 14 14:17 /tmp
^^^
$ mkdir /tmp/mytmp
$ chmod 1755 /tmp/mytmp
$ ls -ld /tmp/mytmp
drwxr-xr-t 2 pattyo sysadmin 1024 Mar 4 14:02 /tmp/mytmp
Changing Ownership and Group
chown
- changes a file or directories
user ownership
# chown someuser
/home/someuser
chgrp
- changes a file or directories
group ownership
# chgrp admin /prj/engineering
Change both at the same time
Recursive Changes
Additional Attributes
- Can be problematic so rarely used
- Can make the a file undeletable and only appendable
- can confuse software
- only provide protection against users who don't know how to change permissions back
$ cp /etc/passwd .
$ sudo chattr +ia passwd
$ ls -l passwd
-rw-r--r-- 1 pattyo
sysadmin 1947 Mar 4 14:15 passwd
$ lsattr passwd
---ia--------- passwd
$ rm passwd
rm: remove write-protected regular file `passwd'? y
rm: cannot remove `passwd': Operation not permitted
- modify permissions on file
$ sudo chattr -ia passwd
$ rm passwd
rm: remove regular file `passwd'? y