File Permissions
File permissions are split into three types of access: read, write, and execute. Permissions can also be assigned across three different groups of access: user, group, and public.
The user is the owner of the file. The person who created the file is the default owner. User groups contain multiple users, which all have the same permissions for the file. Others includes everyone who is neither the owner nor in the user group.
The read permission provides the ability open a file to view its contents. The write permission gives you the ability to modify a file. When applied to a directory, this includes adding, removing, and renaming files in the directory. The execute permission allows you to run a file; without it, the program cannot be executed.
Viewing Permissions
You can view the file permissions in the current directory with ls -l
ls -l
will show you a list of information about all files in the current directory. You can also add a path to a file or folder at the end: ls -l /path/to/file
to view the permissions for a specific file or folder.
Example:
ls -l
total 2
drwxr-xr-x 1 justing justing 789 Jan 1 12:34 myfolder
-rw-r--r-- 1 justing justing 12345 Feb 2 23:45 myfile.txt
Use cut
to only show the permission fields: ls -l /path/to/file | cut -c1-10
Example:
ls -l
total 2
drwxr-xr-x
-rw-r--r--
Permission Field and Octal
The permission fields describe which users have which permissions.
Permission fields can be broken down into three parts: r
represents the read permission, w
represents the write permission, and x
represents the execute permission. The first set of rwx
is for the user, the second is for the group, and the third is for all others.
Example permission fields: drwxr-xr-x
and -rw-r--r--
The first character represents the type of file:
d (directory)
c (character device)
l (symlink)
p (named pipe)
s (socket)
b (block device)
D (door)
- (regular file)
The octal shorthand is computed by suming each permission, where r
is 4, w
is 2, and x
is 1, to get a number between 0 and 7. The first number is for user, then group, then others. For example, rw-r--r--
is 644
in octal (you can use the converter too).
Modifying Permissions
You can set file permissions with the chmod
command.
The syntax for chmod
is chmod [options] [mode] [filename]
. There are two ways to use chmod
: octal and symbolic.
In octal mode, you can use chmod [options] [octal] [filename]
to edit the permissions of a file. For example: chmod 774 myfile.txt
or chmod -R 644 myfolder
.
Some popular options include: -R
(recursive), -f
(force), -v
(verbose). You can find the full list at gnu.org.
In symbolic mode, you can choose from the following options:
References:
u: file owner
g: users in the file group
o: users neither in u nor g
a: all users (u, g, and o)
Operator:
+: add mode
-: remove mode
=: exact mode
Mode:
r: read
w: write
x: execute
For example chmod u+x myfile.txt
will grant the user execute permissions on myfile.txt
.
Examples from gnu.org:
# Change file permissions of FOO to be world readable
# and user writable, with no other permissions.
chmod 644 foo
chmod a=r,u+w foo
# Add user and group execute permissions to FOO.
chmod +110 file
chmod ug+x file
Links:
Ask Ubuntu
Linux Die
Guru99