The chmod Command
Let's look at a summary of chmod's options, and then cover each option in depth:
u user (owner)
o other (world)
a all (user, group, and other)
= set exactly
X conditionally set execute
s Set UID or set GID
t set “sticky” bit
$ chmod a+rwx test_file $ ls -l test_file -rwxrwxrwx 1 eric users
This demonstrates the fourth possible symbol for user when using symbolic mode. We used a to set full permissions for all user classes at once. Let's delete the file and start over in order to demonstrate the difference between the = operator and the + and - operators. (From here on, we'll assume that you know how to get the directory listing, and won't list the ls command.)
$ rm test_file $ touch test_file -rw-rw-r-- 1 eric users $ chmod g+x test_file -rw-rwxr-- 1 eric users
This added execute permission for group.
$ chmod g=x test_file -rw---xr-- 1 eric users
The = operators set group's permissions to execute, and in doing so removed read and write permission. While + and - set or unset the permissions specified, = will set exactly the mode specified and remove any others.
Read, write and execute modes are very straightforward when referring to files. Read and write allow a user to examine and modify/delete data from a file, respectively. Execute allows a user to execute a shell script or binary program. If you ftp a program from one host to another and then try to run it without setting execute permission, it will fail, since ftp does not set execute permission.
For directories, the rules can be a bit more complicated.
Read permission allows a user to examine the contents of a directory.
$ mkdir test_dir $ touch test_dir/foo $ ls test_dir foo $ chmod u-r test_dir $ ls test_dir ls: test_dir: Permission denied
Write permission allows a user to modify the contents of the directory. That means that lack of write permission on a directory does not prevent a user from modifying a file within the directory, if the file's permissions allow it. It does prevent the user from renaming, moving, deleting or creating any file in the directory. This is because a directory is a really a file that contains a list of filenames, and so read and write permission control access to that list.
$ chmod u=rx test_dir dr-xrwxr-x 2 eric users $ touch test_dir/bar touch: test_dir/bar: Permission denied $ mv test_dir/foo ./foo mv: cannot move `test_dir/foo' to `./foo': Permission denied
This property also works the other way. Since write permission allows the modification of directory entries, a user can move or rename a file without permission to examine the contents. This is a very good reason for paying attention to write access for important directories.
$ ls -l test_dir -rw-rw-r-- 2 eric users foo $ chmod u=rwx test_dir $ chmod u=rx test_dir/foo $ cat .bashrc > test_dir/foo bash: test_dir/foo: Permission denied $ mv test_dir/foo ./foo $ ls test_dir (It's empty) $ ls foo foo (It's in our present directory.)
Execute permission for directories (also referred to as search permission) is also very important. Execute permission is necessary for accessing a directory.
$ chmod u=rwx test_dir $ cp ~/.bashrc test_dir (any text file will do) $ chmod u=rw test_dir $ cd test_dir bash: test_dir: Permission denied $ cat test_dir/.bashrc cat: test_dir/.bashrc: Permission denied
This copy of .bashrc does not do us a lot of good. However, setting execute permission for directory and not setting read or write can come in handy.
$ chmod u=x test_dir $ cat test_dir/.bashrc (we see the contents of the file) $ ls test_dir ls: test_dir: Permission denied
A directory that has execute permission only can be used to “hide” files. Only users who know the exact file name and path can access them; this includes both data files and programs.
|Making Linux and Android Get Along (It's Not as Hard as It Sounds)||May 16, 2013|
|Drupal Is a Framework: Why Everyone Needs to Understand This||May 15, 2013|
|Home, My Backup Data Center||May 13, 2013|
|Non-Linux FOSS: Seashore||May 10, 2013|
|Trying to Tame the Tablet||May 08, 2013|
|Dart: a New Web Programming Experience||May 07, 2013|
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- New Products
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Validate an E-Mail Address with PHP, the Right Way
- Trying to Tame the Tablet
- New Products
- Tech Tip: Really Simple HTTP Server with Python
- git-annex assistant
1 hour 42 min ago
- direct cable connection
2 hours 4 min ago
- Agreed on AirDroid. With my
2 hours 14 min ago
- I just learned this
2 hours 18 min ago
2 hours 49 min ago
- not living upto the mobile revolution
5 hours 40 min ago
- Deceptive Advertising and
6 hours 15 min ago
- Let\'s declare that you have
6 hours 16 min ago
- Alterations in Contest Due
6 hours 17 min ago
- At a numbers mindset, your
6 hours 19 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi
It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.