$ run as a normal user# run as root (or with sudo)
Skills in this chapter
- Read and set permissions, owners and special bits; use sudo
Know owners, groups and the world
Every file has an owner (a user) and a group. Everyone else is "the world" (others).
id
uid=1000(me) gid=1000(me) groups=1000(me),10(wheel)
ls -l /etc/shadow
----------. 1 root root 1240 Nov 2 /etc/shadow
less /etc/passwd
less /etc/group- A user has a number (UID) and a main group (GID), and can be in more groups.
- User accounts are in
/etc/passwd, groups in/etc/group, the password hashes in/etc/shadow. - Normal users have UIDs from 1000 on. Root has UID 0. Lower numbers are system users for services.
- Most distributions give every new user a private group with the same name.
Read the rights: r, w, x
The first 10 characters of ls -l are the file type and three groups of rights: owner, group, others.
- rwx r-x ---
| | | +-- others: nothing
| | +-------- group: read and execute
| +-------------- owner: read, write, execute
+------------------- file type| File type | Meaning |
|---|---|
- | Regular file |
d | Directory |
l | Symbolic link (rights are always rwxrwxrwx; the target's rights count) |
c | Character device (terminal, modem) |
b | Block device (disk) |
| Right | On a file | On a directory |
|---|---|---|
r | Read the contents | List the names (needs x too) |
w | Change the contents | Make, delete and rename files in it (needs x too) |
x | Run it as a program | Enter it with cd |
Watch out
Deleting a file is controlled by the w right on the directory, not on the file.
Change rights with chmod (octal)
Each right has a value: r = 4, w = 2, x = 1. Add them per group: rwx = 7, r-x = 5, r-- = 4.
| Octal | Binary | Rights |
|---|---|---|
| 0 | 000 | --- |
| 1 | 001 | --x |
| 2 | 010 | -w- |
| 3 | 011 | -wx |
| 4 | 100 | r-- |
| 5 | 101 | r-x |
| 6 | 110 | rw- |
| 7 | 111 | rwx |
> foo.txt
ls -l foo.txt
-rw-r--r--. 1 me me 0 Nov 8 foo.txt
chmod 600 foo.txt
ls -l foo.txt
-rw-------. 1 me me 0 Nov 8 foo.txt| Mode | Use |
|---|---|
755 | Programs and directories everyone may use |
700 | Private programs and directories |
644 | Normal files everyone may read |
600 | Private files (keys, passwords) |
Change rights with chmod (symbols)
The symbol form changes only what you name: who (u g o a), how (+ - =), what (r w x).
| Example | Meaning |
|---|---|
u+x | Add execute for the owner |
u-x | Remove execute for the owner |
+x | Add execute for all (same as a+x, but the umask is used) |
o-rw | Remove read and write for others |
go=rw | Group and others get exactly read and write |
u+x,go=rx | Two changes at once |
Watch out
chmod -R changes files and directories the same way. chmod -R a+X dir (capital X) adds execute only to directories and files that already had it.
Set default rights with umask
The umask removes rights from new files. Files start from 666, directories from 777; the umask bits are taken away.
umask
0022
rm -f foo.txt; > foo.txt
ls -l foo.txt
-rw-r--r--. 1 me me 0 Nov 8 foo.txt
umask 0000
rm foo.txt; > foo.txt
ls -l foo.txt
-rw-rw-rw-. 1 me me 0 Nov 8 foo.txt
umask 0022| umask | New file | New directory |
|---|---|---|
0022 | 644 rw-r--r-- | 755 |
0002 | 664 rw-rw-r-- | 775 |
0077 | 600 rw------- | 700 |
Note
A umask set on the command line is lost when the shell ends. Put it in ~/.bashrc to keep it.
Use the special bits
Three extra bits sit in front of the normal rights: setuid (4000), setgid (2000) and sticky (1000).
| Bit | On a file | On a directory | Shown as |
|---|---|---|---|
| setuid | The program runs as its owner (often root) | — | s in the owner x place |
| setgid | Runs as the file's group | New files get the directory's group | s in the group x place |
| sticky | — | Only the owner may delete their files | t in the others x place |
ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 32648 Jul 21 /usr/bin/passwd
ls -ld /tmp
drwxrwxrwt. 20 root root 4096 Nov 8 /tmp
chmod u+s program
chmod g+s shared_dir
chmod +t shared_dirAt work
A shared team folder: group owner = the team, chmod 2775 (setgid). Every new file then belongs to the team group.
Become another user with su
su starts a shell as another user (root if you name no user). You need that user's password.
Run a command as root with sudo
sudo runs one command as root. You type your own password. The admin decides in /etc/sudoers who may use it.
sudo backup_script
[sudo] password for me:
sudo -l
User me may run the following commands on this host:
(ALL) ALL
sudo -isudo -lshows what you are allowed to do.- On Ubuntu the root account is locked; the first user can use sudo. On Fedora and RHEL, users in the group
wheelmay. - Every sudo command is logged. That is better for security than many people knowing the root password.
Watch out
Do not work as root all day. One wrong rm -rf as root can destroy the whole system.
Change owner and group with chown and chgrp
chown changes the owner and/or the group of a file. Only root can give a file to another user.
| Form | Result |
|---|---|
chown bob file | Owner becomes bob |
chown bob:users file | Owner bob, group users |
chown :admins file | Only the group becomes admins |
chown bob: file | Owner bob, group = bob's login group |
Share a folder and change passwords
A real example: two users share music files in a folder that belongs to a group.
sudo groupadd music
sudo usermod -aG music bill
sudo usermod -aG music karen
sudo mkdir /usr/local/share/Music
sudo chown :music /usr/local/share/Music
sudo chmod 2775 /usr/local/share/Music
umask 0002
passwd
Changing password for user me.
Current password:
New password:usermod -aG group useradds a user to a group (the user must log in again).- setgid (
2775) + umask0002= new files belong tomusicand the group can change them. passwdchanges your own password; root can change any user's password withpasswd user.
Summary of the chapter
ls -l shows the rights; chmod, chown, umask change them; sudo gives root power for one command.
1Which octal mode gives the owner read and write, and nobody else anything?
600.
2Why can every user write in /tmp, but not delete the files of others?
/tmp has mode 1777: everyone may write, and the sticky bit allows only the owner to delete a file.