The chmod (change mode) command is used to set file and directory permissions in Linux operating systems. The most common use of chmod is to grant or revoke read (r), write (w) or execute (x) permissions for an owner / user (u), a user group (g) or other / anonymous (o) users. Below is a depiction of a typical permissions set for a file:

 read permissions for owner / user (u)
 |write permissions for owner / user (u)
 ||execution permissions for owner / user (u)
 |||read permissions for other / anonymous (o)
 ||||write permissions for other / anonymous (o)
 |||||execution permissions for other / anonymous (o)
 ||||||
-rwxrwxrwx
|      |||
|      ||execute permissions for user group (g)
|      |write permissions for user group (g)
|      read permissions for user group (g)
file/directory ("-" for file "d" for directory)

Current permissions information may be obtained by using the terminal command:

ls -l

  1. From the desktop, press: Alt+F2 to launch the Run Application window.
  2. Type: gnome-terminal then press Enter. This will launch a user terminal (similar to Windows command prompt).
  3. Navigate to the directory containing the target file by typing:cd <directory> Press: Enter to execute the command.
  4. At the terminal prompt, type:chmod <permissions> <filename> where: <permissions> is the access permission that you wish to set and <filename> is the target file that will receive the new permissions. Press: Enter to execute the command.
  5. As an example, suppose I wanted to change the mode (permissions) on: sample.txt by adding (+) the permission for everyone (the owner / user, anyone in the user group and all others) to read and execute the file. I could type:chmod ugo+rw <filename> at the terminal then press: Enter. The test file (sample.txt), can now be read (opened) and written to by everyone. A simpler way to change the permissions for everyone (ugo) is done by using a to represent “all”:chmod a+rw <filename>
  6. Using chmod, you can also revoke (-) permissions. For example, if I wanted to restrict editing of sample.txt to only the file’s owner (assuming that go had wx permissions to start with), I would type:chmod go-wx <filename> and press Enter. This command subtracts the wx (write and execute) permissions from go (the group and others). To see the changes, type: ls -l sample.txt
  7. Another powerful option for chmod is it’s ability to handle recursive (-R) operations. For example, let’s say I wanted to change the permissions for an entire directory that contained 4 sub-directories, each containing a number of files. Rather than going into each one, I could simply use:chmod -R <permissions> * (the “*” is a wildcard symbol used to replace or represent any number of characters). This command will change the permissions for every file and directory below the current one. Note: Before making changes with wildcards, it is a good idea to make a backup of whatever you intend to modify. Run: ls -lR > backup.txt then press: Enter. This will save the existing file and directory permissions to backup.txt …just in case things don’t go as planned.
  8. For additional information about chmod, type:man chmod at the terminal then press: Enter to view the manual page.

Leave a comment

You must be logged in to post a comment.