Introduction to Linux: Basic Permissions
Philipp Fruck & Pius Walter Users and Permissions ██ Goal of this lecture • Understand how to manage users/groups on Linux • Understand how Linux handles permissions • Understand potential security risks of wrong file permissions ██ Why? • In Linux "everything is a file", so we have to care about the files permission! Introduction to Linux: Basic Permissions 2 / 22
Switching users Command │ Description │ Useful Flags / Examples ────────┼──────────────────────────────┼──────────────────────────────────── su │ Switch shell to another user │
sudo │ Superuser do... │ sudo mycommand run command as root
│ Substitute user do... │ sudo -i open interactive root shell
│ │ sudo -u sshd id
In most cases you will want to become the root user. Many ways to elevate: sudo -i, sudo su, su root.
▍ Note: su root requires your root password while sudo -i requires your sudo password!
Introduction to Linux: Basic Permissions 3 / 22
Group Management Command │ Description │ Useful Flags / Examples ────────────────────┼───────────────────────────┼──────────────────────────── id │ Show user/group IDs │
groups │ Show groups you belong to │
groupadd / groupdel │ Add/delete a group │ groupadd --system groupname
groupmod │ Modify a group │
Introduction to Linux: Basic Permissions 4 / 22
Group Management Command │ Description │ Useful Flags / Examples ────────────────────┼───────────────────────────┼──────────────────────────── id │ Show user/group IDs │
groups │ Show groups you belong to │
groupadd / groupdel │ Add/delete a group │ groupadd --system groupname
groupmod │ Modify a group │
Each group is assigned a unique group ID (GID): • 0: The root group
• 1-999: System groups
• >=1000: User groups
For more restrictions, see grep GID /etc/login.defs and cat /proc/sys/kernel/ngroups_max
Introduction to Linux: Basic Permissions 5 / 22
User Management Command │ Description │ Useful Flags / Examples ──────────────────┼─────────────────────────────────┼────────────────────────────────── whoami │ Show current user │
who │ Show logged-in users │
useradd / userdel │ Add/delete a user │ useradd --no-create-home service
usermod │ Modify a user │ usermod -aG secondarygroup myuser
│ (Re-authenticate after changes) │ usermod -g primarygroup myuser
adduser / deluser │ Add/delete a user interactively │ Convenience wrappers
Introduction to Linux: Basic Permissions 6 / 22
User Management Command │ Description │ Useful Flags / Examples ──────────────────┼─────────────────────────────────┼────────────────────────────────── whoami │ Show current user │
who │ Show logged-in users │
useradd / userdel │ Add/delete a user │ useradd --no-create-home service
usermod │ Modify a user │ usermod -aG secondarygroup myuser
│ (Re-authenticate after changes) │ usermod -g primarygroup myuser
adduser / deluser │ Add/delete a user interactively │ Convenience wrappers
Similar to group management: user ID (UID) 0 is root, 1-999 are system users and >=1000 are regular users.
Further restrictions: grep UID /etc/login.defs
Introduction to Linux: Basic Permissions 7 / 22
Exercise A simple recap: 1. Create user foo
2. Create group bar
3. Add user foo to group bar
Introduction to Linux: Basic Permissions 8 / 22
Storing User Information There are multiple files storing information about users/groups. One example is the /etc/passwd:
Field │ Value
alice:x:1000:1000:Alice:/home/alice:/bin/bash ─────────┼────────────
Username │ alice
Password │ x Originally, the second field contained the hashed password, but for user ID lookups to UID │ 1000
work /etc/passwd must be readable by all users! GID │ 1000
GECOS │ Alice Solution: /etc/shadow, only readable by root. Home │ /home/alice
Shell │ /bin/bash
Introduction to Linux: Basic Permissions 9 / 22
Storing User Information There are multiple files storing information about users/groups. One example is the /etc/passwd:
Field │ Value
alice:x:1000:1000:Alice:/home/alice:/bin/bash ─────────┼────────────
Username │ alice
Password │ x Originally, the second field contained the hashed password, but for user ID lookups to UID │ 1000
work /etc/passwd must be readable by all users! GID │ 1000
GECOS │ Alice Solution: /etc/shadow, only readable by root. Home │ /home/alice
Shell │ /bin/bash
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— Group information is stored in /etc/group and /etc/gshadow. See also man 5 shadow / man 5 group / man 5 passwd.
Setting /sbin/nologin as the users default shell disabled login for a certain user (at least interactively).
Advanced: Try sudo su daemon vs sudo -u daemon bash.
Introduction to Linux: Basic Permissions 10 / 22
File Permissions & Ownership Permissions control who can read, write, or execute files
Each file/directory has permissions for: Three basic permission types:
• Owner (user) • r = read
• Group (collection of users) • w = write
• Other (everyone else) • x = execute (or enter directory)
Introduction to Linux: Basic Permissions 11 / 22
Permission Model We already know how to display permissions. Now let's learn how to ██ Understanding Permissions
interpret them. What does -rwxr-xr-- mean?
$ ls -l • First character:
-rwxr-xr-- 1 alice staff 1234 Oct 5 10:00 script.sh ◦ - = file,
# perms links owner group size date & time filename ◦ d = directory,
◦ l = symlink
• Next 9 characters (permissions): ◦ User (owner): rwx
◦ Group: r-x
◦ Other: r--
Q: Who has which permissions on the above script?
Introduction to Linux: Basic Permissions 12 / 22
Permission Model We already know how to display permissions. Now let's learn how to ██ Understanding Permissions
interpret them. What does -rwxr-xr-- mean?
$ ls -l • First character:
-rwxr-xr-- 1 alice staff 1234 Oct 5 10:00 script.sh ◦ - = file,
# perms links owner group size date & time filename ◦ d = directory,
◦ l = symlink
• Next 9 characters (permissions): ◦ User (owner): rwx
◦ Group: r-x
◦ Other: r--
Q: Who has which permissions on the above script?
• Owner (alice) can read, write, execute
• Group (staff) can read, execute
• Everyone else can only read Introduction to Linux: Basic Permissions 13 / 22
Changing Permissions ██ Symbolic Mode • + -> add
• - -> remove
• = -> set exactly (replacing existing permissions)
# add execute for user
chmod u+x file.sh
# remove write for group
chmod g-w file.txt
# set read only for others
chmod o=r file.log
Introduction to Linux: Basic Permissions 14 / 22
Changing Permissions ██ Symbolic Mode ██ Numeric (Octal) Mode
• + -> add Each permission is defined by a number:
• - -> remove
• = -> set exactly (replacing existing permissions) r=4, w=2, x=1
Add them up to combine permissions:
# add execute for user
chmod u+x file.sh 7=rwx, 6=rw-, 5=r-x, 4=r--, 3=-wx, 2=-w-, 1=--x, 0=---
# remove write for group
chmod g-w file.txt
# set read only for others # rwx for user, r-x for group, r-x for others
chmod o=r file.log chmod 755 script.sh
# rw- for user, r-- for group & others
chmod 644 file.txt
Introduction to Linux: Basic Permissions 15 / 22
Changing Permissions ██ Symbolic Mode ██ Numeric (Octal) Mode
• + -> add Each permission is defined by a number:
• - -> remove
• = -> set exactly (replacing existing permissions) r=4, w=2, x=1
Add them up to combine permissions:
# add execute for user
chmod u+x file.sh 7=rwx, 6=rw-, 5=r-x, 4=r--, 3=-wx, 2=-w-, 1=--x, 0=---
# remove write for group
chmod g-w file.txt
# set read only for others # rwx for user, r-x for group, r-x for others
chmod o=r file.log chmod 755 script.sh
# rw- for user, r-- for group & others
chmod 644 file.txt
Think before use! Do not blindly use chmod 777!
Introduction to Linux: Basic Permissions 16 / 22
Special Permissions • Setuid: run program as file owner instead of current user Special characters instead of x:
◦ Example: /usr/bin/passwd (runs as root)
◦ Check yourself: ls -l /usr/bin/passwd • Lowercase: Execute granted
◦ Risky -> use carefully! • Uppercase: Execute not granted
• Setgid: run program with effective GID of the file's group ◦ On directories: new files inherit group Permission │ Meaning
• Sticky Bit: users can only delete or rename files if they own ───────────┼────────────────────
the file or directory -rwsr-xr-x │ User s: Setuid
◦ Common on /tmp -rwxr-sr-x │ Group s: Setgid
drwxr-sr-x │ Setgid directory
drwxrwxrwt │ Other t: Sticky bit
chmod 4755 program # setuid
chmod 2775 shared/ # setgid on directory
chmod 1777 /tmp # sticky bit
Introduction to Linux: Basic Permissions 17 / 22
Managing File Permissions & Ownership Command │ Description │ Useful Flags / Examples ────────┼──────────────────────────────────┼───────────────────────────────────────────────────────── ls │ Show permissions and ownership │ ls -l, ls -ln
stat │ Show file/directory details │ stat -c '%a %n' file.txt
chmod │ Change file permissions │ chmod 644 file.txt, chmod +x script.sh, chmod -R 700 dir
chown │ Change owner/group │ chown user:group file.txt, chown user -R directory
umask │ Show/set default permission mask │ umask 0077 (default 0022)
Introduction to Linux: Basic Permissions 18 / 22
Managing File Permissions & Ownership Command │ Description │ Useful Flags / Examples ────────┼──────────────────────────────────┼───────────────────────────────────────────────────────── ls │ Show permissions and ownership │ ls -l, ls -ln
stat │ Show file/directory details │ stat -c '%a %n' file.txt
chmod │ Change file permissions │ chmod 644 file.txt, chmod +x script.sh, chmod -R 700 dir
chown │ Change owner/group │ chown user:group file.txt, chown user -R directory
umask │ Show/set default permission mask │ umask 0077 (default 0022)
umask explained:
Q: What does umask 0077 do?
New File New Directory
Default: 666 rw-rw-rw- 777 rwxrwxrwx
Umask: 022 ----w--w- 022 ----w--w-
-------------- -----------
Result: 644 rw-r--r-- 755 rwxr-xr-x
Introduction to Linux: Basic Permissions 19 / 22
Managing File Permissions & Ownership Command │ Description │ Useful Flags / Examples ────────┼──────────────────────────────────┼───────────────────────────────────────────────────────── ls │ Show permissions and ownership │ ls -l, ls -ln
stat │ Show file/directory details │ stat -c '%a %n' file.txt
chmod │ Change file permissions │ chmod 644 file.txt, chmod +x script.sh, chmod -R 700 dir
chown │ Change owner/group │ chown user:group file.txt, chown user -R directory
umask │ Show/set default permission mask │ umask 0077 (default 0022)
umask explained:
Q: What does umask 0077 do?
New File New Directory
Default: 666 rw-rw-rw- 777 rwxrwxrwx Newly created files can only be read and written by the
Umask: 022 ----w--w- 022 ----w--w- owner.
-------------- -----------
Result: 644 rw-r--r-- 755 rwxr-xr-x ▍ umask only affects the current session. If you want to
▍ keep a custom umask, add it to your ~/.bashrc
Introduction to Linux: Basic Permissions 20 / 22
Exercise ██ Create a file and experiment ██ Use sticky directory
• Show and change your umask • The /tmp folder is created as sticky directory
◦ Create new files (e.g. touch myfile) and compare ◦ sudo mkdir /tmp
permissions for different umasks ◦ chmod 1777 /tmp
• Change file ownership with chown foo:bar myfile • Try adding/removing files/directories as different
◦ Check which permissions are still available to users. Who can add/remove files?
your user Introduction to Linux: Basic Permissions 21 / 22
Thank you! Thank you for your attention! Don't forget the feedback in Moodle please! Introduction to Linux: Basic Permissions 22 / 22