Mayfield Global Knowledge Center



How do I change the access permissions for a file or directory using chmod?

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)  |||   ||| -rwxr-xr-x |   ||| |   ||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 <file or directory name>

  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 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). Use ls -l sample.txt to see the changes.
  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.
     To be safe, a good idea would be to 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 work right after you run the wildcard change and you need to change something back.
  8. For additional information about chmod, type:man chmod at the termial then press Enter to view the manual page.

Would you like to...


del.icio.us

Print this page Print this page

Email this page Email this page

Post a comment Post a comment

Subscribe me

Add to favoritesAdd to favorites

User Opinions (136 votes)

97% thumbs up 2% thumbs down

How would you rate this answer?

Helpful
Not helpful
Thank you for rating this answer.