(转)Centos 7.3 用户和组管理

Centos 7.3 用户和组管理

原文:http://blog.csdn.net/github_39069288/article/details/73306489

3.1 用户和密码配置文件

  • passwd配置文件详解
/etc/passwd说明
root:x:0:0:root:/root:/bin/bash
/etc/passwd由―:分割成7个字段,每个字段的具体含义是:
(1) 用户名(如第一行中的root就是用户名),代表用户账号的字符串。
(2) 第二个字段存放的是该账号的口令。
(3) 这个数字代表用户标识号,也叫做uid。
(4) 表示组标识号,也叫做gid。
(5) 注释说明,该字段没有实际意义。
(6) 用户的家目录。
(7) 用户的shell。除了/bin/bash外还有/sbin/nologin比较多,它表示不允许该账号登录,建立账号时添加-s选项指定第七段的值。
  • shadow配置文件详解
/etc/shadow说明
bin:*:15980:0:99999:7: : :
(1) 用户名。
(2) 用户密码,这个密码已经加密,该文件属性设置为000,但是root账户是可以访问或更改的。
(3) 上次更改密码的日期,这个数字是这样计算得来的,距离1970年1月1日到上次更改密码的日期,例如上次更改密码的日期为2012年1月1日,则这个值就是365*(2012-1970)+(2012-1970)/4+1=15341。因为如果是闰年,则有366天。
(4) 要过多少天才可以更改密码,默认是0,即不限制。
(5) 密码多少天后到期。默认是99999,可以理解为永远不需要改。
(6) 密码到期前的警告期限。
(7) 账号失效期限。密码已经到期后多少天失效。
(8) 账号的生命周期。跟第三段一样。
(9) 作为保留用的,没有什么意义。

3.2 用户组管理

/etc/group、/etc/gshadow配置文件

  • 系统自动备份的配置文件,源文件删除后可将备份文件恢复。
[root@centos7-1 ~]# ls /etc/passwd
passwd   passwd-  
[root@centos7-1 ~]# ls /etc/group
group   group-  
[root@centos7-1 ~]# ls /etc/shadow
shadow   shadow-  
[root@centos7-1 ~]# ls /etc/gshadow
gshadow   gshadow- 
  • groupadd 创建组
指定gid创建组:
[root@centos7-1 ~]# groupadd -g 1005 temence
[root@centos7-1 ~]# tail -n1 /etc/group
temence:x:1005:
  • groupdel 删除组
删除空组:
[root@centos7-1 ~]# groupdel temence
[root@centos7-1 ~]# tail -n1 /etc/group
ggg:x:1001:

删除非空组:
[root@centos7-1 ~]# groupdel ggg
groupdel:不能移除用户“ggg”的主组
删除非空组时,需要将组里用户删掉才可删除该组
  • groupmems 组成员管理
用于查看扩展组下的所有用户
用法:groupmems [选项] [动作]

选项:
  -g, --group groupname         更改组 groupname,而不是用户的组(只 root)
  -R, --root CHROOT_DIR         chroot 到的目录

动作:
  -a, --add username            将用户 username 添加到组成员中
  -d, --delete username         从组的成员中删除用户 username
  -h, --help                    显示此帮助信息并推出
  -p, --purge                   从组中移除所有成员
  -l, --list                    列出组中的所有成员

3.3 用户管理

  • useradd、adduser 新建用户 

centos7,默认新建的uid和gid都是从1000开始,1000之前为系统保留用户。centos6,默认新建的uid和gid都是从500开始,500之前为系统保留用户。

-u          指定uid或者属主名字
-g          指定gid或者属组名字
-d          指定用户家目录
-s          指定shell
-M          不创建家目录
-G          添加扩展组,可以一次性添加多个组,使用“,”分开
指定uid、gid创建用户:
[root@centos7-1 ~]# tail -n2 /etc/passwd
gbj:x:1000:1000::/home/gbj:/bin/bash
ggg:x:1001:1001::/home/ggg:/bin/bash
[root@centos7-1 ~]# tail -n2 /etc/group
gbj:x:1000:
ggg:x:1001:
[root@centos7-1 ~]# useradd -u 1003 -g 1000 temence
[root@centos7-1 ~]# tail -n2 /etc/group
gbj:x:1000:
ggg:x:1001:
[root@centos7-1 ~]# tail -n2 /etc/passwd
ggg:x:1001:1001::/home/ggg:/bin/bash
temence:x:1003:1000::/home/temence:/bin/bash

