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:
Current permissions information may be obtained by using the terminal command:ls -l <file or directory name>
- From the desktop, press Alt+F2 to launch the Run Application window.
- Type: gnome-terminal then press Enter. This will launch a user terminal (similar to Windows command prompt).
- Navigate to the directory containing the target file by typing:cd <directory> Press Enter to execute the command.
- 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. - 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
ato represent "all":chmod a+rw <filename> - 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
gohadwxpermissions to start with), I would type:chmod go-wx <filename> and press Enter. This command subtracts thewx(write and execute) permissions fromgo(the group and others). Use ls -l sample.txt to see the changes. - Another powerful option for
chmodis 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. - For additional information about chmod, type:man chmod at the termial then press Enter to view the manual page.