用户和组管理

一、将用户tony01添加到用户组tonyGroup01中而不必离开原有的其他用户组tonyGroup

1.新建用户tony01

[root@xue ~]# useradd tony01
[root@xue ~]# grep tony01 /etc/passwd
tony01:x:1000:1000::/home/tony01:/bin/bash

2.新建组tonyGroup

[root@xue ~]# groupadd tonyGroup
[root@xue ~]# grep tonyGroup /etc/group
tonyGroup:x:1001:

3.将用户tony01添加到用户组tonyGroup中而不必离开原有的其他用户组并验证tonyGroup组包括tony01

[root@xue ~]# usermod -a -G tonyGroup tony01
[root@xue ~]# grep tonyGroup /etc/group
tonyGroup:x:1001:tony01

4.新建用户组tonyGroup01并将tony01加入并验证

[root@xue ~]# groupadd tonyGroup01
[root@xue ~]# usermod -a -G tonyGroup01 tony01
[root@xue ~]# grep tony01 /etc/passwd
tony01:x:1000:1000::/home/tony01:/bin/bash
[root@xue ~]# grep 1000 /etc/group
tony01:x:1000:

5.验证tony01同时包含在里面

[root@xue ~]# grep tonyGroup /etc/group
tonyGroup:x:1001:tony01
tonyGroup01:x:1002:tony01

6.如果将一个用户从每个组中删除,要保证tonyGroup不是tony01的主要组

[root@xue ~]# gpasswd -d tony01 tonyGroup
Removing user tony01 from group tonyGroup

二、将用户tony02的主要用户组改为tonyGroup

1.新建用户tony02并设置

[root@xue ~]# useradd tony02
[root@xue ~]# usermod -g tonyGroup tony02
[root@xue ~]# grep tony02 /etc/passwd
tony02:x:1001:1001::/home/tony02:/bin/bash
[root@xue ~]# grep tonyGroup /etc/group
tonyGroup:x:1001:
tonyGroup01:x:1002:tony01

三、增加用户tony03到附属组

1.新建用户tony03

[root@xue ~]# useradd tony03
[root@xue ~]# grep tony03 /etc/passwd
tony03:x:1002:1004::/home/tony03:/bin/bash

2.查看tonyGroup3这个组是否存在,不存在则创建

[root@xue ~]# grep tonyGroup3 /etc/group
[root@xue ~]# groupadd tonyGroup3
[root@xue ~]# usermod -G tonyGroup3 tony03
[root@xue ~]# id tony03
uid=1002(tony03) gid=1004(tony03) groups=1004(tony03),1005(tonyGroup3)
[root@xue ~]# useradd tiara
[root@xue ~]# groupadd testgroup
[root@xue ~]# gpasswd -a tiara testgroup
Adding user tiara to group testgroup

whoami用于获取当前用户名

打印当前所有登录系统用户的信息

[root@xue ~]# useradd -u 510 -g 1000 -d /home/user1 -s /bin/bash -p 123456 -f -1 user1
[root@xue ~]# tail -1 /etc/passwd
user1:x:510:1000::/home/user1:/bin/bash

设置用户账户口令

[root@xue ~]# chage -m 6 -M 60 -W 5 user1
[root@xue ~]# chage -l user1
Last password change                    : Dec 15, 2018
Password expires                    : Feb 13, 2019
Password inactive                   : never
Account expires                     : never
Minimum number of days between password change      : 6
Maximum number of days between password change      : 60
Number of days of warning before password expires   : 5  

用户账户的维护

修改用户账户

[root@xue ~]# usermod -d /var/user1 -s /bin/tcsh user1
[root@xue ~]# grep user1 /etc/passwd
user1:x:510:1000::/var/user1:/bin/tcsh

禁用和恢复用户账户

[root@xue ~]# passwd -l user1
Locking password for user user1.
passwd: Success
[root@xue ~]# grep user1 /etc/shadow
user1:123456:17880:6:60:5:::
[root@xue ~]# passwd -u user1
Unlocking password for user user1.
passwd: Success

你可能感兴趣的:(用户和组管理)