指定家目录、shell
[root@centos7-1 ~]# useradd -d /home/qwer -s /sbin/nologin qwer
[root@centos7-1 ~]# tail -n2 /etc/passwd
temence:x:1003:1000::/home/temence:/bin/bash
qwer:x:1004:1004::/home/qwer:/sbin/nologin

创建用户同时不创建用户家目录
[root@centos7-1 ~]# useradd -M ghj
[root@centos7-1 ~]# tail
tail   tailf  
[root@centos7-1 ~]# tail -n2 /etc/passwd
qwer:x:1004:1004::/home/qwer:/sbin/nologin
ghj:x:1005:1005::/home/ghj:/bin/bash
[root@centos7-1 ~]# ls /home/
gbj  ggg  qwer  temence
  • userdel 删除用户
userdel -r [用户]         删除用户的同时删除用户家目录

3.4 usermod更改用户属性

usermod 命令修改系统帐户文件来反映通过命令行指定的变化

1. 首先看看usermod都是有哪些参数

[root@hxweb101 ~]$ usermod --help
Usage: usermod [options] LOGIN

Options:
  -a, --append                 append the user to the supplemental GROUPS
                               (use only with -G)
  -c, --comment COMMENT        new value of the GECOS field
  -d, --home HOME_DIR          new home directory for the user account
  -e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
  -f, --inactive INACTIVE      set password inactive after expiration
                               to INACTIVE
  -g, --gid GROUP              force use GROUP as new primary group
  -G, --groups GROUPS          new list of supplementary GROUPS
  -h, --help                   display this help message and exit
  -l, --login NEW_LOGIN        new value of the login name
  -L, --lock                   lock the user account
  -m, --move-home              move contents of the home directory to the new
                               location (use only with -d)
  -o, --non-unique             allow using duplicate (non-unique) UID
  -p, --password PASSWORD      use encrypted password for the new password
  -s, --shell SHELL            new login shell for the user account
  -u, --uid UID                new UID for the user account
  -U, --unlock                 unlock the user account
  -Z, --selinux-user           new selinux user mapping for the 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     ##解锁用户的密码

实例说明:

比如我有hexu 和 www 用户和用户组:

  1. www创建为内部用户禁止远程登陆,用于运行web service。
  2. hexu创建为外部使用用户,用于登陆、调试或是上传代码等。

这时可以考虑hexu添加到www用户组,并将代码目录允许组读写:

$ usermod -a -G www hexu   # 将hexu添加到www用户组
$ chmod -R ug+w /data/code  # 将目录添加用户组可写

下面是网友的整理,转过来了: 示例(Examples):

#1,新建用户test,密码test,另外添加usertest组
$ useradd test 
$ echo "test" | passwd --stdin test 
$ groupadd usertest 
#2,把test用户加入usertest组
$ usermod -aG usertest test ##多个组之间用空格隔开 
$ id test 
  uid=500(test) gid=500(test) groups=500(test),501(usertest) 
#3,修改test用户的家目录
$ usermod -md /home/usertest 
$ ls /home 
  usertest 
#4,修改用户名
$ usermod -l testnew(新用户名称)  test(原来用户名称) 
$ id testnew 
  uid=500(testnew) gid=500(test) groups=500(test),501(usertest) 
#5,锁定testnew的密码
$ sed -n '$p' /etc/shadow 
  testnew:$6$1PwPVBn5$o.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t 
  Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7::: 
$ usermod -L testnew 
$ sed -n '$p' /etc/shadow 
  testnew:!$6$1PwPVBn5$o.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t 
  Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7::: 
#6,解锁testnew的密码
$ usermod -U testnew 
$ sed -n '$p' /etc/shadow 
  testnew:$6$1PwPVBn5$o.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t 
  Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7::: 
#7,修改用户的shell
$ sed '$!d' /etc/passwd 
  testnew:x:500:500::/home/usertest:/bin/bash 
