The chmod Command
Let's return to test_file to examine the X option.
$ chmod u=rw,g=r,o=r test_file -rw-r--r-- 1 eric users $ chmod o+X test_file -rw-r--r-- 1 eric users $ chmod u+x test_file -rwxr--r-- 1 eric users $ chmod o+X test_file -rwxr--r-x 1 eric users
In the first command, we see that we can set options for more than one class at a time by using a comma to separate the mode specifications. Here, we set the mode so that no user has execute permission. In the second command, we try to set execute permission for other with X. This fails, because X only works when one of the classes already has execute permissions. When we add execute permissions for owner, X sets executable permission for other.
The s option sets or removes set UID (SUID) and set GID (SGID) mode. These modes are very important in terms of UNIX/Linux security. When a file has SUID mode set, the process executing it has the effective rights of the file's owner for the duration of the program's execution.
For example, the program dip is used to create SLIP network connections. This requires root access, because creating a network interface device requires root access. Instead of forcing users to become root in order to use dip, which would require that the users know the root password, the dip program can belong to root and have the SUID mode set.
$ ls -l /usr/sbin/dip -r-s--x--- 1 root dip
The s in the spot for user's execute field indicates the SUID mode is set. Another example of a use for the SUID mode is the passwd program, which allows users to modify the passwd (or shadow) file.
For security reasons, the SUID bit can affect only binary programs; it has no effect on shell scripts in Linux.
The SGID mode sets the group instead of the owner, and is set with (for example) g+s. It also has another purpose.
When a user creates a new file the group ownership defaults to the user's default group, which is the one listed in the passwd file. Sometimes users belong to more than one group and want to share files. The SGID mode can provide a convenient method for this. If the SGID mode bit is set for a directory, new files created in that directory will belong to that group, regardless of the creator's default group. If you belong to more than one group, try this. (You can check what groups you belong to with the id command. The default group is listed first, and you can use the chgrp command to change the group ownership of a file to another group you are a member of.)
$ mkdir test_dir $ chgrp nondefault test_dir $ chmod g+s test_dir $ touch test_dir/foo $ ls -l test_dir/foo -rw-rw-r-- 1 eric nondefault
The SUID and SGID modes can be a security hole. However, when used carefully, they are very valuable tools and actually enhance system security by providing an alternative to distributing important passwords.
Specifying user classes can be used to simplify copying permissions.
$ chmod g=u test_file -rwxrwxr-x 1 eric users
This copied the permissions from user to group. All of the classes can be used on the right side of the +, - or = operators in this way.
$ chmod o-u test_file -rwxrwx--- 1 eric users
This cleared all of the permissions that user has from other.
The last mode listed above is the t option, known as the “sticky bit”. This mode is actually supported on the command line for compatibility purposes with shell scripts from older operating systems. It is not needed for Linux. If an installation guide instructs you to use it, it actually does nothing.
File access modes can also be set using octal notation. This syntax is built by adding the mode fields together. For each user class, the fields are calculated this way:
Full permissions for any class would be 7, no permissions would be 0.
$ chmod 754 test_file -rwxr-xr-x 1 eric users
The classes are passed to chmod in the same order ls displays them. The mode we set is broken down this way:
Owner = 4 + 2 + 1 = 7 Group = 4 + 1 = 5 World = 4 = 4
Octal mode is convenient because other utilities, such as find, expect modes to be expressed this way.
In octal mode, SUID and SGID are set by specifying them in another column before the user mode. For SUID use 4, for SGID use 2, and use 6 for both:
$ chmod 4755 test_file -rwsr-xr-x 1 eric users
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems
Join editor Bill Childers and Bit9's Paul Riegle on April 27 at 12pm Central to learn how to keep your Linux systems secure.
Free to Linux Journal readers.Register Now!
- Python Scripts as a Replacement for Bash Utility Scripts
- Considering Legacy UNIX/Linux Issues
- [<Megashare>] Watch Mrs Brown's Boys Movie Online Full Movie HD 2014
- Cluetrain at Fifteen
- Memory Ordering in Modern Microprocessors, Part I
- New Products
- Getting Good Vibrations with Linux
- Security Hardening with Ansible
- RSS Feeds
- diff -u: What's New in Kernel Development