SYSTEM ADMINISTRATION MTAT.08.021 EXTRA MATERIAL 1: FILE SYSTEMS

PREPARED BY AMNIR HADACHI, ARTJOM LIND AND ANDRE TÄTTAR UNIVERSITY OF TARTU, INSTITUTE OF COMPUTER SCIENCE UNIX FILE SYSTEMS OUTLINE

• Definition of file systems

• Everything is a file

• File types

hierarchy tree

• Understanding file system

• Navigating file system

• Paths / Mount / Mount point / Permanent Mount

• Files in Unix

• File names in Unix

• Ownership and permissions

• Partitions UNIX FILE SYSTEMS DEFINITIONS OF FILE SYSTEMS

• First definition:

tree: is a hierarchy of directories that consists of a single directory.

• Second definition:

• File system: is the way in which files are named and organized in their appropriate and logical place for storage and retrieval.

EXAMPLE 3.1:

"Linux Filesystem is stored on multiple partitions." vs "Should I use ext4 or for my multimedia usb?" EVERYTHING IS A FILE

QUOTE 4.1: “Everything is a File”

EXAMPLE 4.1: “primary hard drive appears as a file / dev/sda and also all directories are files”

QUOTE 4.2: “If it is not a file then it is a process” UNIX FILE SYSTEM EVERYTHING IS A FILE

• File types:

• Regular files (-)

• Directories (d)

• Special files (c)

• Links (l)

• Domains / sockets (s) CAPTION: COMPARISON OF FILE TYPES IN ROOT(/) VS /DEV VS HOME(~) • Named pipes (p)

• Block device (b) UNIX FILE SYSTEM EVERYTHING IS A FILE

• Special file examples:

EXAMPLE 6.1: “/dev/null” discards all data written to it, like a black hole. It is implemented in software and does not map to a hard drive on your system.

QUOTE: "Please Send complaints to / dev/null"

MEANING: A NICE WAY TO SAY THROW IT IN THE GARBAGE

EXAMPLE 6.2: “/dev/zero” produces infinite stream of zeros, good for wiping hard drives.

EXAMPLE 6.3: “/dev/random” produces stream of random numbers generated from background noise. UNIX FILE SYSTEM FILE SYSTEM HIERARCHY TREE UNIX FILE SYSTEM UNDERSTANDING FILE SYSTEM

• From Humans prospective:

• file systems is a hierarchical tree

• From kernel prospective:

• file system is a flat tree.

REMARK 8.1: Kernel does not differentiate between directories, files and programs. It identify files not by names but by inodes.

DEFINITION 8.1: An inode is a data structure on a filesystem on Linux that stores all the information about a file except its name and its actual data.

EXAMPLE 8.1: “Inode" structure: 1) inode number (unique id) 2) file owner and group 3) file type 4) file permission list 5) File creation, access and modification times 6) Size of the file 7) disk address (identifier for where the file is physically stored) UNIX FILE SYSTEM NAVIGATING LINUX FILE SYSTEM

• Root directory “/”

• Home directory “~”

• Parent directory “..”

• Sub-directory “/home/user” is sub-dir of “/home”

• Current directory “.” UNIX FILE SYSTEM PATHS

• Absolute Path:

DEFINITION 10.1: The absolute path is a full path which contains the root directory and all other subdirectories in which a file or folder is contained. • Always start with “/”

EXAMPLE 10.1: “/home/user/Desktop/Files/index.html”

• Relative Path:

DEFINITION 10.2: A relative path (partial path or non absolute path) is only a portion of the full path.

EXAMPLE 10.2: - bin/gcc (Used when pwd = /usr) - ./bin/gcc (Used when pwd = /usr) - ../bin/gcc (Used when pwd = /usr/lib) UNIX FILE SYSTEM MOUNT / MOUNT POINT

• Mount Command

DEFINITION 11.1: Mount command makes block device file system a part of the root file system, so you can access the data.

