ユーザーと権限
Ubuntu はマルチユーザー オペレーティング システムであり、適切なユーザー管理と権限の構成はシステム セキュリティの基礎です。この記事では、ユーザーの作成と管理、グループの構成、および Ubuntu 26.04 のファイル権限システムを理解する方法について説明します。
ユーザー管理
ユーザーの作成
useradd を使用して新しいユーザーを作成します。
# Create a user with a home directory
sudo useradd -m -s /bin/bash username
# Set the password
sudo passwd usernameadduser コマンドは、セットアップをガイドする対話型のラッパー スクリプトであるため、推奨されます。
sudo adduser usernameユーザー属性の変更
usermod を使用して、既存のユーザーの属性を変更します。
# Change the default shell
sudo usermod -s /bin/zsh username
# Change home directory (and move files)
sudo usermod -d /home/newdir -m username
# Lock a user account
sudo usermod -L username
# Unlock a user account
sudo usermod -U username
# Rename a user
sudo usermod -l newname oldnameユーザーの削除
# Delete a user but keep the home directory
sudo userdel username
# Delete a user and their home directory
sudo userdel -r usernameユーザー情報の閲覧
# Check the current user
whoami
# View user ID and group information
id username
# List all users
cat /etc/passwd
# View currently logged-in users
who
wグループ経営
グループの作成と管理
# Create a new group
sudo groupadd developers
# Add a user to a group (preserving existing groups)
sudo usermod -aG developers username
# Add a user to multiple groups
sudo usermod -aG docker,sudo,developers username
# View the groups a user belongs to
groups username
# View all members of a group
getent group developers
# Delete a group
sudo groupdel developers主要グループと補助グループ
各ユーザーにはプライマリ グループと 0 個以上の補助グループがあります。
# Change a user's primary group
sudo usermod -g newgroup username
# Modify supplementary groups (-a means append; without -a, all supplementary groups are replaced)
sudo usermod -aG extragroup usernamesudoの設定
Ubuntu 26.04 は、従来の C sudo のデフォルトの代替品として、Rust の書き換えである sudo-rs を同梱しています。一般的な sudoers 構文 (ユーザー/グループ ルール、NOPASSWD、ALL、/etc/sudoers.d/ など) の大部分と互換性があるため、日常的な使用法は変わりません。ただし、いくつかのあいまいなディレクティブとサードパーティのプラグインはまだ実装されていません。 sudo に依存するスクリプトでエラーが発生したり予期しない動作が発生した場合は、まず sudo --version を実行して現在の実装を確認し、必要に応じて sudo apt install sudo を使用して従来のバージョンを一時的にインストールしてトラブルシューティングを行い、問題が特定されたら sudo-rs に戻します。
基本的な使い方
# Execute a command with root privileges
sudo apt update
# Switch to the root user
sudo -i
# Execute a command as another user
sudo -u www-data commandsudo グループへのユーザーの追加
# In Ubuntu, members of the sudo group have administrator privileges
sudo usermod -aG sudo usernamesudoers ファイルの編集
sudoers 構成を編集するには、保存する前に構文チェックが実行されるため、常に visudo を使用してください。
sudo visudo一般的な構成例:
# Allow a user to execute all commands without a password
username ALL=(ALL) NOPASSWD: ALL
# Allow a user to execute only specific commands
username ALL=(ALL) /usr/bin/systemctl restart nginx, /usr/bin/systemctl status nginx
# Allow group members to execute apt without a password
%developers ALL=(ALL) NOPASSWD: /usr/bin/apt/etc/sudoers.d/ にスタンドアロン構成ファイルを作成することもできます。
sudo visudo -f /etc/sudoers.d/developersファイル権限の基本
パーミッション表記を理解する
Linux のファイル権限は、所有者、グループ、その他の 3 つのセットで構成されます。各セットには、読み取り (r=4)、書き込み (w=2)、および実行 (x=1) のアクセス許可が含まれます。
# View file permissions
ls -la
# Example output:
# -rwxr-xr-- 1 user group 4096 Jan 1 12:00 script.sh
# │├─┤├─┤├─┤
# │ │ │ └── Others: r-- (read only)
# │ │ └── Group: r-x (read and execute)
# │ └── Owner: rwx (read, write, execute)
# └── File type: - regular file, d directory, l symlinkchmod - 権限の変更
# Numeric 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 permission for the owner
chmod g+w file.txt # Add write permission for the group
chmod o-r file.txt # Remove read permission for others
chmod a+r file.txt # Add read permission for everyone
# Recursively change directory permissions
chmod -R 755 /var/www/htmlchown - 所有権の変更
# Change the file owner
sudo chown newuser file.txt
# Change both owner and group
sudo chown newuser:newgroup file.txt
# Change only the group
sudo chgrp newgroup file.txt
# Recursively change directory ownership
sudo chown -R www-data:www-data /var/www特別な権限
# SUID - Execute as the file owner
chmod u+s executable
chmod 4755 executable
# SGID - Execute as the file group / new files in directory inherit the group
chmod g+s directory
chmod 2755 directory
# Sticky Bit - Only the file owner can delete files (commonly used on /tmp)
chmod +t directory
chmod 1777 directoryumask - デフォルトの許可マスク
# View current umask
umask
# Set umask (new file default = 0666 - umask, new directory = 0777 - umask)
umask 022 # New files 644, new directories 755
umask 077 # New files 600, new directories 700umask を永続的に変更するには、~/.bashrc または /etc/login.defs を編集します。
ACL 拡張アクセス許可
標準のアクセス許可では柔軟性が十分でない場合は、ACL (アクセス コントロール リスト) を使用できます。
# Install ACL tools
sudo apt install acl
# Grant permissions to a specific user
setfacl -m u:username:rwx /path/to/file
# Grant permissions to a specific group
setfacl -m g:groupname:rx /path/to/dir
# View ACL settings
getfacl /path/to/file
# Recursively set ACL
setfacl -R -m u:username:rwx /path/to/dir
# Set default ACL (newly created files inherit automatically)
setfacl -d -m u:username:rwx /path/to/dir
# Remove ACL
setfacl -x u:username /path/to/fileベストプラクティス
- 最小権限の原則: ユーザーには、作業を行うために必要な最小限の権限のみを付与します。
- root を直接使用しないでください: root としてログインしたままにするのではなく、
sudoを使用して管理コマンドを実行します。 - グループを賢く使用する: グループを使用して権限を管理する方が、各ユーザーを個別に構成するよりも効率的です。
- 定期的に監査する:
/etc/passwdと/etc/groupを定期的に確認して、不要になったユーザーとグループをクリーンアップします。 - 機密ファイルの保護:
/etc/shadowキーや SSH キーなどのファイルに正しいアクセス許可 (600 以上) が設定されていることを確認します。