Linuxでユーザーをグループに追加する方法【useradd・groupmemsコマンド実例付き】
このチュートリアルでは、Linuxでユーザーをグループに追加する方法を解説します。Bashシェルコマンドは、特定の目的を達成するための強力なツールです。グループは情報を共有・保護するための効果的な手段であり、Linuxのファイル権限では、ユーザー権限・グループ権限・全体(グローバル)権限によって所有権を細かく設定できます。また、root権限へのアクセス管理にも活用できる重要な仕組みです。
それでは、具体的な手順を見ていきましょう。
wheelグループを指定してユーザーを追加する
新しい管理者がチームに加わり、ファイルサーバーの管理者権限を付与する必要があるとします。ユーザーの追加には、シンプルかつ強力な「useradd」コマンドが便利です。このコマンドを使えば、ユーザーの作成と同時にグループへの割り当ても行えます。
NAME
useradd - create a new user or update default new user information
SYNOPSIS
useradd [options] LOGIN
これはuseraddのmanページにある基本情報です。それでは、新しいユーザー「Admin.Bob」と「Admin.Jane」を追加するところから始めましょう。
useraddコマンドの主なオプション
-c, --comment COMMENT -G, --groups GROUP1[,GROUP2,...[,GROUPN]]
主に使用するのは、コメント(-c)とグループ指定(-G)の2つのオプションです。まずはBobを追加してみます。
[admin@wsxdn.com ec2-user]# useradd -c "USA/CO/Denver Office" Admin.Bob
続いて、Janeをwheelグループ付きで追加します。
[admin@wsxdn.com ec2-user]# useradd -c "USA/CO/Denver Office" Admin.Jane -G wheel
Bobは管理者であり、管理者権限が必要なことは分かっています。しかし、場合によってはその情報が不明だったり、うっかり忘れていたりすることもあります。そのような場合は、後からグループを追加すれば対応できます。
一方、Janeはすでにwheelグループに所属しています。wheelグループは多くのLinuxシステムに標準で組み込まれているグループで、sudoなどによる権限昇格を許可するデフォルトのグループです。
[admin@wsxdn.com ec2-user]$ sudo su
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for Admin.Jane:
Sorry, try again.
手順の忘れといえば、パスワードの設定を忘れていたようです。useraddコマンドで直接パスワードを設定することは推奨しません。代わりに、続けてpasswdコマンドを実行してください。passwdは履歴に残らない安全なターミナル上でパスワード入力を求めるため安全です。あるいは、read -sp を使ってスクリプト内でパスワードを処理する方法もあります。
[admin@wsxdn.com ec2-user]# passwd Admin.Jane
Changing password for user Admin.Jane.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[admin@wsxdn.com ec2-user]# su Admin.Jane
[admin@wsxdn.com ec2-user]$ sudo su
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for Admin.Jane:
[admin@wsxdn.com ec2-user]#
これで完了です。Janeはwheelグループに属するユーザーとして追加され、特別なグループ操作コマンドは一切不要でした。もし最初から複数のグループに所属させたい場合は、次のようにカンマ区切りで指定できます。
[admin@wsxdn.com ec2-user]# useradd Admin.Jane -G wheel,ec2-users [admin@wsxdn.com ec2-user]# passwd Admin.Jane [admin@wsxdn.com ec2-user]# su Admin.Jane [admin@wsxdn.com ec2-user]$ groups Admin.Jane wheel ec2-users [admin@wsxdn.com ec2-user]$
groupsコマンドで確認すると、Admin.Janeは自分専用のユーザーグループ、sudoアクセス用のwheel、そしてec2-usersの3つのグループに所属していることが分かります。
では、Bobはどうでしょうか?Bobはwheelグループに属さない状態で作成されたため、別のコマンドを使って対応する必要があります。
既存のユーザーをグループに追加する
Bobは管理者ユーザーとして作成されましたが、管理者権限がありません。ここで登場するのが「groupmems」コマンドです。
groupmemsコマンドの主なオプション
SYNOPSIS
groupmems -a user_name | -d user_name | [-g group_name] | -l | -p
OPTIONS
The options which apply to the groupmems command are:
-a, --add user_name
Add an user to the group membership list.
If the /etc/gshadow file exist, and the group has no entry in the /etc/gshadow file, a new entry will be created.
-d, --delete user_name
Delete a user from the group membership list.
If the /etc/gshadow file exist, the user will be removed from the list of members and administrators of the group.
If the /etc/gshadow file exist, and the group has no entry in the /etc/gshadow file, a new entry will be created.
-g, --group group_name
The superuser can specify which group membership list to modify.
-h, --help
Display help message and exit.
-l, --list
List the group membership list.
まず、wheelグループの現在のメンバー一覧を取得してみましょう。
[admin@wsxdn.com ec2-user]# getent group wheel wheel:x:10:ec2-user,samberry,Admin.Steve,Admin.Jane
一覧にBobがいないことを確認できたので、Bobをwheelグループに追加します。
[admin@wsxdn.com ec2-user]# groupmems -a Admin.Bob -g wheel [admin@wsxdn.com ec2-user]# groupmems -g wheel -l ec2-user samberry Admin.Steve Admin.Jane Admin.Bob
これで完了です。Admin.Bobがwheelグループのメンバーとなり、「groupmems -l」(--list)オプションで正しく追加されたことを確認できました。
以上が、Linuxにおけるグループ管理の基本です。なお、家庭用や個人利用のLinux環境では、wheelではなく「adm」がデフォルトの管理グループとして使われていることが一般的です。
admin@wsxdn.com:/home/samuelberry# groupmems -g adm -l syslog samuelberry
自分の環境でどちらが使われているか気になる場合は、/etc/sudoers設定ファイルを確認してみてください。
admin@wsxdn.com:/home/samuelberry# cat /etc/sudoers # Members of the admin group may gain root privileges %admin ALL=(ALL) ALL # Allow members of group sudo to execute any command %sudo ALL=(ALL:ALL) ALL [admin@wsxdn.com ec2-user]$ cat /etc/sudoers ## Allows people in group wheel to run all commands %wheel ALL=(ALL) ALL ## Same thing without a password # %wheel ALL=(ALL) NOPASSWD: ALL
お気づきかもしれませんが、今回の例ではEC2インスタンス上にユーザーを追加しました。次回はユーザー/グループ管理シリーズの一環として、EC2経由でLinuxインスタンスにユーザーを追加する手順を詳しく解説する予定です。
-
Linuxのwatchコマンドの使い方と実用例をわかりやすく解説
Linuxのwatchコマンドは、指定したコマンドを一定間隔で繰り返し実行し、その結果を画面に表示し続けるためのツールです。コマンドの出力が時間とともにどう変化するかをリアルタイムで監視したい場合に非常に便利です。この記事では、watchコマンドの基本的な使い方と、覚えておくと役立つ実用的な例を紹介します。 watchコマンドの基本構文 watchコマンドの構文は以下のとおりです。 watch オプション コマンド 各要素の意味は次のとおりです。 オプション: 後述の表から選択したオプションを指定します。watchコマンドのデフォルト動作を変更できます。 コマンド: watchが繰り返し実行
-
PowerShellスクリプトに一時停止(ポーズ)を追加する方法【具体例つき】
この記事では、PowerShellスクリプトに一時停止(ポーズ)を追加する方法を解説します。 一時停止や待機コマンドを追加する方法はいくつかあります。以下では、それぞれの方法を具体例とともにわかりやすく紹介していきます。 PowerShellスクリプトに一時停止を追加する基本の方法 最も基本的なのは、Start-Sleepコマンドレットを使う方法です。以下の手順で追加できます。 スタートメニューから「PowerShell ISE」を開きます 一時停止コマンドを追加したいPowerShellスクリプトを開きます 一時停止させたい箇所に、start-sleep -Seconds 5という行を追