EXAMPLE 11.1: Manually Mounting “$ mount -t ext4 /dev/sdb1 /mnt/my_usb” (some OS's do it automatically)

DEFINITION 11.2: A mount point is a directory in the currently accessible filesystem on which an additional filesystem is mounted

REMARK 11.1: Mount options are Block devices(HDD, SSD, SDcard, CD-ROM) and also network share filesystems like nfs

REMARK 11.2: “Will be Covered in bash extra material” Important commands for mounts: mount, df -h, lsblk, fdisk -l and many more UNIX FILE SYSTEM MOUNT POINTS UNIX FILE SYSTEM PERMANENT MOUNT

REMARK 13.1: Configured using the /etc/fstab file - To remount, after inserting lines, use command mount -a UNIX FILE SYSTEM FILES IN UNIX UNIX FILE SYSTEM FILE NAME IN UNIX

• Case sensitive

• Hidden file start with “.”

• File name max length = 255 bytes

• File path max length = None

• Reserved characters: & ; | * ? ` " [ ] ( ) $ < > { } % ! # @ \

• Escaping reserved chars With '\' \( \) \! \\ UNIX FILE SYSTEM OWNERSHIP AND PERMISSIONS

• Three Ownership types: (Owner, Group, Others) REMARK 16.1: Owner These permissions apply only to owner of the file or directory

REMARK 16.2: Group These permissions apply only to the group that has been assigned to the file or directory

REMARK 16.3: Others There permissions apply to all other users on the system. Important for security

• Three Permission types: (Read, Write, Execute) REMARK 16.4: Read User can read the contents of the file

REMARK 16.5: Write User can write or modify the contents of the file

REMARK 16.6: Execute User can execute file or view contents of a directory UNIX FILE SYSTEM OWNERSHIP AND PERMISSION UNIX FILE SYSTEM PERMISSIONS MANAGEMENT UNIX FILE SYSTEM PARTITIONS

• Data Partition

DEFINITION 19.1: Data partition is a logical division of hard disk, that include the OS system data, root partition and all the data to ensure the start up and run of the system.

• Swap Partition

DEFINITION 19.2: Swap partition is the expansion of the computer’s physical memory and it acts as a overflow for the memory. UNIX FILE SYSTEM PARTITIONS

• Partitioning is an essential task

• Partitioning configuration is stored in partition table.

EXAMPLE 20.1: Popular options for storing partitioning information

DOS partition table

GPT (GUID Partition Table) or (Globally Unique Identifiers Partition Table) UNIX FILE SYSTEM STICKY BITS

CASE DESCRIPTION 21.1: So far, it is clear that we can control the read/write/execute on file and read/write/list on directory. Now, if we allow to write to directory - this means that we also allow to remove from directory. Moreover, “o+rwx" on directory means anyone can remove anything. What if I want to allow anyone to write to directory; however, removing process can be only allowed to the files and directories created by the concerned users themselves and at the same time they cannot remove the existing random files. “Looks like impossible task considering classic UNIX permission schema ?” The solution to our case that we can suggest is called "sticky bit"

SOLUTION To add sticky bit: “ +t dir” To remove sticky bit: “chmod -t dir”

REMARK 21.1: Make sure that it has en effect if directory is allowed to write by many users, so either g+w or o +w or both were given on the directory. If the directory is only writable by owner user (u+w) - the sticky bit is useless. UNIX FILE SYSTEM FORCING GROUP ID CHANGE ON EXCISION

CASE DESCRIPTION 22.1: let’s suppose we have a directory called “dir” and it has the following ownership and permission: drwxrwxr-x userA.G1 dir we see that our directory is owned by userA and it is associated to group G1. Moreover, any user who belongs to group G1 can read, write and execute commands in the “dir”. In addition we suppose that the userA has G1 a his primary group.

Now if we have another userB who belongs to group G1 and group G2, plus having G2 as primary group. What happen if userA create a file and also userB did the same? What happened if the same case as previous question happened with a small change in the ownership and permission of the “dir” (drwxrwsr-x)?

REMARK 22.1: The “s” approving in the group persimmon is the result of command “chmod g+s” on directory “dir”. chmod g+s dir UNIX FILE SYSTEM FORCING GROUP ID CHANGE ON EXCISION

SOLUTION

First question: when the userA create the file1 in directory “dir” the results ownership and permission of the new file will be: -rw-rw-r— userA.G1 and for the file2 created by userB will be: -rw-rw-r— userB.G2

REMARK 23.1: As you can in this case the user ownership of the created files is assigned directly to the primary group of each user. UNIX FILE SYSTEM FORCING GROUP ID CHANGE ON EXCISION

SOLUTION

Second question: when the userA create the file1 in directory “dir” the results ownership and permission of the new file will be: -rw-rw-r— userA.G1 and for the file2 created by userB will be: -rw-rw-r— userB.G1

REMARK 24.1: As you can in this case the user ownership of the created files for both users is group G1, due to the fact that the directory “dir” has a group ownership G1 and “chmod g+s” was given. This latter, means that before executing any command within the directory “dir” the group ID of user is changed to the one of the directory “dir”. UNIX FILE SYSTEM ASSIGNING EXECUTABLE BIT RECURSIVELY BUT TO DIRECTORIES ONLY CASE DESCRIPTION 25.1: let’s suppose in this case that we executed: “chmod -R go+x dir” this command will go recursively through directory “dir” assigning “chmod go+x” to all content of “dir” including files and directories. But, what if we want to assign “go+x” to all the directories inside the “dir” and not to the files?

SOLUTION

First solution: we may issue find command to find all the directories and for each execute “chmod go+x”. find dir -type d -exec chmod go+x {} \;

Second solution: Or we just issue the following command: chmod go+X dir which will do the same.