chmod

What is chmod?

On unix-like systems, chmod is a system-level command that stands for "change-mode" and allows you to manually change the permission settings of a file/directory.

Not to be confused with chown, which is another system-level command on unix-like systems that stands for "change owner" and lets you assign ownership of a file to another user, or chgrp, which stands for "change group" and assigns a file to a different group. These are important to know, but not as commonly used.

How to use chmod

# syntax, options = optional
chmod <PERMISSIONS> <OPTIONS> <FILE>

##### examples
# r = readable
# w = writeable
# x = executeable
chmod 777 /etc/passwd # makes the file rwx for : owner, group, others
chmod 775 /etc/passwd # makes the file rwx for the owner + group, r-x for others
chmod 754 /etc/passwd # makes the file rwx for owner, r-x for group, r-- for others

##### Create dir & create file in directory, after that changing permssions. Explanation is given behind the commands
mkdir testDir
touch testDir/test.txt
mkdir testDir/testDir2
touch testDir/testDir2/test2.txt

chmod 777 testDir # only the directory has the permission : 777
chmod -R 777 testDir # all directories and files within the directory testDir have the permission : 777 (recursive)
chmod -f -R 777 testDir # supresses error messages, for example when you don't have the permission to edit permissions on a file/directory. Does this recursively (check explanation above)

The basics of Linux file permissions

Every file has an owner, which determines the file's "user class". Every file also has a group which determines the file's "group class". Any system user who isn't the owner and doesn't belong in the same group is determined to be the others.

All files on unix-like systems have permissions assigned to all 3 classes (user, group, others) and these determine which actions can be taken by said classes for the given file/directory.

The 3 actions available on unix-like system are : read, write, execute

read : Ability to open and view the contents of the file/directory

write : Ability to open and modify the contents of a file

execute : Ability to run the file as an executable program.

The permissions can be set for the 3 classes (user, group, others)

Basic example

Lets say the permission on a file is as follows:

-rwxr-xr-- 

Now we can split this up in the following 3 classes:

-|rwx|r-x|r--

# 1 - rwx (user class)  | The first group (rwx) is the user itself. The user/owner of this file/directory has the permission to read, write and execute the file/directory
# 2 - r-x (group class) | The second group (r-x) is the group attached to the file. All users that are in this group have the permssion to read and execute the file/directory
# 3 - r-- (other class) | All other people, that are not the user, or part of the group have only the permission "read" on the file/directory

This means it’s a regular file with read, write, and execute permissions for the owner; read and execute permissions for the group; and only read permissions for everyone else.

Tip: with the command ls -l, all permissions of files/directories become visible, also make sure to checkout ls page.

Numeric notation

The second format is called numeric notation, which is a string of three digits that each represent user, group, and other permissions, respectively. Each digit can range from 0 to 7, and each digit’s value is obtained by summing the class’s permissions:

  • 0 means no permissions allowed.

  • +1 if the class can execute the file.

  • +2 if the class can write to the file.

  • +4 if the class can read the file.

In other words, the meaning of each digit value ends up being:

  • 0: No permission

  • 1: Execute

  • 2: Write

  • 3: Write and execute

  • 4: Read

  • 5: Read and execute

  • 6: Read and write

  • 7: Read, write, and execute

So the above example (-rwxr-xr--) would be 754 in numeric notation.

Advanced linux file permissions : setuid, setgid, sticky bit

Introduction

Normally, on a unix-like operating system, the ownership of files and directories is based on the default uid (user-id) and gid (group-id) of the user who created them. The same thing happens when a process is launched: it runs with the effective user-id and group-id of the user who started it, and with the corresponding privileges. This behavior can be modified by using special permissions.

in short

  • if setuid is set --> execute a file as the owner of the file (4xxx OR u+s)

  • if setgid is set --> execute a file as the group owner of the file (2xxx OR g+s)

  • if sticky bit is set (can only be set on directory) --> only the owner of the directory can delete files within this directory (1xxx OR +t ), example of this is /tmp directory.

examples

##### examples
# setuid
touch file

chmod 4777 file # setUID bit is set
chmod u+s file # setUID bit is set

# setgid
chmod 2777 file # setGID bit is set
chmod g+s # setGID bit is set

# sticky bit
mkdir testDirectory

chmod +t testDirectory # sticky bit it set
chmod 1777 testDirectory # sticky bit is set.

# from this point, whenever a file is created within the directory "testDirectory", it cannot be deleted unless
# you are the OWNER of the directory "testDirectory".

##### setuid bit - example (same could be followed, with different commands for setgid)
root@Corrosie:~/test# cp /bin/bash ./
root@Corrosie:~/test# ls -l bash 
-rwxr-xr-x 1 root root 1310440 Aug 23 16:10 bash
root@Corrosie:~/test# ./bash 
root@Corrosie:~/test# exit
exit
root@Corrosie:~/test# su predator
predator@Corrosie:/root/test$ ./bash 
predator@Corrosie:/root/test$ exit
exit
predator@Corrosie:/root/test$ exit
exit
root@Corrosie:~/test# chmod u+s bash 
root@Corrosie:~/test# su predator
predator@Corrosie:/root/test$ ./bash -p
bash-5.0# id
uid=1000(predator) gid=1000(predator) euid=0(root) groups=1000(predator)
bash-5.0# whoami
root

