Linux 101: Using chmod and chown
When you use Linux long enough you are going to use the command line. Although nearly every command line trick can be done from a GUI front end now, there are times when the command line is the only route (headless server for example). When you have to go that route, you will be glad to have the fundamentals of the more important Linux commands under your belt. Two very important commands, chmod and chown, deal with permissions and ownership (respectively). With these tools you control who owns and who reads, writes, and executes files and folders on your Linux system. Let's take a look at how to use these commands.
Not a bit. By default you will have both chmod and chown installed on your system.
The chmod command allows you to change permissions on a file. The basic usage is:
chmod PERMISSIONS FILE
Where PERMISSIONS is either the numeric or the alpha equivalent of the permissions you want to assign and FILE is the file (or folder) you want to effect.
Since the numeric permissions are the easiest to understand (and use) let's look at that method.
Remember, file permissions are in the form:
OWNER | GROUP | All Others
Each of those sections includes:
READ | WRITE | EXECUTE
Each permission (read, write, execute) is represented with the binary representation of the initial letter:
- r - 4
- w - 2
- x - 1
To get the numeric permission you add which permissions you want to use together. So if you wantÂ r+w you get 6.Â If you wantÂ r+w+x you get 7.Â If you want r+x you get 5. If you want only r you get 4. So now, remembering you have to cover permissions for three different users (Owner, Group, All Others), you will need to have a numeric number for each. So if you want Owner and Group to have rwx permission and All Others to only have r permissions, you would have:
Now, to change the permission of a particular file to 774 you would issue the command:
chmod 774 FILENAME
Where FILENAME is the name of the file.
Now let's examine the changing of ownership. This will bite you when you install an application in a directory as one user and need to use it as another. So let's say you have a folder in the /opt directory called APP that belongs to user bethany but user jacob needs to own it instead (of course if they both need access you could just change permissions or create a group for this). To change the ownership of this folder (you will need administrative rights to do this) you would issue a command like:
sudo chown jacob.jacob /opt/APP
The above command would change both ownership and group ownership of the folder (since jacob.jacob was used). If you wanted to leave that folder belonging to the original group issue the command like:
sudo chown jacob /opt/APP
and the original group ownership would remain intact.
Permissions and ownership on Linux isn't nearly as challenging as you though - even from the command line! Of course you can do these same tasks from within your file manage - if you have a file manager!Advertisement
Why even bother remembering the octals of the read write and execute bits?
Its pointless when the symbolic names are easy and mean you are less likely to make mistakes:
chmod 774 FILENAME
Much easier, and you dont have to do octal arithmetic.
Despite that it is math, you’ll probably use the same few modes over and over (i.e. 755 for executables and directories; 644 for files). Change the last number to zero to block out “others.” I don’t know why anyone would ever use 774 (if it where placed on a directory, others could read but not change into the directory; if it where placed on a file, others couldn’t execute it, but they could read it?).
I agree with the OP, you should have included a discussion of the symbols both absolute like OP mentioned and relative (i.e. chmod ug+x …). Also, you should have included a discussion of umask so that we can get all the octal math out and in the open.
I find it deliciously ironic that Troy’s comment saying symbolic names are easier and less error prone actually has an error.
You can convert octals to symbolic and vice versa with this site:
it’s let you modify permissions without to do any octal arithmetic at all…
It depends on the user, I myself love the octal way. i find it comfortable and easy to use.
thanks for the explanations. It’s good to have advise when quick needing the correct command. For those who just wanna use it without knowing or thinking a lot about it: I found a cool tool on this. You give it the rights table and it generates the correct chmod command – for both: absolute and relative settings.
have fun with this. I hopethis helps a lot when hurrying with chmod….
Thanks Jack!! very helpful