$ usermod -s /bin/sh testnew 
$ sed -n '$p' /etc/passwd 
  testnew:x:500:500::/home/usertest:/bin/sh 
# 也可以手动编辑 vi /etc/passwd 找到testnew编辑保存即可
$ vi /etc/password

#8,修改用户的UID
$ usermod -u 578 testnew (UID必须唯一) 
$ id testnew 
  uid=578(testnew) gid=500(test) groups=500(test),501(usertest) 
#9,修改用户的GID
$ groupadd -g 578 test1 
$ usermod -g 578 testnew (578组一定要存在) 
$ id testnew 
  uid=578(testnew) gid=578(test1) groups=578(test1),501(usertest) 
#10,指定帐号过期日期
$ sed -n '$p' /etc/shadow 
  testnew:$6$1PwPVBn5$o.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t 
  Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7::: 
$ usermod -e 2012-09-11 testnew 
$ sed -n '$p' /etc/shadow 
  testnew:$6$1PwPVBn5$o.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t 
  Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7::15594: 
11,指定用户帐号密码过期多少天后,禁用该帐号
$ usermod -f 0 testnew 
$ sed -n '$p' /etc/shadow 
  testnew:$6$1PwPVBn5$o.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t 
  Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7:0:15594:

注意(caution): usermod不允许你改变正在线上的使用者帐号名称。当usermod用来改变userID,必须确认这名user没在电脑上执行任何程序

/etc/passwd user_name:x:uid:gid:commnet:home:shell

/etc/shadow username:passwd:lastchg:min:max:warn:inactive:expire:flag

  • –用户名
  • –密码
  • –从1970年1月1日起到上次修改密码所经过的天数
  • –密码再过几天可以被变更(0表示随时可以改变)
  • –密码再过几天必须被变更(99999表示永不过期)
  • –密码过期前几天提醒用户(默认为一周)
  • –密码过期几天后帐号被禁用
  • –从1970年1月1日算起,多少天后账号失效
  • 查看用户属性
[root@centos7-1 ~]# id root
uid=0(root) gid=0(root) 组=0(root) 

3.5 用户密码管理

[root@centos7-1 ~]# ll /usr/bin/passwd 
-rwsr-xr-x. 1 root root 27832 6月  10 2014 /usr/bin/passwd

passwd拥有set_uid权限,可以让其他用户也拥有root权限使用该密码
  • passwd [用户]更改密码
  • cat /etc/shadow配置文件 

配置文件第二列为用户密码 
如果为 *,则表示该用户密码被锁定 
如果为!,则表示该用户密码为空。

[root@centos7-1 ~]# cat /etc/shadow
root:$6$964uE1k0sQeev/EK$fHR5bvMDuvoQ/CaHprM4r5U48hCtZ8yty8aPSQe.z8NlvrZESJrKcbbTixvetRiJbz4.8Z24t4Fy9UzP1zSds.::0:99999:7:::
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::
shutdown:*:17110:0:99999:7:::
halt:*:17110:0:99999:7:::
mail:*:17110:0:99999:7:::
operator:*:17110:0:99999:7:::
games:*:17110:0:99999:7:::
ftp:*:17110:0:99999:7:::
nobody:*:17110:0:99999:7:::
systemd-bus-proxy:!!:17315::::::
systemd-network:!!:17315::::::
dbus:!!:17315::::::
polkitd:!!:17315::::::
colord:!!:17315::::::
abrt:!!:17315::::::
libstoragemgmt:!!:17315::::::
rpc:!!:17315:0:99999:7:::
setroubleshoot:!!:17315::::::
rtkit:!!:17315::::::
rpcuser:!!:17315::::::
nfsnobody:!!:17315::::::
usbmuxd:!!:17315::::::
tss:!!:17315::::::
geoclue:!!:17315::::::
ntp:!!:17315::::::
chrony:!!:17315::::::
mysql:!!:17315::::::
sssd:!!:17315::::::
pulse:!!:17315::::::
gdm:!!:17315::::::
postfix:!!:17315::::::
sshd:!!:17315::::::
tcpdump:!!:17315::::::
gbj:!!:17324:0:99999:7:::
ggg:!!:17325:0:99999:7:::
temence:!!:17328:0:99999:7:::
ghj:!!:17328:0:99999:7:::
  • 锁定用户
