How to Change File Permissions in Linux
The easiest way to change file permissions in Linux is with the chmod command which can take a bunch of parameters for either removing or adding permissions to an individual file or directory.
chmod +rwx filename
The above command adds permissions using +, the letters after this determine what can be done with the file by the owner or group users. The permission types are as follows.
read
(r
) - the user's ability to open and read the filewrite
(w
) - the users ability to write changes to the fileexecute
(x
) - the user's ability to execute and run the contents of the file.
Here are a couple more basic examples:
remove execute permissions
chmod -x filename
add read and write permissions
chmod +rw
Changing permissions for different users
On Linux there are four types of users in relation to the ownership of a file or directory
u
- Owner - whoever created the fileg
- Group - user with a group assignmento
- Othersa
- All users
To modify permissions for certain users and group owners you can add the flag into a chmod command like this:
chmod g+rw filename
chmod o-wx filename
The first one will grant read and write permissions for group owners while the second one will remove write and execute permissions for others. You can also make changes for all users in one command by specifying them.
chmod ugo+rwx filename
Changing ownership of the file
You can change the ownership of a file by using the chown command.
chown owner:group filename
Changing the group ownership of the file
sudo chgrp www-data filename
sudo chmod ug+rwx filename
Firstly we are changing the group owner to www-data and then setting the owner and the group to have read write and execute permissions.
To list the groups you have available type in "groups"
Recursively changing permissions in a directory
There are going to be times when you need permissions setting on all files and folders located within a directory without having to manually apply the changes to each one. In Linux, we can do this by adding the -R
flag to our command to recursively apply the changes. You can also apply the command to multiple folders by separating them with a space.
chmod -R ug+rwx folder1 folder2