Seeing and setting file permissions in the Linux terminal
Let's create an example file first:
touch example.txt
To see the file's permissions, type:
ls -l example.txt
Once executed, you'll see something similar to this:
-rw-r--r-- 1 synthird synthird 0 Nov 21 23:35 example.txt
Let's focus on the rw-rw-r-- part since that will indicate the file permissions.
Permission characters
There are four characters used to indicate file permissions:
r= Allowed to read permission.w= Allowed to write permission.x= Allowed to execute as a program or script permission.-= No permission on the file allowed.
User permissions (rw-rw-r--)
The first three characters of the rw-rw-r-- part tells which permissions you're allowed to do on the file. In this case, you can only read and write to the file.
Group permissions (rw-rw-r--)
The middle three characters are the group permissions. In this case, the group who has permissions on the file can only read and write to the file.
Other users' permissions (rw-rw-r--)
The final three characters are permissions other users who created an account on your system can do on the file. In this case, they can only read the file.
Setting permissions
To set permissions, type:
chmod 000 example.txt
Let's look at the 000 part since changing it allows us to set permissions:
- 000 = Set your permissions.
- 000 = Set group permissions.
- 000 = Set other users' permissions.
To set specific permissions, replace the zeros with different numbers:
| Number | Permission |
|---|---|
| 0 | No permission |
| 1 | Execute permission |
| 2 | Write permission |
| 3 | Write + Execute permission |
| 4 | Read permission |
| 5 | Read + Execute permission |
| 6 | Read + Write permission |
| 7 | Read + Write + Execute permission |
If you want everyone on the system to read, write, and execute the file, type:
chmod 777 example.txt
Now if you type:
ls -l example.txt
You'll see that the rw-rw-r-- part is replaced by rwxrwxrwx to show that everyone can read, write, and execute the file.
If you want only yourself to read, write, and execute the file, type:
chmod 700 example.txt
Now typing:
ls -l example.txt
Will show rwx------ instead of rwxrwxrwx to show that you have read, write, and execute permissions, but everyone else doesn't.
If you want to read and write to a file, but make the file read-only to everyone else, type:
chmod 644 example.txt
Now typing:
ls -l example.txt
Will show rw-r--r-- instead of rwx------ to tell you that you have read and write permissions, but the file is read-only to everyone else.