##### sticky bit - example
root@Corrosie:~/test# ls -l
total 4
drwxr-xr-x 2 root root 4096 Aug 23 16:49 testDirectory
root@Corrosie:~/test# chmod +t testDirectory/
root@Corrosie:~/test# ls -l
total 4
drwxr-xr-t 2 root root 4096 Aug 23 16:49 testDirectory
root@Corrosie:~/test# cd testDirectory/
root@Corrosie:~/test/testDirectory# ls
root@Corrosie:~/test/testDirectory# touch test.txt
root@Corrosie:~/test/testDirectory# ls -l
total 0
-rw-r--r-- 1 root root 0 Aug 23 16:50 test.txt
root@Corrosie:~/test/testDirectory# chmod 777 test.txt 
root@Corrosie:~/test/testDirectory# ls -l
total 0
-rwxrwxrwx 1 root root 0 Aug 23 16:50 test.txt
root@Corrosie:~/test/testDirectory# su predator
predator@Corrosie:/root/test/testDirectory$ rm -rf test.txt 
rm: cannot remove 'test.txt': Permission denied
predator@Corrosie:/root/test/testDirectory$ ls -l
total 0
-rwxrwxrwx 1 root root 0 Aug 23 16:50 test.txt

What is Setuid?

Setuid is a Linux file permission setting that allows a user to execute that file or program with the permission of the owner of that file. This is primarily used to elevate the privileges of the current user. If a file is “setuid” and is owned by the user “root” then a user that has the ability to execute that program will do so as the user root instead of themselves. The most common example of this in Linux is ‘sudo’. In this example, the user ‘test’ located the executable ‘sudo’ and did a full listing of it with the ‘ls -l’ command.

root@host [~]# id
uid=1002(test) gid=1002(test) groups=1002(test)

root@host [~]# which sudo
/usr/bin/sudo

root@host [~]# ls -l /usr/bin/sudo
-rwsr-xr-x 1 root root 136808 Jan 31 13:37 /usr/bin/sudo

root@host [~]#  

If you look at the permissions level of the ‘sudo’ executable, you can see the ‘s’ in the permissions for the user where normally there would be an ‘x’. Also, notice that this file is owned by the user ‘root’ (the super-user) and that the file is executable by the world (the last ‘x’ in the permissions). This indicates that when a user executes this program, the operating system will execute that file not as the user ‘test’, but as the user ‘root‘. In the matter of using the ‘sudo’ command, this allows a normal user to perform elevated system functions without having to log in as the root user.

How Do I Set Up Setuid?

Setting the ‘setuid’ permission is as simple as setting any other permission in Linux. The file ownership is modified using the command. An example command to set this would be as follows.

root@host [~]# chmod u+s <filename> 

In this example, we will create a file called ‘myfile’ using the command ‘touch’ and then we will examine its permissions with the ‘ls -l’ command.

root@host [~]# touch myfile
root@host [~]# ls -l myfile
-rw-rw-r-- 1 test test 0 Mar 2 17:59 myfile
root@host [~]# 

Notice that the file does not have the execute permissions for user, group, or world. We will add the setuid bit as seen below.

root@host [~]# chmod u+s myfile 
root@host [~]# ls -l myfile
-rwSrw-r-- 1 test test 0 Mar 2 17:59 myfile
root@host [~]# 

This output looks a little different from what we were expecting. The lowercase ‘s’ we were looking for is the now a capital ‘S.’ This signifies that the setuid IS set, but the user that owns the file does not have execute permissions. We can add that permission using the ‘chmod u+x’ command.

root@host [~]#  chmod u+x myfile 
root@host [~]#  ls -l 
total 0
-rwsrw-r-- 1 test test 0 Mar 2 17:59 myfile
root@host [~]#  

What is Setgid?

Setgid, when used on files, is very similar to setuid. A process, when executed, will run as the group that owns the file. A typical example of a file that uses this is the ‘crontab’ command.

root@host [~]#  which crontab
/usr/bin/crontab
root@host [~]#  ls -l /usr/bin/crontab
-rwxr-sr-x 1 root crontab 36080 Apr 5 2016 /usr/bin/crontab
root@host [~]#  

How Do I Set Up Setgid?

Similar to ‘setuid,’ ‘setgid’ is inserted with the ‘chmod g+s’ command. Let’s create a new file called ‘myfile2’.

root@host [~]#  touch myfile2
root@host [~]#  ls -l myfile2
-rw-rw-r-- 1 test test 0 Mar 2 19:30 myfile2
root@host [~]# 

Now we will run the ‘chmod g+s‘ command and review the results.

root@host [~]#  chmod g+s myfile2
root@host [~]#  ls -l myfile2
-rw-rwSr-- 1 test test 0 Mar 2 19:30 myfile2
root@host [~]# 

