You don't need to convert them to numbers. chmod
understands symbols just fine, if you split them into user, group and other fields. The following are equivalent:
chmod 755
chmod u=rwx,g=rx,o=rx
So given a set of permissions like, split them like so:
-rwxrwxrwx == - rwx rwx rwx
drwxr-xr-x == d rwx r-x r-x
-rw-r--r-- == - rw- r-- r--
And then assign the first triplet to u
, the second to g
and the third to o
, skipping the hyphens:
chmod u=rwx,g=rwx,o=rwx
chmod u=rwx,g=rx,o=rx
chmod u=rw,g=r,o=r
When two fields are the same, you can combine them. The last chmod
would be the same as:
chmod u=rw,go=r
And you can use a
(all) to assign to u
,g
and o
at once, so the first is equivalent to:
chmod a=rwx
Now, there are a few special permission bits: s
(setuid/setgid) and t
(sticky bit).
These are shown over the field where x
is normally seen, so if a directory has the sticky bit for others, you'd see a t
(if execute permissions are present) or a T
(if execute permissions are not present) . For example, the permissions of /tmp
:
drwxrwxrwt
In such cases, you need to write t
as xt
, and s
as rwxs
:
chmod u=rwx,g=rwx,o=rwxt
The setuid bit means that when this file is executed, it runs as the user who owns the file, not as the user executing it. Consider passwd
(used for changing the password):
# stat `which passwd`
Access: (4755/-rwsr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
If I (user muru
) run passwd
, the process that is started runs with root
's permissions, not with mine. It is usually seen on binaries which need to be root for performing some action (passwd
edits /etc/shadow
, for example).
The setgid bit on a directory means that any newly created files or directories in it inherit the group ownership. It is usually seen on directories used for web or FTP servers, etc.
The sticky bit means that even if an user has write permissions on the directory, they cannot move or rename another user's files. It is usually seen on shared directories, like /tmp
.