In Linux you can set permissions on your files and folders. This is useful when you have a Linux computer with multiple users. This way you can specify which users can access what files and folders as well as what they can do with the files and folders. You can also setup groups and set group permissions as well.
Linux chmod Command Usage Syntax
You can change permissions of files and folders by using the Linux chmod
command. The usage of the Linux chmod
command is easy after you get the hang of it and know the chmod
command’s usage syntax. The usage syntax for the Linux chmod
command is chmod [OPTION] MODE[,MODE] FILE
and each MODE
is in the form [ugoa]*([-+=]([rwxXst]*|[ugo]))+
.
The mode form for chmod
is broke down by who you are setting the permissions for.
u – is for setting permissions on the user owning the file or folder.
g – is for setting permissions on the group owning the file or folder.
o – is for setting permissions for everyone else that does not own the file or folder.
a – is for setting all three of the above.
Then you can specify if you are taking away permissions or adding permissions by using – to remove permissions or + to add permissions.
Next in the Linux chmod
syntax is what permissions you want to add or remove.
r – add or remove the read permissions, this allows a user or group to view a file.
w – is to add or remove write permissions to a file or folder, this allows a user or group to make changes to a file or folder.
x – is to add or remove execution permissions to a file or folder.
Linux chmod Command Examples
Now for some chmod
command examples. If you have a folder called private-photos
that you don’t want any other user on your Linux system to access you can remove the permissions of all other users and groups and then add permissions just for the user owning the directory. You can do this using these commands:
Remove all permissions:
chmod -R a-rwx private-photos
In the above command we are using -R
for recursive since we are using the chmod
command on a directory. Then using a
to specify all users, groups, and others. We then use -
to specify we are removing permissions. Then we use rwx
to specify read, write, and execute permissions to be removed. Finally private-photos
which is the directory we are changing permissions for.
Now we need to give the user that owns the directory permissions again since we just removed all permissions. We can do this with the chmod
command by using the following syntax:
chmod -R u+rwx private-photos
Now the above command is giving the owner permissions again. -R
is telling the chmod
command we are going recursive since this is a directory. u
is the syntax for user. +
is the syntax for adding permissions. rwx
is the chmod
syntax for specifying read, write, and execute permissions. Finally private-photos
is the directory we are changing permissions of.
Now what if you have a directory called family-photos
and you wanted all the users that are part of the group family
to be able to have permissions to the directory you would first make sure that family-photos
is owned by the family
group, by using the chown
command. Then add permissions using the chmod
command.
chown -R Your-Username:family family-photos
Now that family
owns the family-photos
directory we need to change the permissions on family-photos
chmod -R a-rwx family-photos
That will remove all permissions. Now we need to add permissions for the user and group.
chmod -R ug+rwx family-photos
Now you’re done, but if you wanted your username to be the only one that can edit photos, add or remove photos, etc. but everyone else in the family
group can only view them, you could have done these commands instead:
chmod -R u+rwx family-photos
chmod -R g+rx family-photos
I hope this Linux tutorial on the Linux chmod
command has helped you understand how Linux permissions work and how to change Linux permissions.