Set File Permissions In Unix With The chmod Command
How Do Unix File Permissions Work?
File permissions in Unix are a bit different from Windows. When we set the permissions for a file, we will be granting access to three different entities: user, group, and other.
The user is whichever specific user owns the file.
The group is the user group that the file's owner is in.
And other refers to everyone else on the system; anyone that is not the owner of the file or in the same user group.
Using chmod To Set Permissions
Now we can move on to using chmod (which stands for change mode) to alter file permissions. The syntax is pretty simple:
chmod ### filename
chmod is simply telling the system that you will be changing permissions. Next comes a three digit number -- one digit for each of the entities we discussed above -- user, group, and other. And finally, we specify the file name or directory that we are setting permissions on.
To determine which number to use in the command, we'll need some quick math. Here are the possible permissions and their values: Read: 4, Write: 2, and Execute: 1. We add up the numbers for the permissions we want to grant to each entity. For example, 700 would grant full access (read + write + execute) to the file's owner, and deny all access to everyone else. 777 would grant full access to everyone on the system.
To keep the system secure, it is important to grant the minimum possible access to each entity. If the file's owner is the only one who needs access, 700 or 744 would be appropriate permission levels. If the owner and his group need access, then 770 or 774 would work. In both of these cases we are denying write and execute permissions to the "other" group, which will prevent most users on the system from altering the file.
Examples
Let's go through a couple of quick examples to make sure everything is clear.
chmod 700 MySecret
In this example, the owner of the MySecret file will be granted full access, and nobody else on the system can see or change it.
chmod 760 MyScript
In this example, the owner has full access again. The owner's group can read and write to the file, but not execute it. Because the owner's group lacks execute permissions, only the owner can run the script. The "other" group once again has no access.
chmod 777 PublicFile
In this example, everyone on the system has full access to PublicFile. This would be dangerous for any administrative files and scripts, so granting 777 access should be used sparingly.