1. passwd -l [用户]锁定用户:
[root@centos7-1 ~]# passwd -l temence
锁定用户 temence 的密码 。
passwd: 操作成功
[root@centos7-1 ~]# tail -5 /etc/shadow
tcpdump:!!:17315::::::
gbj:!!:17324:0:99999:7:::
ggg:!!:17325:0:99999:7:::
temence:!!$6$4ppsCRXr$eFNutGrc9zF64UrO3cE2uOL2mDOUpjKmzPfZ7kQorNiU2YUeBvzipNDpVR8uv/0qsL8eroCW8yzGtirGU.dj50:17328:0:99999:7:::
2. usermod -L [用户]锁定用户:
[root@centos7-1 ~]# usermod -L temence
[root@centos7-1 ~]# tail -5 /etc/shadow
tcpdump:!!:17315::::::
gbj:!!:17324:0:99999:7:::
ggg:!!:17325:0:99999:7:::
temence:!$6$4ppsCRXr$eFNutGrc9zF64UrO3cE2uOL2mDOUpjKmzPfZ7kQorNiU2YUeBvzipNDpVR8uv/0qsL8eroCW8yzGtirGU.dj50:17328:0:99999:7:::
  • 解锁用户
1. passwd -u [用户]解锁用户:
[root@centos7-1 ~]# passwd -u temence
解锁用户 temence 的密码。
passwd: 操作成功
2. usermod -U [用户]解锁用户:
[root@centos7-1 ~]# tail -5 /etc/shadow
tcpdump:!!:17315::::::
gbj:!!:17324:0:99999:7:::
ggg:!!:17325:0:99999:7:::
temence:$6$4ppsCRXr$eFNutGrc9zF64UrO3cE2uOL2mDOUpjKmzPfZ7kQorNiU2YUeBvzipNDpVR8uv/0qsL8eroCW8yzGtirGU.dj50:17328:0:99999:7:::
  • 脚本更改密码 

此方法可以用作shell脚本更改用户密码。

1. passwd --stdin更改密码:
[root@centos7-1 ~]# echo "12345" |passwd --stdin ggg
更改用户 ggg 的密码 。
passwd:所有的身份验证令牌已经成功更新。
2. echo -e更改密码:
自动换行
[root@centos7-1 ~]# echo -e "12345
12345" |passwd temence
更改用户 temence 的密码 。
新的 密码:无效的密码: 密码少于 8 个字符
重新输入新的 密码:passwd:所有的身份验证令牌已经成功更新。

3.6 mkpasswd命令

用户生成密码的工具 
[root@Temence ~]# yum install -y expect

-l          指定密码长度,默认位9位
-s          指定特殊符号个数
-d          指定数字个数
-c          指定最少小写字母个数
-C          指定最少大写字母个数
  • 随机生成一个15位密码
@Ssa^p=/35Xob!d

3.7 su命令

su - [用户],切换用户

  • -参数

“-”这个字符的作用是,加上后会初始化当前用户的各种环境变量

  • -c 参数 

在不切换用户的情况下执行某一条命令

[root@centos7-1 ~]# su - -c "touch /home/ggg/2017" ggg
[root@centos7-1 ~]# ll /home/ggg/2017 
-rw-rw-r-- 1 ggg ggg 0 6月  12 22:35 /home/ggg/2017
  • 切换无家目录的用户解决办法:
[root@centos7-1 ~]# useradd -M 2017
[root@centos7-1 ~]# su - 2017
su: 警告:无法更改到 /home/2017 目录: 没有那个文件或目录
-bash-4.2$ 

创建2017用户家目录并更改属主、属组:
[root@centos7-1 ~]# id 2017
uid=1006(2017) gid=1006(2017) 组=1006(2017)
[root@centos7-1 ~]# mkdir /home/2017
[root@centos7-1 ~]# chown 2017:2017 /home/2017/
[root@centos7-1 ~]# su 2017
bash-4.2$ 

