2023-03-03

File Mode in Linux

What is file mode in Linux

File mode in Linux refers to the permissions set on a file or directory that determine who can access it and how they can access it. The file mode consists of three sets of permissions: read, write, and execute, and each set applies to a specific group of users: owner, group, and others.

The read permission allows users to view the contents of a file or directory, the write permission allows users to modify or delete a file or directory, and the execute permission allows users to run a file or access a directory.

File mode is represented by a string of 10 characters, including the file type, permissions for the owner, group, and others, and special permissions like setuid or setgid. Understanding file mode is crucial for managing file and directory access in a Linux system.

How to Check File Mode

To check the file mode in Linux, you can use the ls command along with the -l option to display the long format listing of the files. The file mode is displayed as a 10-character string that includes the file type, permissions, and special permissions.

Here's an example of how to check the file mode of a file named "example.txt":

bash
$ ls -l example.txt

-rw-r--r-- 1 user user 0 Mar 3 10:31 example.txt

Components of File Mode

File mode in Linux is composed of three main components: file types, permissions, and special permissions. Understanding these components is essential for managing files in Linux.

File Types

In Linux, there are several file types that can be identified by the first character of the file mode. These file types determine how the file can be accessed and used. Here are some examples of file types in Linux.

Regular files

Regular files are the most common type of file in Linux. They can contain any type of data, such as text, images, or code. Regular files are represented by a hyphen (-) in the file mode.

bash
-rw-r--r-- 1 user user 1024 Mar 1 10:30 myfile.txt

In this example, "myfile.txt" is a regular file with read and write permissions for the owner (user) and read-only permissions for the group and others.

Directories

Directories are used to organize files into a hierarchical structure. They can be thought of as folders that contain other files and directories. Directories are represented by a "d" in the file mode.

bash
drwxr-xr-x 2 user user 4096 Mar 1 11:00 mydir/

In this example, "mydir" is a directory with read, write, and execute permissions for the owner and read and execute permissions for the group and others.

Symbolic links are files that point to another file or directory. They are used to create shortcuts or aliases to files or directories in different locations. Symbolic links are represented by an "l" in the file mode.

bash
lrwxrwxrwx 1 user user 15 Mar 1 12:00 mylink -> /home/user/file

In this example, "mylink" is a symbolic link that points to "/home/user/file".

Block Devices

Block devices are files that allow the transfer of data in blocks or chunks. They are used to interact with hardware devices such as hard drives and USB drives. Block devices can be created using the mknod command.

bash
brw-rw---- 1 user disk 8, 1 Mar 3 12:15 sda1

Here, the letter "b" indicates a block device.

Character Devices

Character devices are similar to block devices, but they transfer data character-by-character. They are used to interact with hardware devices such as printers and serial ports. Character devices can also be created using the mknod command.

bash
crw-rw---- 1 user lp 6, 0 Mar 3 12:15 lp0

Here, the letter "c" indicates a character device.

Named Pipes

Named pipes, also known as FIFOs, are files that provide interprocess communication between two or more processes. They are used to transfer data between processes without the need for a temporary file. Named pipes can be created using the mkfifo command.

bash
prw-r--r-- 1 user user 0 Mar 3 12:15 mypipe

Sockets

Sockets are files that provide interprocess communication over a network. They are used to transfer data between processes running on different computers. Sockets can be created using the socket system call.
Example:

bash
srwxrwxrwx 1 user user 0 Mar 3 12:15 mysocket

Here, the letter "s" indicates a socket.

Permissions

Linux file permissions are represented by three sets of three characters, for a total of nine characters. The three sets represent permissions for the file owner, the file's group, and all other users.

Each set of three characters represents three permission bits:

  • r (read)
  • w (write)
  • x (execute)

The first set of permission bits represents the file owner's permissions, the second set represents the file group's permissions, and the third set represents all other users' permissions.

Here are some examples of file permissions:

  • rw-r--r--: This file can be read and written by the owner, and can only be read by the group and other users.
  • rwxr-xr-x: This file can be read, written, and executed by the owner, and can be read and executed by the group and other users.
  • drwxr-xr-x: This directory can be read, written, and accessed (i.e., entered) by the owner, and can be read and accessed by the group and other users.

To set or modify file permissions, you can use the chmod command followed by the desired permissions. For example, the command "chmod 755 myfile" sets the file permissions to rwxr-xr-x, which allows the owner to read, write, and execute the file, and allows the group and other users to read and execute the file, but not write to it.

It is important to use file permissions responsibly to protect sensitive files and prevent unauthorized access.

Special Permissions

In Linux file mode, special permissions refer to the three additional permissions that can be set on a file or directory.

  • setuid (SUID)
  • setgid (SGID)
  • sticky bit

These permissions modify the behavior of the file or directory in specific ways.

Setuid (SUID)

When set on an executable file, SUID allows the file to be executed with the permissions of the file's owner, rather than the permissions of the user executing the file. This is commonly used for programs that require elevated privileges to run, such as password-changing programs. An example of a SUID file is the "passwd"

bash
-rwsr-xr-x 1 root root 27832 Oct 17  2018 /usr/bin/passwd

Setgid (SGID)

When set on a directory, SGID allows new files created in that directory to inherit the group ownership of the directory, rather than the group ownership of the user creating the file. This is useful for directories that multiple users need to access and modify. An example of a SGID directory is the "/var/www" directory used for web server content:

bash
drwxrwsr-x 2 root www-data 4096 Oct 23 14:23 /var/www

Sticky bit

When set on a directory, the sticky bit restricts the deletion of files within that directory to only the owner of the file or the root user. This is commonly used for directories with shared write access, such as the "/tmp" directory:

bash
drwxrwxrwt 14 root root 4096 Mar  4 07:20 /tmp

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!