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
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Chris Birchall's Re-Engineering Legacy Software (Manning Publications)
- Cipher Security: How to harden TLS and SSH
- Petros Koutoupis' RapidDisk
- ServersCheck's Thermal Imaging Camera Sensor
- Firefox 46.0 Released
- Varnish Software's Varnish Massive Storage Engine
- Linux Mint 18
- The Italian Army Switches to LibreOffice
Until recently, IBM’s Power Platform was looked upon as being the system that hosted IBM’s flavor of UNIX and proprietary operating system called IBM i. These servers often are found in medium-size businesses running ERP, CRM and financials for on-premise customers. By enabling the Power platform to run the Linux OS, IBM now has positioned Power to be the platform of choice for those already running Linux that are facing scalability issues, especially customers looking at analytics, big data or cloud computing.
￼Running Linux on IBM’s Power hardware offers some obvious benefits, including improved processing speed and memory bandwidth, inherent security, and simpler deployment and management. But if you look beyond the impressive architecture, you’ll also find an open ecosystem that has given rise to a strong, innovative community, as well as an inventory of system and network management applications that really help leverage the benefits offered by running Linux on Power.Get the Guide