此时还是无法正常切换用户,原因在于用户家目录中缺少配置文件,拷贝一份系统模板文件即可:
[root@centos7-1 ~]# cp /etc/skel/.bash* /home/2017/
[root@centos7-1 ~]# ls /home/2017/
[root@centos7-1 ~]# su - 2017
上一次登录:一 6月 12 22:59:04 CST 2017pts/1 上

3.8 sudo 命令

sudo 是允许让普通用户临时执行一些或者全部的root命令的一个工具,如halt,reboot,su等等。这样不仅减少了root用户的登录 和管理时间,同样也提高了安全性。

-s              以目标用户身份运行 shell,完全等于sudo /bin/bash,约等于sudo su ;
-i              以目标用户身份运行一个登录 shell,切换至root同时切换环境变量PATH,约等于sudo su -;
-l              列出目前用户可执行与无法执行的指令;
-u              指定用户作为新的身份,默认为root;
-b              在后台执行命令
  • visudo命令

使用visudo命令去编辑相关的配置文件/etc/sudoers。

  • 添加授权用户
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
ggg     ALL=(root)       NOPASSWD:/usr/bin/ls,/user/bin/mv,/usr/bin/cp

第一部分是用户名,指定哪个用户将拥有sudo的权利
第二部分右边的ALL其实是一个用户名,即,test用户可以切换到哪个用户的身份,如果是ALL就表示所有用户。
第三部分ALL,用来指定test用户可以使用的命令都有哪些,如果是多个命令,用英文逗号隔开。可以在前面添加NOPASSWD 不使用密码。
  • 添加授权用户组
%wheel ALL=(ALL) ALL 以组的形式添加。

## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL
%ggg    ALL=(root)      NOPASSWD: ALL
  • 使用别名添加授权
User_Alias USER_SU = test, test1, test2
Cmnd_Alias SU = /bin/su
USER_SU ALL=(ALL) NOPASSWD: SU
  • 总结:
sudo -i: 为了频繁的执行某些只有超级用户才能执行的权限,而不用每次输入密码,可以使用该命令。提示输入密码时该密码为当前账户的密码。没有时间限制。执行该命令后提示符变为“#”而不是“$”。想退回普通账户时可以执行“exit”或“logout” 。
其实,还有几个类似的用法:
sudo /bin/bash   : 这个命令也会切换到root的bash下,但不能完全拥有root的所有环境变量,比如PATH,可以拥有root用户的权限。这个命令和 sudo -s 是等同的。
sudo -s : 如上
sudo su  : 这个命令,也是登录到了root,但是并没有切换root的环境变量,比如PATH。
sudo su - :  这个命令,纯粹的切换到root环境下,可以这样理解,先是切换到了root身份,然后又以root身份执行了 su - ,这个时候跟使用root登录没有什么区别。这个结果貌似跟sudo -i 的效果是一样的,但是也有不同,sudo 只是临时拥有了root的权限,而su则是使用root账号登录了linux系统。

所以,我们再来总结一下:
sudo su -  约等于  sudo -i 
sudo -s  完全等于  sudo  /bin/bash  约等于 sudo su 
sudo 终究被一个"临时权限的帽子"扣住,不能等价于纯粹的登录到系统里。

3.9 限制Root远程登陆

vim /etc/ssh/sshd_config修改ssh配置文件,禁用root远程登陆

  • 更改ssh配置文件 
    修改配置文件/etc/sshd/sshd_config,在文件中查找PermitRootLogin yes这句话, 修改为PermitRootLogin no

  • 重启sshd服务

[root@centos7-1 ~]# systemctl restart sshd.service

/etc/skel 目录
/etc/skel作用:
1,可以把通知的内容放到skel,让登入的人去看
2,统一初始化心用户的环境变量
3、面试题:出现-bash-4.1$问题原因及解决方法
知识点:

/etc/login.defs配置文件
这个文件是用来定义创建新用户

/etc/default/useradd文件
这个文件是在使用useradd添加用户时,用户信息默认的配置文件,可以使用“useradd -D (参数)-s /bin/bash”

[root@bogon ~]# more /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

 
 
原文地址:https://www.cnblogs.com/liujiacai/p/8146352.html