Managing file and directory permissions is a critical aspect of using Linux. Permissions determine who can read, write, or execute a file or folder. It’s essential to understand and manage these permissions for both security and functionality purposes.
This guide provides an in-depth look at Linux folder permissions, including a comprehensive cheat sheet and FAQ section. It aims to clarify the intricacies of permissions and the commands needed to manage them.
Linux folder permissions cheat sheet
Below is a cheat sheet for Linux folder (directory) permissions commands in a table format:
Command | Description |
---|---|
ls -l |
List files and directories with their permissions. |
chmod 755 <directory_name> |
Set directory permissions to rwxr-xr-x . |
chmod u+rwx <directory_name> |
Give the owner rwx permissions. |
chmod g+rx <directory_name> |
Give the group rx permissions. |
chmod o-x <directory_name> |
Remove execute permission from others. |
chmod a+r <directory_name> |
Give read permissions to everyone (owner, group, others). |
chmod -R 755 <directory_name> |
Set rwxr-xr-x permissions recursively to all sub-directories and files. |
chown <username>:<groupname> <directory_name> |
Change the owner and group of a directory. |
chown -R <username>:<groupname> <directory_name> |
Change the owner and group of a directory recursively. |
chgrp <groupname> <directory_name> |
Change the group of a directory. |
chgrp -R <groupname> <directory_name> |
Change the group of a directory recursively. |
setfacl -m u:<username>:rwx <directory_name> |
Give a specific user rwx permissions using ACLs. |
setfacl -m g:<groupname>:rwx <directory_name> |
Give a specific group rwx permissions using ACLs. |
getfacl <directory_name> |
View Access Control List (ACL) permissions for a directory. |
setfacl -x u:<username> <directory_name> |
Remove specific user permissions using ACLs. |
setfacl -b <directory_name> |
Remove all ACLs from a directory. |
This table serves as a quick reference, but make sure to check the man pages (man chmod
, man chown
, etc.) for more detailed explanations and additional options.
Notes:
r
stands for read,w
for write, andx
for execute.- For
chmod
, the owner (u
), group (g
), and others (o
) can be adjusted. The permission modes are additive when using the+
sign and subtractive when using the-
sign. - The numeric method for
chmod
uses a 3-digit octal number: the first digit represents the owner’s permissions, the second represents the group’s permissions, and the third represents the permissions for others. - ACLs (Access Control Lists) provide more granular permissions compared to traditional Unix permissions. The
setfacl
andgetfacl
commands help manage these.
In Linux, permissions for files and directories are often represented using symbolic (e.g., rwx
) or numeric (often called octal) notation. The numeric representation is especially useful with the chmod
command.
Here’s a table that shows the numeric codes for different permissions:
Number | Permission Type | Code Meaning |
---|---|---|
4 | Read | r |
2 | Write | w |
1 | Execute | x |
0 | No Permission | - |
To determine the numeric value for a combination of permissions, you’d add the numbers together. Here are some examples:
Combination | Numeric Value | Symbolic Representation |
---|---|---|
Read only | 4 | r-- |
Write only | 2 | -w- |
Execute only | 1 | --x |
Read & Write | 6 | rw- |
Read & Execute | 5 | r-x |
Write & Execute | 3 | -wx |
All permissions | 7 | rwx |
None | 0 | --- |
For chmod
command usage with numeric values, permissions are usually specified with three numbers, representing Owner, Group, and Others respectively. For example:
chmod 755
would translate torwxr-xr-x
: Owner has full permissions, while group and others can read and execute but not write.chmod 640
would berw-r-----
: Owner can read and write, group can only read, and others have no permissions.
This table should help provide a clearer understanding of how numeric values correspond to permissions in Linux!
Linux folder permissions FAQ
Q1: What does rwx
stand for in permissions?
A: r
stands for “read”, w
for “write”, and x
for “execute”. For directories, x
means the ability to access the directory.
Q2: What’s the difference between a file’s permissions and a directory’s permissions?
A: For files, the permissions are straightforward: r
allows reading the file, w
allows modifying it, and x
allows executing it (if it’s a script or a program). For directories, r
lets you list its contents, w
lets you create or delete files and subdirectories inside it, and x
lets you enter the directory and access its contents.
Q3: What does the chmod 777
command do?
A: chmod 777
grants full read, write, and execute permissions to the owner, group, and everyone else for the specified file or directory. It’s usually not recommended due to security concerns.
Q4: How do I view current permissions for a file or directory?
A: Use the ls -l
command. The output will display permissions, the number of links, owner, group, size, and modification date.
Q5: What does the chown
command do?
A: The chown
command changes the owner (and optionally the group) of a file or directory.
Q6: Why can’t I enter a directory even if I have r
(read) permission?
A: To enter (or cd
into) a directory, you need x
(execute) permission on it.
Q7: What are sticky bits, and how are they useful?
A: A sticky bit on a directory ensures that only the file’s owner, the directory’s owner, or the root user can rename or delete the file. It’s often used on directories like /tmp
to prevent users from deleting or renaming others’ files. You can set it using chmod +t <directory_name>
.
Q8: How can I set default permissions for new files and directories?
A: Use the umask
command. The umask
defines the default permissions when creating new files and directories. For example, a common umask
value is 022
, which means new files get 644
(rw-r–r–) and new directories get 755
(rwxr-xr-x).
Q9: What’s the difference between hard and symbolic (or “soft”) links in terms of permissions?
A: Hard links share the same inode and data on disk, so they always have the same permissions. Changing the permissions on one changes the permissions on all hard links to that data. Symbolic links, on the other hand, have their own permissions, but these permissions don’t matter much: what counts are the permissions of the file or directory they point to.
Q10: What are Access Control Lists (ACLs)?
A: ACLs provide a way to set more granular permissions than the traditional Unix rwx system. They allow specifying permissions for individual users and groups beyond the owner/group/other system.