Use the chmod
(change mode) command to change permissions on files and directories.
To make a file called prog.py
readable by anyone:
chmod 644 prog.py
To make a directory called public_html
accessible by anyone:
chmod 755 public_html
To see file permissions on files and directories, run ls -l
:
$ ls -l
-rw-r--r-- 1 csmajor1 users 685 Jan 10 14:09 prog.py
drwxr-xr-x 3 csmajor1 users 4096 Jan 12 09:30 public_html/
In the above output, the file and directory are owned by csmajor1
, and in the users
group.
The file permissions for prog.py
are rw-r--r--
. This means: read and write (rw) for the owner of the file (csmajor1), read (r) for everyone in the users
group, and read (r) for everyone else on the system.
The file permissions for the directory public_html
are rwxr-xr-x
. This means: read, write, and execute (rwx) for the owner of the directory, read and execute (r-x) for everyone in the users
group, and read and execute (r-x) for everyone else on the system. For directories, execute (x) just means you can get into the directory (using cd
).
To see what groups you are in, run the groups
command. If you need to change the group of a file, there’s a chgrp
command.
chmod
commandThe chmod
command can use numbers or letters to change the permissions of a file or directory. For numbers, read permission is 4 points, write permission is 2 points, and execute is 1 point. Adding the points together gives the full permissions. For example, 6 would be read and write (rw-), 7 would be read, write, and execute (rwx), and 5 would be just read and execute (r-x).
The chmod
command takes 3 numbers: the first for the owner’s permissions, the second for the group, and the third for everyone else on the system. So chmod 644 prog.py
means the owner gets read and write (6), the group gets read (4), and everyone else gets read permission (4).
Here are a few common examples:
# only owner can read/access
$ chmod 600 prog.py
$ chmod 700 public_html
$ ls -l
-rw------- 1 csmajor1 users 25 Jan 30 09:31 prog.py
drwx------ 2 csmajor1 users 4096 Jan 30 09:31 public_html/
# owner and group members can read/access
$ chmod 640 prog.py
$ chmod 750 public_html
$ ls -l
-rw-r----- 1 csmajor1 users 25 Jan 30 09:31 prog.py
drwxr-x--- 2 csmajor1 users 4096 Jan 30 09:31 public_html/
# anyone can read/access
$ chmod 644 prog.py
$ chmod 755 public_html
$ ll
-rw-r--r-- 1 csmajor1 users 25 Jan 30 09:31 prog.py
drwxr-xr-x 2 csmajor1 users 4096 Jan 30 09:31 public_html/
The chmod
command can also use letters: u
for user (owner), g
for group, o
for other, and a
for all (u, g, and o). So you could do chmod g+r prog.py
to add read access for the group.
There’s also a recursive flag, if you want to set the permissions for every file in a directory. For more information about chmod
, see the man page (man chmod
).