1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Linuxユーザーとグループ管理コマンド - ユーザーの削除

Posted at

ユーザーの削除

userdel コマンドは Linux のユーザーを削除するために使用されます。

フォーマット:

userdel [options] LOGIN

よく使うオプション

  • -f | --force # 強制削除。ログイン状態のユーザーでも削除する
  • -r | --remove # ホームディレクトリとメールスプールを削除する

例:

ユーザーの作成とパスワードの設定

[root@rocky8 ~]# useradd zhangsan
[root@rocky8 ~]# passwd zhangsan
Changing password for user zhangsan.
New password:

関連データの確認

[root@rocky8 ~]# id zhangsan
uid=1004(zhangsan) gid=1004(zhangsan) groups=1004(zhangsan)

[root@rocky8 ~]# ll -a /home/zhangsan
total 12
drwx------. 3 zhangsan zhangsan 78 Jun 25 15:32 .
drwxr-xr-x. 7 root root 74 Jun 25 15:32 ..
-rw-r--r--. 1 zhangsan zhangsan 18 Jul 27 2021 .bash_logout
-rw-r--r--. 1 zhangsan zhangsan 141 Jul 27 2021 .bash_profile
-rw-r--r--. 1 zhangsan zhangsan 376 Jul 27 2021 .bashrc
...

[root@rocky8 ~]# ll /var/spool/mail/zhangsan
-rw-rw----. 1 zhangsan mail 0 Jun 25 15:32 /var/spool/mail/zhangsan

別のターミナルで zhangsan としてログイン

[zhangsan@rocky8 ~]$ whoami
zhangsan

ログイン中のユーザーの削除に失敗

[root@rocky8 ~]# userdel zhangsan
userdel: user zhangsan is currently used by process 5162

強制削除

[root@rocky8 ~]# userdel -f zhangsan
userdel: user zhangsan is currently used by process 5162

[root@rocky8 ~]# id zhangsan
id: ‘zhangsan’: no such user

削除後のエラー

[zhangsan@rocky8 ~]$ whoami
whoami: cannot find name for user ID 1004

ユーザーが削除された後、そのユーザーのファイルの所有者が表示されなくなり、UID のみが表示される

[root@rocky8 ~]# ll -a /home/zhangsan
total 16
drwx------. 4 1004 1004 131 Jun 25 15:45 .
drwxr-xr-x. 7 root root 74 Jun 25 15:32 ..
-rw-------. 1 1004 1004 58 Jun 25 15:45 .bash_history
-rw-r--r--. 1 1004 1004 18 Jul 27 2021 .bash_logout
-rw-r--r--. 1 1004 1004 141 Jul 27 2021 .bash_profile
-rw-r--r--. 1 1004 1004 376 Jul 27 2021 .bashrc
drwx------. 3 1004 1004 19 Jun 25 15:39 .config
...

[root@rocky8 ~]# ll /var/spool/mail/zhangsan
-rw-rw----. 1 1004 mail 0 Jun 25 15:32 /var/spool/mail/zhangsan

元のユーザーの UID を使用して新しいユーザーを作成すると、元のユーザーのファイルを継承できる

[root@rocky8 ~]# useradd -u 1004 lisi

[root@rocky8 ~]# ll /home/
total 4
drwx------. 3 lisi lisi 78 Jun 25 15:48 lisi
drwx------. 4 lisi lisi 131 Jun 25 15:45 zhangsan
...

[root@rocky8 ~]# ll /var/spool/mail/
total 0
-rw-rw----. 1 lisi mail 0 Jun 25 15:48 lisi
-rw-rw----. 1 lisi mail 0 Jun 25 15:32 zhangsan
...

ユーザーファイルの削除

[root@rocky8 ~]# userdel -r lisi

[root@rocky8 ~]# ll /home/lisi
ls: cannot access '/home/lisi': No such file or directory

[root@rocky8 ~]# ll /var/spool/mail/lisi
ls: cannot access '/var/spool/mail/lisi': No such file or directory
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?