はじめに
前回、作成したtest3ユーザーを削除して、そのあと戻せるかどうか検証してみました。
前回のtest3ユーザー
https://qiita.com/gama1234/items/9dde01ea507cac003cfa
linuxユーザー削除の手順
削除前のユーザー情報を確認
test3のユーザーを確認した
#ユーザーアカウントの情報
# cat /etc/passwd | grep test3
test3:x:1002:1001::/home/test3:/bin/bash
#パスワード情報
# cat /etc/shadow | grep test3
test3:$パスワード情報:0:99999:7:::
ユーザー情報をアーカイブする
# cd /home
#test3ユーザーの情報をアーカイブした
# tar cvzf /home/test/test3.tar.gz ./test3
./test3/
./test3/.bash_logout
./test3/.bash_profile
./test3/.bashrc
./test3/.ssh/
./test3/.ssh/id_rsa
./test3/.ssh/id_rsa.pub
./test3/.ssh/authorized_keys
./test3/.bash_history
# アーカイブファイルが出来ていることを確認した
# ls -ltr /home/test
total 4
-rw-r--r--. 1 root root 2546 Sep 11 10:40 test3.tar.gz
test3ユーザーを削除した
# userdel test3
# echo $?
0
linuxユーザー復元の手順
アーカイブファイルを展開する
# cd /home
# tar xzvf /home/test/test3.tar.gz
./test3/
./test3/.bash_logout
./test3/.bash_profile
./test3/.bashrc
./test3/.ssh/
./test3/.ssh/id_rsa
./test3/.ssh/id_rsa.pub
./test3/.ssh/authorized_keys
./test3/.bash_history
test3ユーザーが作成されていることを確認した
上記でアーカイブファイルを展開したら、test3のhomeディレクトリは出来るがユーザー自体は存在しないことが分かった。
[root@localhost ~]# id test3
id: test3: no such user
[root@localhost ~]#
ユーザーアカウント情報ファイルに、test3ユーザーを追加した
※vipwコマンドでも、/etc/passwdファイルを編集モードで開けます。
# vi /etc/passwd
出力結果
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
test:x:1000:1000:test:/home/test:/bin/bash
test2:x:1001:1001::/home/test2:/bin/bash
以下を追加した
test3:x:1002:1001::/home/test3:/bin/bash
idコマンドで確認したところ、test3ユーザーが作成されたことを確認した
# id test3
uid=1002(test3) gid=1001(testgroup) groups=1001(testgroup)
パスワード設定されていないことを確認した。
cat /etc/shadow | grep test3
#
パスワードを設定しました。
# passwd test3
Changing password for user test3.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
test3ユーザーは、ssh接続が出来ることを確認した
test@DESKTOP-0S2U0NI:~/.ssh$ ssh -i test3.pem test3@192.168.0.4
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'test3.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "test3.pem": bad permissions
test3@192.168.0.4's password:
Last failed login: Sun Sep 11 11:08:20 JST 2022 from 192.168.0.14 on ssh:notty
There were 3 failed login attempts since the last successful login.
Last login: Sun Sep 11 10:59:52 2022 from 192.168.0.14
まとめ
検証した後に気づいたことは、この記事の手順通りに実施すると、プライマリーグループ以外でユーザーが所属しているグループは復元されません。ユーザーを削除前に、idコマンドでユーザー情報を残しておいた方が、もともとどのグループにユーザーが所属しているか分かります。プライマリグループ以外のグループはユーザー復元後、各グループにユーザーを追加することになります。