useraddコマンドはLinuxのユーザを追加できるコマンドになります。このコマンドは管理者権限が必要なコマンドになります。ユーザを追加した後はpasswdコマンドでパスワードを設定できます。
ユーザの情報は/etc/passwdのファイルに保存されます。また、ユーザのパスワードは暗号化され、/etc/shadowのファイルに保存されます。
useraddコマンドとは別にDebian系のOSではadduserコマンドが存在します。このadduserコマンドは対話的にユーザを追加できるコマンドで、ユーザを追加するuseraddコマンド・パスワードを設定するpasswdコマンド・現実のユーザ情報を設定するchfnコマンドを合わせたようなコマンドになります。
目次
- 1 ユーザを追加
- 2 ホームディレクトリのためのベースディレクトリを指定(-bオプション)
- 3 ホームディレクトリを自動で作成(-mオプション)
- 4 名前情報を設定(-cオプション)
- 5 ホームディレクトリを指定(-dオプション)
- 6 設定ファイルの値を上書き(-Kオプション)
- 7 ユーザアカウントの有効期限を設定(-eオプション)
- 8 パスワードが期限切れになりアカウントロックされるまでの日数を設定(-fオプション)
- 9 グループの設定(-gオプション)
- 10 複数のグループを設定(-Gオプション)
- 11 テンプレートのディレクトリを指定(-kオプション)
- 12 ユーザが使用するシェルを設定(-sオプション)
- 13 ユーザ作成のデフォルト値を確認(-Dオプション)
ユーザを追加
useraddコマンドは管理者権限が必要なコマンドになります。useraddコマンドの引数は追加したいユーザ名を入力して実行すると、ユーザを追加できます。
コマンド例
| 1 | sudo useradd testuser | 
その後、passwdコマンドを利用するとパスワードを設定できます。このパスワード入力は新しいパスワードを入力した後に、もう一度同じパスワードを入力します。
コマンド例と実行結果
| 1 2 3 4 | $ sudo passwd testuser Enter new UNIX password:  Retype new UNIX password:  passwd: password updated successfully | 
ユーザ情報を確認
| 1 2 3 4 5 | $ sudo ./result.sh testuser /etc/passwd testuser:x:1004:1005::/home/testuser: /etc/shadow testuser:!:17695:0:99999:7::: /etc/group testuser:x:1005: /etc/gshadow testuser:!:: | 
補足
useraddコマンドでユーザが追加されたときに、/etc/passwd等のユーザ情報を管理しているファイルに追加されたユーザの情報の行が追加されます。
「ユーザ情報を確認」でのresult.shは、/etc/passwd等のファイルにあるユーザ情報や/etc/group等のグループ情報を確認してみる簡易的なスクリプトになります。このスクリプトの引数としてユーザ名が入ります。result.shの内容は以下のようになります。
result.sh
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/bin/bash main () {   local name="^$1"   local root_flag="${EUID:-${UID}}"   printfile "passwd" $name    [ $root_flag = 0 ] && printfile "shadow" $name   printfile "group" $name    [ $root_flag = 0 ] && printfile "gshadow" $name  } printfile () {   paste -d' ' <(echo '/etc/'"$1") <(cat '/etc/'"$1" | grep "$2") } [ -n "$1" ] && main "$1" | 
/etc/passwd等のファイルのデータは、":"の文字でデータが区切られています。それぞれのファイルでのそれぞれの列の意味はmanコマンドで意味を確認できます。
| 1 2 3 4 | man 5 passwd man 5 shadow man 5 group man 5 gshadow | 
passwdコマンドでパスワードを設定すると、/etc/shadowのファイルの2列目に暗号化されたパスワードがデータに追加されます。
| 1 2 3 4 5 6 7 8 9 | $ sudo cat /etc/shadow | grep '^testuser' testuser:!:17695:0:99999:7::: $ sudo passwd testuser Enter new UNIX password:  Retype new UNIX password:  passwd: password updated successfully $ $ sudo cat /etc/shadow | grep '^testuser' testuser:$6$i9lJbxpX$gMU/r2qGSqccakJkkuzqCBtlgTEd4ROTGvAaeiKMmpY1jJ1GrHML1j0U/91EpERqIq2E0M1EqjbzKeu7E75NO1:17696:0:99999:7::: | 
さらに、chfnコマンドを用いると現実のユーザ情報を入力することができます。ただし、プライバシーの問題もあるため、設定するかどうかはよく考える必要があります。
| 1 2 3 4 5 6 7 8 9 10 11 | $ sudo chfn testuser Changing the user information for testuser Enter the new value, or press ENTER for the default     Full Name []: my name     Room Number []: my office location     Work Phone []: xxx-xxxx     Home Phone []: xxx-xxxx-xxxx     Other []: example     $ $ cat /etc/passwd | grep '^testuser' testuser:x:1004:1005:my name,my office location,xxx-xxxx,xxx-xxxx-xxxx,example:/home/testuser: | 
また、上の情報はfingerコマンドを利用することで、情報を参照することができます。
| 1 2 3 4 5 6 7 | $ finger testuser Login: testuser                   Name: my name Directory: /home/testuser               Shell: /bin/sh Office: my office location, xxx-xxxx    Home Phone: xxx-xxxx-xxxx Never logged in. No mail. No Plan. | 
ホームディレクトリのためのベースディレクトリを指定
(-bオプション)
-bオプションはホームディレクトリのためのベースディレクトリを指定します。
ベースディレクトリを指定したら、ベースディレクトリ名(例:/home/basedir)と作成されるユーザ名(例:testuser)を合わせたディレクトリ名がホームディレクトリ(例:/home/basedir/testuser)になります。
コマンド例と実行結果
| 1 2 3 4 5 6 7 8 9 10 | $ whoami ubuntu $ sudo mkdir /home/basedir $ sudo useradd -b /home/basedir -m testuser $  $ sudo su - testuser $ whoami testuser $ pwd /home/basedir/testuser | 
-mオプションはホームディレクトリを自動で生成するオプションです。
'sudo su - testuser'の'-'は、-lまたは--loginオプションと同じ意味で、このコマンドはtestuserというユーザに直接ログインしたかのように、環境を初期化してユーザを切り替えるコマンドになります。
また、ユーザにログインするコマンドとしてはloginコマンドがあります。
ホームディレクトリを自動で作成
(-mオプション)
-mオプションを用いると、作成するユーザのホームディレクトリが存在しないときに、ホームディレクトリを自動で作成します。
コマンド例と実行結果
| 1 2 3 4 5 | $ ls /home/ ubuntu $ sudo useradd -m testuser $ ls /home/ testuser  ubuntu | 
名前情報を設定
(-cオプション)
-cオプションはコメントとして名前情報を設定できます。この情報はfingerコマンドで確認できます。
コマンド例と実行結果
| 1 2 3 4 5 6 7 | $ sudo useradd -c 'my name' testuser $ finger testuser Login: testuser                   Name: my name Directory: /home/testuser               Shell: /bin/sh On since Thu Jun 14 13:36 (JST) on pts/4   1 second idle No mail. No Plan. | 
ホームディレクトリを指定
(-dオプション)
-dオプションはホームディレクトリを指定できます。ホームディレクトリはユーザがログインしたときの最初のディレクトリになります。
-dオプションを利用しない場合、デフォルトのホームディレクトリは、ベースディレクトリに作成するユーザ名をつけたディレクトリになります。
コマンド例と実行結果
| 1 2 3 4 5 6 | $ sudo mkdir /home/commondir $ sudo useradd -d /home/commondir testuser $  $ sudo su - testuser $ pwd /home/commondir | 
設定ファイルの値を上書き
(-Kオプション)
-Kオプションは/etc/login.defにあるユーザ作成時に利用される設定をKEY=VALUEの形式で上書きできます。また、-Kオプションは複数回利用できます。
指定できる変数の例
| 設定変数 | 意味 | 
|---|---|
| CREATE_HOME | ホームディレクトリを作成するかどうか | 
| GID_MAX | グループID(GID)の最大値 | 
| GID_MIN | グループID(GID)の最小値 | 
| PASS_MAX_DAYS | パスワードの有効日数 (指定しない場合、値は-1で制限なし) | 
| PASS_MIN_DAYS | パスワードの変更から次に変更できるまでの日数 (指定しない場合、値は-1で制限なし) | 
| PASS_WARN_AGE | パスワードの期限切れの何日前に注意喚起をするかの日数 (負の値で注意なし) | 
| UID_MAX | ユーザID(UID)の最大値 | 
| UID_MIN | ユーザID(UID)の最小値 | 
コマンド例と実行結果
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | $ sudo useradd -K PASS_MAX_DAYS=1 -K PASS_WARN_AGE=3 testuser $ sudo passwd testuser Enter new UNIX password:  Retype new UNIX password:  passwd: password updated successfully $  $ sudo login testuser Password:  Warning: your password will expire in 1 day Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.13.0-32-generic x86_64)  * Documentation:  https://help.ubuntu.com  * Management:     https://landscape.canonical.com  * Support:        https://ubuntu.com/advantage 139 packages can be updated. 82 updates are security updates. $  | 
ユーザアカウントの有効期限を設定
(-eオプション)
-eオプションを用いるとユーザアカウントに有効期限を設定できます。その有効期限の日付がきた場合、そのユーザはロックされログインできなくなります。
有効期限の日付の書式はYYYY-MM-DDになります。
コマンド例と実行結果
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | $ date '+%Y-%m-%d' 2018-06-15 $ sudo useradd -e 2018-06-14 testuser $ sudo passwd testuser Enter new UNIX password:  Retype new UNIX password:  passwd: password updated successfully $  $ sudo login testuser Password:  Your account has expired; please contact your system administrator Authentication failure | 
ユーザの有効期限を変更するにはusermodコマンドやchageコマンドが利用できます。usermodコマンドの場合は-eオプションを用いて、以下のように変更できます。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | $ sudo login testuser Password:  Your account has expired; please contact your system administrator Authentication failure $  $ sudo usermod -e 2018-06-16 testuser $ sudo login testuser Password:  Last login: 金  6月 15 12:51:46 JST 2018 on pts/4 Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.13.0-32-generic x86_64)  * Documentation:  https://help.ubuntu.com  * Management:     https://landscape.canonical.com  * Support:        https://ubuntu.com/advantage 139 packages can be updated. 82 updates are security updates. $  | 
また、chageコマンドはパスワードなど有効期限を設定できるコマンドになります。chageコマンドの場合は-Eオプションを用いるとアカウントの有効期限を変更できます。アカウントの有効期限の書式はuseraddコマンドと同様にYYYY-MM-DDの書式になります。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | $ sudo login testuser Password:  Your account has expired; please contact your system administrator Authentication failure $  $ sudo chage -E -1 testuser $ sudo login testuser Password:  Last login: 金  6月 15 13:01:36 JST 2018 on pts/4 Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.13.0-32-generic x86_64)  * Documentation:  https://help.ubuntu.com  * Management:     https://landscape.canonical.com  * Support:        https://ubuntu.com/advantage 139 packages can be updated. 82 updates are security updates. $  | 
usermodコマンド、chageコマンドともにオプションの後の日付を-1の数字にすることで、アカウントの有効期限はなくなります。
パスワードが期限切れになりアカウントロックされるまでの日数を設定
(-fオプション)
-fオプションはパスワードが期限切れになってから、アカウントがロックされるまでの日数を設定できます。また、-fオプションに-1を指定することでこの機能を無効にできます。
コマンド例と実行結果
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | $ date '+%Y-%m-%d' 2018-06-15 $ sudo useradd -K PASS_MAX_DAYS=0 -f 0 testuser $ sudo passwd testuser Enter new UNIX password:  Retype new UNIX password:  passwd: password updated successfully $ $ sudo chage -d 2018-06-14 testuser $ sudo login testuser Password:  Your account has expired; please contact your system administrator Authentication failure $  $ sudo chage -d 2018-06-15 testuser $ sudo login testuser Password:  Warning: your password will expire in 0 days Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.13.0-32-generic x86_64)  * Documentation:  https://help.ubuntu.com  * Management:     https://landscape.canonical.com  * Support:        https://ubuntu.com/advantage 139 packages can be updated. 82 updates are security updates. $  | 
chageコマンドの-dオプションでパスワードを変更した日付を変更できます。
グループの設定
(-gオプション)
-gオプションを利用するとユーザの作成時に、既に存在しているグループからグループを設定できます。
コマンド例と実行結果
| 1 2 3 | $ sudo useradd -g group1 testuser $ id -a testuser uid=1004(testuser) gid=1005(group1) groups=1005(group1) | 
複数のグループを設定
(-Gオプション)
-Gオプションを用いると複数のグループを設定できます。グループはコンマ区切りでリストします。
コマンド例と実行結果
| 1 2 3 | $ sudo useradd -G group1,group2,group3 testuser $ id -a testuser uid=1004(testuser) gid=1008(testuser) groups=1008(testuser),1005(group1),1006(group2),1007(group3) | 
テンプレートのディレクトリを指定
(-kオプション)
-kオプションはテンプレートのディレクトリを指定できます。-kオプションは-mオプションでホームディレクトリを自動作成するときに、テンプレートのディレクトリにあるファイルがホームディレクトリへコピーされます。
デフォルトのテンプレートとなるディレクトリは/etc/skelのディレクトリや/etc/default/useraddの変数SKELで記述されるディレクトリになります。
コマンド例と実行結果
| 1 2 3 4 5 | $ ls skelton/ file1.txt  file2.txt  file3.txt $ sudo useradd -k skelton -d /home/testuser -m testuser $ sudo ls /home/testuser/ file1.txt  file2.txt  file3.txt | 
ユーザが使用するシェルを設定
(-sオプション)
-sオプションはユーザが使用するシェルを設定できます。ログインをしないユーザの場合はnologinのシェルを指定するとログインできないユーザを作成できます。
コマンド例と実行結果
| 1 2 3 | $ sudo useradd -s /usr/sbin/nologin testuser $ sudo su - testuser This account is currently not available. | 
ユーザ作成のデフォルト値を確認
(-Dオプション)
-Dオプションはユーザ作成のデフォルト値を確認できます。
設定ファイルの/etc/default/useraddを編集することで変更できます。
コマンド例と実行結果
| 1 2 3 4 5 6 7 8 | $ useradd -D GROUP=100 HOME=/home INACTIVE=-1 EXPIRE= SHELL= SKEL=/etc/skel CREATE_MAIL_SPOOL=no | 
