Users, Groups, and Permissions
Overview
- What you’ll learn: How to manage user accounts, groups, and file permissions to secure a Linux system.
- Prerequisites: Lesson 2 – Shell Navigation and File System
- Estimated reading time: 18 minutes
Introduction
Linux is a multi-user operating system. Multiple users can log in simultaneously, each with their own files, processes, and privileges. The security model is built around three concepts: users, groups, and permissions.
Understanding how Linux controls access is critical for any system administrator. Misconfigured permissions are one of the most common causes of security vulnerabilities. In this lesson, you will learn how to create and manage user accounts, organize users into groups, and set file permissions to control who can read, write, and execute files.
We will also cover the sudo mechanism, which allows regular users to execute commands with elevated privileges in a controlled manner.
User Accounts
Every user on a Linux system has an entry in /etc/passwd. This file contains basic account information:
$ cat /etc/passwd | head -3
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
admin:x:1000:1000:Admin User:/home/admin:/bin/bash
Each line has seven fields separated by colons: username:password:UID:GID:comment:home:shell. The actual password hashes are stored in /etc/shadow, which is readable only by root:
$ sudo cat /etc/shadow | head -2
root:$6$abc123...:19400:0:99999:7:::
admin:$6$xyz789...:19400:0:99999:7:::
Managing User Accounts
$ sudo useradd -m -s /bin/bash -c "John Developer" john
$ sudo passwd john
New password:
Retype new password:
passwd: password updated successfully
$ sudo usermod -aG docker john # Add john to docker group
$ sudo usermod -l johnny john # Rename user from john to johnny
$ sudo userdel -r john # Delete user and home directory
useradd: Creates a new user.-mcreates the home directory,-ssets the login shell,-csets the comment (full name).passwd: Sets or changes a user’s password.usermod: Modifies an existing user account.userdel: Deletes a user account.-ralso removes the home directory.
Groups
Groups allow you to organize users and apply permissions collectively. Every user belongs to a primary group (set during account creation) and can belong to multiple supplementary groups.
$ cat /etc/group | grep admin
admin:x:1000:
sudo:x:27:admin
$ groups admin
admin : admin sudo
$ id admin
uid=1000(admin) gid=1000(admin) groups=1000(admin),27(sudo)
Managing Groups
$ sudo groupadd developers
$ sudo usermod -aG developers admin # Add admin to developers group
$ sudo gpasswd -d admin developers # Remove admin from developers
$ sudo groupdel developers # Delete the group
File Permissions
Every file and directory on Linux has three sets of permissions for three categories of users:
- Owner (u): The user who owns the file.
- Group (g): The group associated with the file.
- Others (o): Everyone else on the system.
Each category can have three types of permissions:
- Read (r = 4): View file contents or list directory contents.
- Write (w = 2): Modify a file or create/delete files in a directory.
- Execute (x = 1): Run a file as a program or enter a directory.
$ ls -l script.sh
-rwxr-xr-- 1 admin developers 1024 Jan 15 10:00 script.sh
│╰─┬──╯╰─┬──╯╰─┬──╯
│ │ │ └── Others: read only (r-- = 4)
│ │ └── Group: read + execute (r-x = 5)
│ └── Owner: read + write + execute (rwx = 7)
└── File type (- = regular file, d = directory)
Changing Permissions with chmod
# Numeric (octal) mode
$ chmod 755 script.sh # rwxr-xr-x
$ chmod 644 config.txt # rw-r--r--
$ chmod 600 secret.key # rw-------
# Symbolic mode
$ chmod u+x script.sh # Add execute for owner
$ chmod g-w file.txt # Remove write for group
$ chmod o=r file.txt # Set others to read only
$ chmod a+r file.txt # Add read for all (a = all)
Changing Ownership with chown
$ sudo chown john:developers file.txt # Change owner and group
$ sudo chown john file.txt # Change owner only
$ sudo chown :developers file.txt # Change group only
$ sudo chown -R john:developers project/ # Recursive
sudo and Elevated Privileges
The sudo command allows permitted users to execute commands as root (or another user). sudo access is configured in /etc/sudoers, which should only be edited using visudo:
$ sudo visudo
# /etc/sudoers entries:
root ALL=(ALL:ALL) ALL
%sudo ALL=(ALL:ALL) ALL # Members of sudo group
admin ALL=(ALL) NOPASSWD: /usr/bin/systemctl # No password for systemctl
$ sudo whoami
root
$ sudo -u postgres psql # Run command as postgres user
The umask
The umask sets the default permission mask for new files and directories:
$ umask
0022
$ umask 0077 # New files: 600, new dirs: 700
$ touch newfile.txt
$ ls -l newfile.txt
-rw------- 1 admin admin 0 Jan 15 10:00 newfile.txt
The umask value is subtracted from the maximum permissions (666 for files, 777 for directories). A umask of 0022 creates files with 644 and directories with 755.
Key Takeaways
- User accounts are stored in
/etc/passwd; password hashes in/etc/shadow. - Use
useradd,usermod, anduserdelto manage users;groupaddandgroupdelfor groups. - Permissions are split into owner, group, and others with read (4), write (2), and execute (1) bits.
chmodchanges permissions (numeric or symbolic);chownchanges ownership.sudoprovides controlled privilege escalation; configure viavisudo.- The
umaskdetermines default permissions for newly created files and directories.
What’s Next
In Lesson 4, you will learn about Process Management and Signals — how to view, control, and send signals to running processes on Linux.
繁體中文
概述
- 學習目標:學習如何管理使用者帳戶、群組和檔案權限以保護 Linux 系統。
- 先決條件:第 2 課 – Shell 導航與檔案系統
- 預計閱讀時間:18 分鐘
簡介
Linux 是一個多使用者作業系統。多個使用者可以同時登入,每個人都有自己的檔案、行程和權限。安全模型建立在三個概念上:使用者、群組和權限。
了解 Linux 如何控制存取對任何系統管理員都至關重要。設定錯誤的權限是安全漏洞最常見的原因之一。
使用者帳戶
Linux 系統上的每個使用者在 /etc/passwd 中都有一個條目。實際的密碼雜湊儲存在 /etc/shadow 中,只有 root 可以讀取。
$ cat /etc/passwd | head -3
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
admin:x:1000:1000:Admin User:/home/admin:/bin/bash
管理使用者帳戶
$ sudo useradd -m -s /bin/bash -c "John Developer" john
$ sudo passwd john
$ sudo usermod -aG docker john # 將 john 加入 docker 群組
$ sudo userdel -r john # 刪除使用者及主目錄
useradd:建立新使用者。-m建立主目錄,-s設定登入 shell。passwd:設定或更改使用者密碼。usermod:修改現有使用者帳戶。userdel:刪除使用者帳戶。-r同時刪除主目錄。
群組
群組允許您組織使用者並集體應用權限。每個使用者都屬於一個主要群組,並且可以屬於多個附加群組。
$ groups admin
admin : admin sudo
$ id admin
uid=1000(admin) gid=1000(admin) groups=1000(admin),27(sudo)
檔案權限
Linux 上的每個檔案和目錄都有三組權限,適用於三類使用者:
- 擁有者 (u):擁有檔案的使用者。
- 群組 (g):與檔案關聯的群組。
- 其他人 (o):系統上的其他所有人。
每類都可以有三種權限:讀取 (r=4)、寫入 (w=2)、執行 (x=1)。
使用 chmod 更改權限
# 數字(八進位)模式
$ chmod 755 script.sh # rwxr-xr-x
$ chmod 644 config.txt # rw-r--r--
# 符號模式
$ chmod u+x script.sh # 為擁有者增加執行權限
$ chmod g-w file.txt # 移除群組的寫入權限
使用 chown 更改擁有權
$ sudo chown john:developers file.txt # 更改擁有者和群組
$ sudo chown -R john:developers project/ # 遞迴更改
sudo 和提升權限
sudo 命令允許經授權的使用者以 root 身分執行命令。sudo 存取在 /etc/sudoers 中設定,應僅使用 visudo 編輯。
$ sudo whoami
root
umask
umask 設定新檔案和目錄的預設權限遮罩。umask 值從最大權限中減去(檔案為 666,目錄為 777)。umask 為 0022 時,建立的檔案權限為 644,目錄為 755。
重點摘要
- 使用者帳戶儲存在
/etc/passwd;密碼雜湊在/etc/shadow。 - 使用
useradd、usermod、userdel管理使用者;groupadd、groupdel管理群組。 - 權限分為擁有者、群組和其他人,各有讀取 (4)、寫入 (2)、執行 (1) 位元。
chmod更改權限;chown更改擁有權。sudo提供受控的權限提升;透過visudo設定。umask決定新建檔案和目錄的預設權限。
下一步
在第 4 課中,您將學習行程管理和信號——如何檢視、控制 Linux 上正在執行的行程並向其發送信號。
日本語
概要
- 学習内容:Linux システムを保護するためのユーザーアカウント、グループ、ファイルパーミッションの管理方法。
- 前提条件:レッスン2 – Shell ナビゲーションとファイルシステム
- 推定読了時間:18分
はじめに
Linux はマルチユーザーオペレーティングシステムです。複数のユーザーが同時にログインでき、各ユーザーは自分のファイル、プロセス、権限を持っています。セキュリティモデルは3つの概念に基づいています:ユーザー、グループ、パーミッション。
Linux がアクセスをどのように制御するかを理解することは、システム管理者にとって不可欠です。
ユーザーアカウント
Linux システム上のすべてのユーザーは /etc/passwd にエントリを持っています。実際のパスワードハッシュは /etc/shadow に保存されており、root のみが読み取り可能です。
$ cat /etc/passwd | head -3
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
admin:x:1000:1000:Admin User:/home/admin:/bin/bash
ユーザーアカウントの管理
$ sudo useradd -m -s /bin/bash -c "John Developer" john
$ sudo passwd john
$ sudo usermod -aG docker john # john を docker グループに追加
$ sudo userdel -r john # ユーザーとホームディレクトリを削除
useradd:新しいユーザーを作成。-mでホームディレクトリ作成、-sでログインシェル設定。passwd:ユーザーのパスワードを設定または変更。usermod:既存のユーザーアカウントを変更。userdel:ユーザーアカウントを削除。-rでホームディレクトリも削除。
グループ
グループを使用すると、ユーザーを整理し、パーミッションをまとめて適用できます。各ユーザーはプライマリグループに属し、複数の補助グループに所属できます。
$ groups admin
admin : admin sudo
$ id admin
uid=1000(admin) gid=1000(admin) groups=1000(admin),27(sudo)
ファイルパーミッション
Linux 上のすべてのファイルとディレクトリには、3つのカテゴリのユーザーに対する3セットのパーミッションがあります:
- 所有者 (u):ファイルを所有するユーザー。
- グループ (g):ファイルに関連付けられたグループ。
- その他 (o):システム上の他のすべてのユーザー。
各カテゴリは3種類のパーミッションを持てます:読み取り (r=4)、書き込み (w=2)、実行 (x=1)。
chmod でパーミッションを変更
# 数値(8進数)モード
$ chmod 755 script.sh # rwxr-xr-x
$ chmod 644 config.txt # rw-r--r--
# シンボリックモード
$ chmod u+x script.sh # 所有者に実行権限を追加
$ chmod g-w file.txt # グループの書き込み権限を削除
chown で所有権を変更
$ sudo chown john:developers file.txt # 所有者とグループを変更
$ sudo chown -R john:developers project/ # 再帰的に変更
sudo と権限昇格
sudo コマンドは、許可されたユーザーが root としてコマンドを実行できるようにします。sudo アクセスは /etc/sudoers で設定され、visudo のみで編集する必要があります。
umask
umask は新しいファイルとディレクトリのデフォルトパーミッションマスクを設定します。umask 値は最大パーミッション(ファイルは 666、ディレクトリは 777)から減算されます。
重要ポイント
- ユーザーアカウントは
/etc/passwdに保存され、パスワードハッシュは/etc/shadowにあります。 useradd、usermod、userdelでユーザーを管理。groupadd、groupdelでグループを管理。- パーミッションは所有者、グループ、その他に分かれ、読み取り (4)、書き込み (2)、実行 (1) ビットがあります。
chmodでパーミッションを変更、chownで所有権を変更。sudoは制御された権限昇格を提供。visudoで設定。umaskは新しく作成されるファイルとディレクトリのデフォルトパーミッションを決定します。
次のステップ
レッスン4では、プロセス管理とシグナルについて学びます。Linux 上で実行中のプロセスを表示、制御し、シグナルを送信する方法を学びます。