Advanced
Searching inSummaries
How to search
  • Words find lesson titles, text and code. Filter by type (guides, courses, programs, books) or stay in this book.
  • book:rhcsa and ch:8 narrow the search; #code finds a word only in commands of the lessons.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Chapter 9Name in the book: Permissions

Permissions and users

Linux is a multi-user system: many people (and services) use one computer at the same time. Permissions protect each user's files from the others. This chapter explains owners and groups, the read/write/execute rights, chmod, umask, special bits, and how to work as another user with su and sudo.

  • 11 Lessons
  • 18 Commands
  • 1 Labs

$ 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

9.1

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.

9.2

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.

-rwxr-x---
-   rwx   r-x   ---
|    |     |     +-- others: nothing
|    |     +-------- group:  read and execute
|    +-------------- owner:  read, write, execute
+------------------- file type
File typeMeaning
-Regular file
dDirectory
lSymbolic link (rights are always rwxrwxrwx; the target's rights count)
cCharacter device (terminal, modem)
bBlock device (disk)
RightOn a fileOn a directory
rRead the contentsList the names (needs x too)
wChange the contentsMake, delete and rename files in it (needs x too)
xRun it as a programEnter it with cd

Watch out

Deleting a file is controlled by the w right on the directory, not on the file.

Commands in this lesson

9.3

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.

OctalBinaryRights
0000---
1001--x
2010-w-
3011-wx
4100r--
5101r-x
6110rw-
7111rwx
> 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
ModeUse
755Programs and directories everyone may use
700Private programs and directories
644Normal files everyone may read
600Private files (keys, passwords)

9.4

Change rights with chmod (symbols)

The symbol form changes only what you name: who (u g o a), how (+ - =), what (r w x).

ExampleMeaning
u+xAdd execute for the owner
u-xRemove execute for the owner
+xAdd execute for all (same as a+x, but the umask is used)
o-rwRemove read and write for others
go=rwGroup and others get exactly read and write
u+x,go=rxTwo changes at once
chmod u+x myscript.sh
chmod go-rwx private/
chmod -R g+w shared/

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.

Commands in this lesson

9.5

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
umaskNew fileNew directory
0022644 rw-r--r--755
0002664 rw-rw-r--775
0077600 rw-------700

Note

A umask set on the command line is lost when the shell ends. Put it in ~/.bashrc to keep it.

9.6

Use the special bits

Three extra bits sit in front of the normal rights: setuid (4000), setgid (2000) and sticky (1000).

BitOn a fileOn a directoryShown as
setuidThe program runs as its owner (often root)—s in the owner x place
setgidRuns as the file's groupNew files get the directory's groups in the group x place
sticky—Only the owner may delete their filest 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_dir

At work

A shared team folder: group owner = the team, chmod 2775 (setgid). Every new file then belongs to the team group.

9.7

Become another user with su

su starts a shell as another user (root if you name no user). You need that user's password.

su -
Password:
exit
su -c 'ls -l /root/*'
  • su - (or su -l) starts a login shell: you get the user's environment and home directory.
  • su -c 'command' runs one command. Put it in quotes so it is expanded in the new shell.

9.8

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 -i
  • sudo -l shows 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 wheel may.
  • 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.

9.9

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.

FormResult
chown bob fileOwner becomes bob
chown bob:users fileOwner bob, group users
chown :admins fileOnly the group becomes admins
chown bob: fileOwner bob, group = bob's login group
sudo cp myfile.txt ~tony
sudo chown tony: ~tony/myfile.txt
chgrp wheel notes.txt
sudo chown -R www-data: /var/www/site

9.10

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 user adds a user to a group (the user must log in again).
  • setgid (2775) + umask 0002 = new files belong to music and the group can change them.
  • passwd changes your own password; root can change any user's password with passwd user.

9.11

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.

3What is the difference between su and sudo?

su needs the target user's password and opens a shell; sudo needs your own password, runs one command, and is controlled by /etc/sudoers.

Practice

All commands in this chapter