3.1用户配置文件和密码配置文件
3.2 用户组管理
3.3用户管理
3.4usermod命令
3.5用户密码管理
3.6mkpasswd管理
3.7su命令
3.8sudo命令
3.9限制root远程登录
3.1用户配置文件和密码配置文件
这两个文件非常重要,关乎安全。
/etc/passwd 这个也是7段内容
/etc/shadow
3.2用户组管理
用法:groupadd[-g GID ] gruoupname,例如:
Groupdel groupname 删除用户组
3.3用户管理
用法:useradd [-u UID][-g GID][-d HOME][-M][-s]
-u表示自定义uid
-g表示新增用户属于哪个组,后面可以跟组id,也可以跟组名
-d 指定家目录
-M表示不建立家目录
-s表示自定义shell
userdel [-r]username 表示删除用户时,一并删除用户的家目录
3.4usermod命令
usermod - 修改用户帐户信息 modify a user account
- a | -- append ##把用户追加到某些组中,仅与-G选项一起使用
- c | -- comment ##修改/etc/passwd文件第五段comment
- d | -- home ##修改用户的家目录通常和-m选项一起使用
- e | -- expiredate ##指定用户帐号禁用的日期,格式YY-MM-DD
- f | -- inactive ##用户密码过期多少天后采用就禁用该帐号,0表示密码已过期就禁用帐号,-1表示禁用此功能,默认值是-1
- g | -- gid ##修改用户的gid,改组一定存在
- G | -- groups ##把用户追加到某些组中,仅与-a选项一起使用
- l | -- login ##修改用户的登录名称
- L | -- lock ##锁定用户的密码
- m | -- move - home ##修改用户的家目录通常和-d选项一起使用
- s | -- shell ##修改用户的shell
- u | -- uid ##修改用户的uid,该uid必须唯一
- U | -- unlock ##解锁用户的密码
3.5用户密码管理
现在密码对于我们日常用户是非常重要的,也是管理员非常重要的一个任务。
命令:passwd 创建密码有以下几个规则尽量复杂
a 长度大于10个字符
b密码中包含大小写字母、数字以及特殊字符*、&、%等;
c不规则性(不要出现一些非常容易的“1111111”“123456789”“abcdef”)
d密码中不要带自已的名字、电话、生日以及公司名字等。
只有root才能修改其它账号的密码,用户只能修改自已的密码
3.6 mkpasswd管理
这个命令用于生成密码,需要安装一个expect软件包
用法:mkpasswd –l 指定长度 -s指定特殊字符个数 -d指定数字个数
3.7su命令
用法: su [-] username
加上“-”相当于初始化当前用户的各种环境变量
普通用户 su- 就是切换到root用户
下面我们来看一下实际操作中加“-”和不加“-”
[root@localhost /]# su - user1
[user1@localhost ~]$ pwd
/home/user1
[user1@localhost ~]$ id
uid=1001(user1) gid=1001(user1) groups=1001(user1) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
下面我们不加“-”
[root@localhost ~]# su user1
[user1@localhost root]$ pwd
/root
[user1@localhost root]$ id
uid=1001(user1) gid=1001(user1) groups=1001(user1) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
3.8sudo命令
为了防止我们泄露root密码报露安全问题,就有sudo这个命令。默认情况下,只有root用户才能使用sudo命令,普通用户想要使用sudo,是需要root预先设定的。
需要对这个配置文件进行编辑:/etc/sudoers
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
## Same thing without a password
# %wheel ALL=(ALL) NOPASSWD: ALL
## Allows members of the users group to mount and unmount the
## cdrom as root
# %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
主要是看这行配置:只要是wheel这个组的所有用户都拥有了sudo的权利。
## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
3.9限制root远程登录
在实际生产环境中,可能一台服务 器很多人员都需要去远程登录使用它,有时候为了安全起见我们会限制root用户远程登录。
这里需要更改这个配置文件:/etc/ssh/sshd_config
改为以后需要重启服务:
Sysstemctl restart sshd.service
这里只是限制从ssh这种登录方式。