Again we see the capital ‘S’ is set, but we can modify that.

root@host [~]#  chmod g+x myfile2
root@host [~]#  ls -l
total 0
-rwsrw-r-- 1 test test 0 Mar 2 17:59 myfile
-rw-rwsr-- 1 test test 0 Mar 2 19:30 myfile2
root@host [~]#  

Setgid on Directories

Applying the setgid permission on a directory has as different behavior. A directory that has ‘setgid’ on it will cause all files that are created in that directory to be owned by the group of the directory as opposed to the group of the owner. First, we create a directory.

root@host [~]#  mkdir mydir
root@host [~]#  ls -ld mydir
drwxrwxr-x 2 test test 4096 Mar 2 19:36 mydir
root@host [~]#  

Then we change the group ownership of the directory by using the ‘chgrp‘ command, and then we can add the ‘setgid’ permission like before.

root@host [~]#  chgrp test2 mydir/
root@host [~]#  chmod g+s mydir 
root@host [~]#  ls -ld mydir/
drwxrwsr-x 2 test test2 4096 Mar 2 19:36 mydir/
root@host [~]# 

Let’s test it out by creating a file in that directory. All other files in this tutorial were created this way and had ‘test’ as the group. Because ‘setgid’ is set on the directory and it is owned by group ‘test2’, this file will get ‘test2’ as its group.

root@host [~]#  touch mydir/myfile3
root@host [~]#  ls -l mydir/myfile3
-rw-rw-r-- 1 test test2 0 Mar 2 19:59 mydir/myfile3
root@host [~]#

What Is A Sticky Bit?

The final special permission is the ‘sticky bit.’ When this is set on a directory, the files in that directory can only be removed by the owner. A typical use of this is ‘/tmp/.’ The /tmp directory can be written to by any user, but other users cannot delete the files of others.

root@host [~]#  ls -ld /tmp
drwxrwxrwt 8 root root 4096 Mar 2 20:17 /tmp
root@host [~]#

Notice that /tmp can be written to by everyone but has the ‘t’ in place of the ‘x’ at the end of the permissions list. This means it has the sticky bit.

How Do I Set Up A Sticky Bit?

The sticky bit is set with ‘chmod +t’ command.

root@host [~]#  mkdir mydir2
root@host [~]#  ls -ld mydir2
drwxrwxr-x 2 test test 4096 Mar 2 20:17 mydir2
root@host [~]#  chmod +t mydir2 
root@host [~]#  ls -ld mydir2
drwxrwxr-t 2 test test 4096 Mar 2 20:17 mydir2
root@host [~]#

Setting Special Permissions With Number Notation

You may remember from the definitions above that permissions can be set with a series of three numbers. The numbers represent the permissions for owner, group, and world, respectively. To determine the number you want to set, you can use x=1, w=2, and r=4. You add the numbers together to get the permission number. If we wanted to have read, write, and execute permissions, we would use 7. Read and write would be 6. Just read is 4. An example to set the file to read, write, and execute for owner, read and execute for group and world would look like this:

root@host [~]#  chmod 755 myfile
root@host [~]#  ls -l myfile
-rwxr-xr-x 1 test test 0 Mar 2 17:59 myfile
root@host [~]#  

For the special permissions, you prepend these numbers with another number where 4 is setuid, 2 is setgid, and 1 is the sticky bit. The following commands are all the same (assuming the file has the permissions we set above).

root@host [~]#  chmod 4755 myfile
root@host [~]#  chmod u+s myfile
root@host [~]#  ls -l myfile 
-rwsr-xr-x 1 test test 0 Mar 2 17:59 myfile
root@host [~]#

root@host [~]#  chmod 2755 myfile
root@host [~]#  chmod g+s myfile
root@host [~]#  ls -l myfile
-rwxr-sr-x 1 test test 0 Mar 2 17:59 myfile
root@host [~]#  

root@host [~]#  chmod 1755 mydir
root@host [~]#  chmod +t mydir
root@host [~]#  ls -ld mydir
drwxr-sr-t 2 test test2 4096 Mar 2 19:59 mydir
root@host [~]#  

Removing Special Permissions

To remove special permissions, we can use the same chmod commands with a ‘’ instead of a ‘+.’

root@host [~]#  chmod u-s myfile
root@host [~]#  chmod g-s mydir
root@host [~]#  chmod -t mydir2
root@host [~]#  ls -l
total 8
drwxr-xr-x 2 test test2 4096 Mar 2 19:59 mydir
drwxrwxr-x 2 test test 4096 Mar 2 20:17 mydir2
-rwxr-xr-t 1 test test 0 Mar 2 17:59 myfile
root@host [~]#  

Conclusion

All in all, these special permissions are very useful for separating the ability of a user or group to read, write or execute a file or affect a change on a folder.

On what do setuid/setgid bits work?

##### works on (double verify this with 
.elf files 
.c files 

##### does not work on:
anything that starts with #! (shebang), for example #!/bin/bash does not work

Reference(s)

Last updated