In Linux, every file and directory has a set of permissions that control who can read, write, or execute them. These permissions are critical for ensuring system security and proper file management in multi-user environments. Permissions are assigned to three categories: the file owner, the group, and others. Understanding how to check these permissions is essential for managing access to files and directories in Linux.
Each file or directory has permission bits that determine the level of access for each category. The ls command is commonly used to view these permissions, displayed in a specific format. In addition to ls, the stat command can provide more detailed information about permissions and other file attributes.
Being able to check and interpret file and folder permissions is fundamental for ensuring that files are properly secured and accessible to the right users. By following the steps below, you can easily check and understand the permission settings in Linux.
Steps to check file and folder permissions in Linux:
- Open the terminal in your Linux system.
- Use the ls command with the -l option to list files and display their permissions.
$ ls -l /path/to/file_or_directory -rwxr-xr-- 1 user group 4096 Sep 10 12:00 file.txt drwxr-xr-x 2 user group 4096 Sep 10 12:00 directory/
This displays a long listing of files, including their permissions, owners, and groups. The permissions are shown in the format rwxr-xr-x, where each character represents read ®, write (w), or execute (x) permissions for the owner, group, and others, respectively.
- To view permissions for a directory and its contents, use the recursive option with ls.
$ ls -lR /path/to/directory /path/to/directory: total 8 drwxr-xr-x 2 user group 4096 Sep 10 12:00 subdir/ -rw-r--r-- 1 user group 1024 Sep 10 12:00 file1.txt /path/to/directory/subdir: total 4 -rw-r--r-- 1 user group 2048 Sep 10 12:00 file2.txt
The -R option lists the permissions for all files and subdirectories inside the specified directory.
- Use the stat command to get detailed permission and ownership information for a file or folder.
$ stat /path/to/file_or_directory File: 'file.txt' Size: 1024 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 12345678 Links: 1 Access: (0755/-rwxr-xr-x) Uid: ( 1000/user) Gid: ( 1000/group) Access: 2024-09-11 12:00:00.000000000 +0000 Modify: 2024-09-11 12:00:00.000000000 +0000 Change: 2024-09-11 12:00:00.000000000 +0000
The stat command displays file permissions in both symbolic (rwx) and numeric (e.g., 755) formats, along with additional information like the last modification time and file size.
- To check only the permission bits in numeric form, use the stat command with a format specifier.
$ stat -c "%a" /path/to/file_or_directory 755
This outputs the permission bits as a three-digit number, where each digit represents the permissions for the owner, group, and others (e.g., 755 for rwxr-xr-x).
- Use the getfacl command to check file permissions when Access Control Lists (ACLs) are used.
$ getfacl /path/to/file_or_directory # file: file.txt # owner: user # group: group user::rwx group::r-x other::r--
The getfacl command provides information on any ACLs applied to the file or directory, which allow for more fine-grained permission control than the standard Unix permission model.
- To view the default permissions for newly created files and directories, check the current umask value.
$ umask 0022
The umask value determines the default permission bits that are masked (or turned off) when a new file or directory is created. For example, a umask value of 022 means new files will have 755 permissions by default.
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.