A Deep-dive into the Linux Filesystem | Part 2
The part one of this article series explained what a filesystem is and how it manages files or itself (in a preliminary level) in conjunction with the operating system. Since this is a Linux-themed article series, filesystems that are commonly associated with the Linux operating system were briefly discussed.
In this article, we take a deeper look into Linux’s filesystem hierarchy; file permissions, formatting and mounting of file systems will also be discussed.
Filesystem Hierarchy
NB: In Linux everything is a file! Block devices, processes, peripherals, kernel data structures, what else? Think of anything. It’s a file in Linux.
The filesystem hierarchy defines how files and directories are structured within the operating system.
Every Linux filesystem starts with a root directory. This is the entry point into the filesystem for all other files and directories. The root is represented with a ‘forward slash’ (/). i.e. it is a host for all other directories.
This root directory is different from the /root directory above. We would get to the difference later on.
/bin: This folder contains executable files of core operating system commands. Examples of such commands include: ls, rm, lsmod, lsblk, cat, kill etc
/home: Home to the users files such as pictures, personal documents, music, downloads etc. Linux is a multi-user OS so all other users of the Linux system would have their own home folder (with appropriate permissions) containing data peculiar to them.
/lost+found: Data recovered after a system crash are dumped here.
/proc: Hosts information about processes. You can ‘ls’ within this directory to get the PID’s of current running processes.
/snap: Contains files and folders pertaining to installed Snap applications.
/usr: Secondary hierarchy for read-only user data. Contains the binaries, executable files, libraries of secondary level applications
/usr/include: contains standard libraries and files. These standard libraries are usually C/C++ header files.
/usr/lib: hosts libraries to binary programs in the /bin and /sbin directories.
/usr/libexec: includes internal binaries that are not intended to be executed directly by users or shell scripts.
/usr/local: Contains data such as header files, binaries, man pages of applications installed locally. i.e compiled by a Sysadmin rather than the Linux system distribution.
/usr/sbin: Contains inessential binaries that are useful to Sysadmin’s. E.g for binaries for repairs, daemons for network-services etc
/usr/src: Contains source to local and non-local compiled software. E.g kernel source code
/boot: Houses boot-loader files (grub), initrd. These files are essential for starting up a Linux machine.
/media: Removable media such as USB, CD-ROM are mounted on this directory.
/srv: Contains data served by this system for specific services. They could be data and scripts for web servers or version-control related data.
/var: Contains configuration files, system specific data and data that are subject to change (can be overwritten). Examples include system and application logs.
/cdrom: CD-Roms can be mounted to this folder. But they are rarely mounted here as a result of the official Linux Filesystem Hierarchy specification stating that mounts should be made on the /media directory.
/mnt: Contains temporarily mounted filesystems.
/root: Linux systems have a root user account. This directory is the home directory of the superuser account. On Ubuntu, the superuser is unavailable by default. It would have to be created by the Sysadmin.
/dev: Physical devices are mounted on this directory. Hard drive partitions are listed as sda’s here.
/lib: Dynamic libraries, shared libraries to executable found in /bin and /sbin.
/run: This temporary directory that becomes available from boot time. It stores runtime information (PID files, socket endpoints etc) required by boot-time programs ( programs that start up very early) before the /var/run becomes available to the system.
/sys: Contains files that provides an interface certain kernel modules and subsystems, hardware devices to the user space.
/etc: Contains system-wide configuration files that controls critical parts of the system
/opt: for optional software packages
/sbin: Contains essential operating system commands.
/tmp: Contains temporary files generated by applications. The files are usually deleted after shutdown.
vmlinuz (virtual memory linux gzip): This is not a directory. It is the Linux kernel executable in compressed form. It loads (vmlinuz is bootable) the operating system into the RAM and also mounts with initrd during boot-time.
initrd (initial ramdisk):
The initial RAM disk (initrd) is an initial root file system that is mounted prior to when the real root file system is available. The initrd is bound to the kernel and loaded as part of the kernel boot procedure. The kernel then mounts this initrd as part of the two-stage boot process to load the modules to make the real file systems available and get at the real root file system.
Reference: https://developer.ibm.com/articles/l-initrd/
Linux Filesystem Permissions
Linux provides an intuitive and simple mechanism for Sysadmins and Linux users to impose restrictions on who-and-who can access files and directories.
Permissions to files and directories are represented as a collection of 9 bits which appears as -rwxrwxrwx.
The first three bits specifies the owner’s permission status, the second does for groups while the last specifies the permission for others.
- ‘r’ represents a ‘read permission’. Current user can read files and directories.
- ‘w’ character represents a ‘write’ permission. Current user can write, delete, rename files. Can also add files to directories and rename.
- ‘x’ represents an ‘executable’ permission. Current user can run a file as a program and also execute programs from a directory, search through a directory too.
It’s also worthy of note that the ‘-’ dash before the 9-bits indicates a file object. A ‘d’ represents a directory, ‘l’ stands for a symbolic link, ‘p’ stands for a named pipe, while ‘s’ stands for a socket.
File permissions can also be changed. There are a few commands used for setting permissions.
Chmod
Each permission (rwx) is assigned a number; r = 4, w = 2, x = 1. The sum total of permissions of each of the 3-bits is represented as a number.
Hence,
- -rwxrwxrwx has a 777 permission value — can read, write and execute for everbody
- -rwxr — r — has a 722 permission value — can read, write and execute for owners only. But can read only for groups and others.
I assigned full permissions to owner, groups and others by doing chmod 777 bfs.py
Chmod with Letters
We’ve been able to change permissions using numbers. Linux also gives us the ability to change permission using letters. Let us see.
You can also turn file permissions on and off using plus (+) and minus (–) signs, respectively, along with letters to indicate what changes and for whom.
Using letters, for each file you can change permission for the user (u), group (g), other (o), and all users (a). What you would change includes the read (r), write (w), and execute (x) bits.
For example, start with a file that has all permissions open (-rwxrwxrwx). Run the following chmod commands using minus sign options.
Reference: Negus, C. “Linux Bible”
So,
- bfs.py has -rwxrwxrwx permission status. chmod go-wx takes away (minus) write and execute permissions for groups and other users.
- chmod g+x bfs.py adds (plus) execute permission to groups.
Managing Filesystems and Partitions
Disk partitions could be viewed using the lsblk or fdisk command
I have an 8gb flash drive hooked up to my usb port for demonstrating filesystem and disk management concepts. The flash drive is labelled sdb1 within the /dev folder.
NB: Be sure to run sudo umount /dev/sdb to be sure that the flash drive is unmounted
Firstly, I’d be creating a new partition
- Partition your flash drive using fdisk -c -u /dev/sdb1 command
- Type p to show the partition table. Mine shows my flash drive is a Linux partition
- Type d to delete the existing partition then type ’n’ to create a new one.
- You’d be prompted to type ‘p’ for a primary partition or ‘e’ for an extended one. I chose ‘p’ and you should.
- Type ‘1’ so as to create 1 partition on the flash drive
- Type ‘w’ to write the partition to disk.
At this point, a partition has been created but can’t be used.
A confirmation can be made to ensure if the partition was created by typing ‘ls -la /dev/sdb*
We’d need to format this partition with a filesystem to let the operating system access it.
This can be done using the mkfs(8) command. Remember in the last article where we discussed about different types of filesystems? We’d be formatting with the ext4 filesystem member of the ext’s family.
We do this by,
The filesystem can now be mounted on a particular directory for access.
I created a folder called ‘mountp’ in my ‘/’ directory and mounted the filesystem there.
The /etc/fstab file should be edited this way so that the OS knows where the filesystem should be mounted whenever it is plugged to the system.
So far, we have read a lot concerning Linux filesystems. In the next article, we’d take a further look into more intricate things than we have done so far. Eg. VFS (Virtual File System).
Hope you enjoyed the article.
Please leave a comment below and a clap beside.
Thanks and bye!