# useradd violet02 useradd: user 'violet02' already exists # id violet02 uid=1003(violet02) gid=1003(violet02) groups=1003(violet02) # id violet03 uid=1004(violet03) gid=1004(violet03) groups=1004(violet03) # usermod -G violet02 violet03 # id violet03 uid=1004(violet03) gid=1004(violet03) groups=1004(violet03),1003(violet02) # userdel violet02 userdel: group violet02 not removed because it has other members. # id violet02 id: violet02: no such user # ll -d /home/violet02 drwx------ 2 1003 violet02 62 Jan 15 20:48 /home/violet02
可以从例子中看到,可以删除该用户,但该用户的主组无法删除。
上面例子中,用户的家目录未被删除。
接着上个例子创建被删除的用户
1 2 3 4 5 6 7 8
# useradd violet02 useradd: group violet02 exists - if you want to add this user to that group, use -g. # useradd violet02 -g violet02 useradd: warning: the home directory already exists. Not copying any file from skel directory into it. Creating mailbox file: File exists # id violet02 uid=1008(violet02) gid=1003(violet02) groups=1003(violet02)
用于显示用户信息。用户打印有效用户的UID, GID和组,对于不存在的用户,该命令会提示形如"id 查询的用户名: no such user"的错误信息
应用 - 查询用户UID,GID,GROUPS
1
$ id violet
应用 - 检查用户是否存在
1 2
$ id violet100 id: violet100: no such user
w命令
1 2 3 4 5 6 7
$ w 17:02:27 up 3 days, 14:44, 4 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root tty1 Thu14 26:24m 0.02s 0.02s -bash violet pts/0 192.168.10.1 16:40 12:35 0.05s 0.05s -bash root pts/4 192.168.10.1 15:00 3.00s 0.03s 0.00s w root pts/5 192.168.10.1 15:00 13:55 0.30s 0.30s -bash
列明
简明描述
描述
USER
用户名称
用于显示使用当前终端的用户名称
TTY
终端
用于显示登陆终端的类型(tty/pts)
FROM
登录IP
用户登录到哪个IP地址
LOGIN@
登录时间
用户登录的时间
IDLE
空闲时间
用户上次使用终端进行操作的时间
JCPU
所有进程使用时长
与用户登录的终端相关的所有进程的使用时长
PCPU
当前进程使用时长
用户当前使用的进程(该进程信息在WHAT列有显示)的使用时长
WHAT
当前进程信息
用户当前使用的进程和相关选项或参数。
last命令
用于显示用户上次登录信息
1 2 3 4 5 6 7 8 9
# last violet pts/0 192.168.10.1 Fri Jan 22 16:40 still logged in root pts/5 192.168.10.1 Fri Jan 22 15:00 still logged in root pts/4 192.168.10.1 Fri Jan 22 15:00 still logged in ... root tty1 Fri Dec 25 14:55 - 15:22 (00:27) reboot system boot 3.10.0-862.el7.x Fri Dec 25 14:43 - 15:22 (00:39)
# GROUP:当group有多个时,应该有逗号进行分割,而不是空格。 $ usermod -a -G GROUP USER
violet用户加入games组的实例如下
1 2 3 4 5 6 7
# id violet uid=1000(violet) gid=1000(violet) groups=1000(violet) # usermod -a -G games violet # id violet uid=1000(violet) gid=1000(violet) groups=1000(violet),20(games) # awk '$1 ~ /games/{print}' /etc/group games:x:20:violet
games:x:20:violet表示games组中gid=20,该组中用户有violet。
用户退出一个组
-d - 在用户组中删除指定用户
语法格式如下
1
gpasswd -d USER GROUP
用户退出一个组的正确步骤:
利用id检查退出的组是否为主组,若退出的组为主组,需要先修改主组为次要组,否则进行下一步
使用"gpasswd -d USER GROUP"退出组。
violet用户退出games组(其中games为主组)的实例
1 2 3 4 5 6 7 8 9
# id violet uid=1000(violet) gid=20(games) groups=20(games),1000(violet) # usermod -g violet violet # id violet uid=1000(violet) gid=1000(violet) groups=1000(violet),20(games) # gpasswd -d violet games Removing user violet from group games # id violet uid=1000(violet) gid=1000(violet) groups=1000(violet)
# id violet uid=1000(violet) gid=1000(violet) groups=1000(violet) # usermod -g games violet # id violet uid=1000(violet) gid=20(games) groups=20(games),1000(violet) # gpasswd -d violet games Removing user violet from group games gpasswd: user 'violet' is not a member of 'games' # awk -F: '$1 ~ /games$/{print}' /etc/group games:x:20:
-c - 添加用户备注信息,这个备注是关于GECOS (the full name of the user)的信息,备注信息会添加到/etc/passwd
1
$ usermod -c "GECOS Comment" USER
-d - 改变用户的家目录,原来家目录的内容不会移动到新的家目录
-m - 配合’-d’使用,改变家目录的同时,原家目录内容也会迁移到新的家目录。
1 2 3 4
# By default, the command doesn’t move the content of the user’s home directory to the new one. $ usermod -d HOME_DIR USER # To move the content, use the -m option. If the new directory does not already exist, it is created: $ usermod -d HOME_DIR -m USER
-s - 改变用户默认的shell
The default shell is the shell that is run after you log in to the system. By default, on most Linux systems, the default shell is set to Bash Shell.
You can find out what shells are available on your system by displaying the /etc/shells file’s content.
1 2
# SHELL:其他shell的绝对路径 $ usermod -s SHELL USER
-u - 改变用户UID
1
$ usrmod -u UID USER
-l - 改变用户名称
1 2 3 4
# NEW_USER: 新用户名称 $ usermod -l NEW_USER USER # In the example below, we are renaming the user linuxize to lisa to “1050”: sudo usermod -l linuxize lisa
$ usermod -e DATE USER # For example, to disable the user linuxize on 2022-02-21, you would run the following command: sudo usermod -e "2022-02-21" linuxize # To disable the expiration of an account, set an empty expiry date: sudo usermod -e "" linuxize
查询用户到期时间
1
$ chage -l USER
-L - 锁定用户。
The commands will insert an exclamation point (!) mark in front of the encrypted password. When the password field in the /etc/shadow file contains an exclamation point, the user will not be able to login to the system using password authentication. Other login methods, like key-based authentication or switching to the user are still allowed. If you want to lock the account and disable all login methods, you also need to set the expiration date to 1.