目录
-u:指定用户的UID号,注意只有未被使用的UID号方可被指定
-s:指定用户登录的shell(/bin/bash可登录系统, /sbin/nologin和/bin/false不可登录系统)
用户和用户账号
用户的类型
超级用户:root用户是Linux系统默认的超级用户,拥有用户态最高权限,超级用户在系统中是唯一的。
普通用户:root用户和其他管理员用户创建的,拥有权限有限,一般只在用户自己的宿主目录下拥有完整权限。
程序用户:安装系统和某些程序时添加的低权限用户,这些用户一般没有登录权限,仅用于维持系统或程序的正常运行。常见的有:bin、daemon、ftp、mail等。
UID(用户标识号,user identify):超级用户UID为0,程序用户的UID1~999,普通用户的UID1000+。
用户信息文件目录
Linux系统中用户账号信息存放在/etc/passwd中。
passwd中的用户具体信息与每个参数的含义如下:
heiguaner: x :1007:1007: :/home/heiguaner: /bin/bash
用户名 密码占位符 UID GID 用户全名 宿主目录 登录shell信息
其中,登录Shell信息中 /bin/bash 为可登陆系统,/sbin/nologin 和 /bin/false 为禁止用户登陆系统。
用户的密码信息存放在 etc/shadow 中。
用户增删改查相关命令
添加用户---useradd
useradd 选项 参数
常用选项
-u:指定用户的UID号,注意只有未被使用的UID号方可被指定
[root@sunny ~]# useradd -u 1200 sl007
[root@sunny ~]# tail -1 /etc/passwd
sl007:x:1200:1200::/home/sl007:/bin/bash
-d:指定用户宿主目录的位置(不可和-M同用)
[root@sunny /]# useradd -d /abc/sl008 sl008
[root@sunny /]# ls /abc/
sl008
-e:指定用户的失效时间
[root@sunny /]# useradd -e 2024-8-15 sl009
[root@sunny /]# tail -1 /etc/shadow
sl009:!!:19943:0:99999:7::19950:
-g:指定用户的基本组名(可用GID号),组名需已存在
[root@sunny /]# useradd -g sl009 sl010
[root@sunny /]# tail -2 /etc/passwd
sl009:x:1202:1202::/home/sl009:/bin/bash
sl010:x:1203:1202::/home/sl010:/bin/bash
-G:指定用户的附加组(可用GID号),组名需已存在
[root@sunny /]# useradd -G sl007 sl011
[root@sunny /]# cat /etc/group |grep "sl007"
sl007:x:1200:sl011
-M:不建立宿主目录
[root@sunny /]# useradd -M sl012
[root@sunny /]# cat /etc/passwd |grep "sl012"
sl012:x:1205:1205::/home/sl012:/bin/bash
[root@sunny /]# ls /home/
fk jiaoxue shichang sl009 sl011 xy yewu
hanmeimei lilei sl007 sl010 xy03 zhangsan
-s:指定用户登录的shell(/bin/bash可登录系统, /sbin/nologin和/bin/false不可登录系统)
[root@sunny /]# useradd -s /sbin/nologin sl013
[root@sunny /]# cat /etc/passwd |grep "sl013"
sl013:x:1206:1206::/home/sl013:/sbin/nologin
删除用户---userdel
userdel 选项 参数
常用选项
-r:可将用户的宿主用户一并删除
[root@sunny /]# userdel -r zhangsan
[root@sunny /]# ls /home/
fk hanmeimei jiaoxue lilei shichang sl010 sl011 sl013 xy xy03 yewu
用户信息变更---usermod
usermod 选项 参数
常用选项
-u:修改UID号
[root@sunny /]# tail -1 /etc/passwd
sl013:x:1206:1206::/home/sl013:/sbin/nologin
[root@sunny /]# usermod -u 1300 sl013
[root@sunny /]# tail -1 /etc/passwd
sl013:x:1300:1206::/home/sl013:/sbin/nologin
-d:修改用户宿主目录位置
[root@sunny /]# usermod -d /abc/xy03 xy03
[root@sunny /]# cat /etc/passwd |grep "xy03"
xy03:x:1002:1002::/abc/xy03:/bin/bash
-e:修改账号的失效时间
[root@sunny /]# cat /etc/shadow |grep "xy03"
xy03:!!:19937:0:99999:7:::
[root@sunny /]# usermod -e 2024-9-9 xy03
[root@sunny /]# cat /etc/shadow |grep "xy03"
-g:修改账号的基本组名(可用GID号),组名需已存在
[root@sunny /]# cat /etc/shadow |grep "xy03"
xy03:!!:19937:0:99999:7::19975:
[root@sunny /]# cat /etc/passwd |grep "xy03"
xy03:x:1002:1002::/abc/xy03:/bin/bash
[root@sunny /]# usermod -g xy xy03
[root@sunny /]# cat /etc/passwd |grep "xy03"
xy03:x:1002:1001::/abc/xy03:/bin/bash
-G:修改账号的附加组名(可用GID号),组名需已存在
[root@sunny /]# cat /etc/group |grep "xy03"
xy03:x:1002:test
[root@sunny /]# usermod -G xy test
[root@sunny /]# cat /etc/group |grep "xy"
xy:x:1001:test
xy03:x:1002:
-s:指定用户的登录shell
[root@sunny /]# cat /etc/passwd |grep "xy03"
xy03:x:1002:1001::/abc/xy03:/bin/bash
[root@sunny /]# usermod -s /sbin/nologin xy03
[root@sunny /]# cat /etc/passwd |grep "xy03"
xy03:x:1002:1001::/abc/xy03:/sbin/nologin
用户查询---id
id 参数
[root@sunny /]# id xy
uid=1001(xy) gid=1001(xy) groups=1001(xy)
[root@sunny /]# id 1001
uid=1001(xy) gid=1001(xy) groups=1001(xy)
用户账号设置密码---passwd
设置用户密码
[root@sunny /]# passwd sl011
Changing password for user sl011.
New password:
BAD PASSWORD: The password fails the dictionary check - it is too simplistic/systematic
Retype new password:
passwd: all authentication tokens updated successfully.
免交互设置用户密码
[root@sunny /]# echo 123456 |passwd --stdin sl011
Changing password for user sl011.
passwd: all authentication tokens updated successfully.
组和组账号
组账号的类型
基本组(私有组):账号只有一个基本组,一般为创建用户是指定的组。
附加组(公共组):除基本组外添加的组,可有可无,可有多个。
GID(组标识号,group identify)
用户组信息文件的目录
Linux系统中用户账号信息存放在/etc/group中。
passwd中的用户具体信息与每个参数的含义如下:
sl011 : x : 1204 :
组账号名 密码占位符 组账号UID 组账号包含的成员(不含基本组对应的用户账号)
组账号的密码信息存放在 etc/shadow 中。(一般不设密码)
用户组增删改查相关命令
添加用户组---groupadd
[root@sunny /]# groupadd sl111
[root@sunny /]# tail -1 /etc/group
sl111:x:1207:
删除用户组---groupdel
[root@sunny /]# groupdel sl111
[root@sunny /]# tail -1 /etc/group
sl013:x:1206:
查询用户所在组---groups
[root@sunny /]# groups xy
xy : xy
[root@sunny /]# groups test
test : xy
基本组 附加组
添加删除组成员---gpasswd
gpasswd 选项 组账号
常用选项
-a:向组内添加一个用户
[root@sunny /]# tail -1 /etc/group
sl013:x:1206:
[root@sunny /]# gpasswd -a lilei sl013
Adding user lilei to group sl013
[root@sunny /]# tail -1 /etc/group
sl013:x:1206:lilei
-d:从组内删除一个用户
[root@sunny /]# tail -1 /etc/group
sl013:x:1206:lilei
[root@sunny /]# gpasswd -d lilei sl013
Removing user lilei from group sl013
[root@sunny /]# tail -1 /etc/group
sl013:x:1206:
-M:可操作多个用户
[root@sunny /]# gpasswd -M xy,xy03,fk sl013
[root@sunny /]# tail -1 /etc/group
sl013:x:1206:xy,xy03,fk