分类: LINUX
2018-08-10 18:25:03
用户管理-002
作者:xzlab
邮箱:xzlab1234@163.com
n常用命令
id
查看用户id信息
[root@host1 ~]# id jim
uid=1004(jim) gid=1004(jim) groups=1004(jim)
su
切换用户
[root@host1 ~]# su jim
[jim@host1 root]$
切换用户(切换家目录和环境变量)
[root@host1 ~]# su - jim
Last login: Fri Aug 10 15:31:29 CST 2018 on pts/0
[jim@host1 ~]$
useradd
添加用户、指定uid
[root@host1 ~]# useradd -u 2000 kevin
[root@host1 ~]# cat /etc/passwd | grep kevin
kevin:x:2000:2000::/home/kevin:/bin/bash
添加用户、指定辅助组(G,会新建同名组)
[root@host1 ~]# useradd -G team1 lily
[root@host1 ~]# id lily
uid=2001(lily) gid=2002(lily) groups=2002(lily),2001(team1)
添加用户、指定主要组(g,不会新建同名组)
[root@host1 ~]# useradd -g team1 lucy
[root@host1 ~]# id lucy
uid=2002(lucy) gid=2001(team1) groups=2001(team1)
添加用户、指定家目录
[root@host1 ~]# useradd -d /aaa john
[root@host1 ~]# ls /aaa -al
total 16
drwx------. 3 john john 74 Aug 10 15:36 .
dr-xr-xr-x. 18 root root 4096 Aug 10 15:36 ..
-rw-r--r--. 1 john john 18 Jan 11 2015 .bash_logout
-rw-r--r--. 1 john john 193 Jan 11 2015 .bash_profile
-rw-r--r--. 1 john john 231 Jan 11 2015 .bashrc
drwxr-xr-x. 4 john john 37 Apr 4 13:01 .mozilla
添加用户、指定登陆shell
[root@host1 ~]# useradd -s /sbin/nologin jack
[root@host1 ~]# cat /etc/passwd | grep jack
jack:x:1006:1006::/home/jack:/sbin/nologin
添加用户、指定用户有效期
[root@host1 ~]# useradd -e 2018-08-11 stone
[root@host1 ~]# chage -l stone
Last password change : Aug 10, 2018
Password expires : never
Password inactive : never
Account expires : Aug 11, 2018
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
passwd
修改用户密码(交互模式)
[root@host1 ~]# passwd lily
Changing password for user lily.
New password:
BAD PASSWORD: The password is shorter than 7 characters
Retype new password:
passwd: all authentication tokens updated successfully.
修改用户密码(非交互模式)
[root@host1 ~]# echo lab123 | passwd --stdin lucy
Changing password for user lucy.
passwd: all authentication tokens updated successfully.
查看密码状态(查看是否被锁定)
[root@host1 ~]# passwd -S lily
lily LK 2018-08-10 0 99999 7 -1 (Password locked.)
userdel
删除用户,并删除用户家目录
[root@host1 ~]# userdel -r john
[root@host1 ~]# ls /home -al
total 20
drwxr-xr-x. 6 jim jim 49 Aug 9 21:55 .
dr-xr-xr-x. 17 jim jim 4096 Apr 4 13:13 ..
drwx------. 5 boss boss 4096 Aug 5 11:00 boss
drwx------. 5 1004 1004 4096 Aug 9 21:50 jim
drwx------. 5 tom tom 4096 Aug 5 22:17 tom
drwx------. 15 xzlab xzlab 4096 Jul 16 21:41 xzlab
usermod
修改用户(锁定用户)
[root@host1 ~]# usermod -L lily
[root@host1 ~]# chage -l lily
Last password change : Aug 10, 2018
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
[root@host1 ~]# passwd -S lily
lily LK 2018-08-10 0 99999 7 -1 (Password locked.)
修改用户(取消锁定)
[root@host1 ~]# usermod -U lily
[root@host1 ~]# passwd -S lily
lily PS 2018-08-10 0 99999 7 -1 (Password set, SHA512 crypt.)
groupadd
[root@host1 ~]# groupadd -g 3000 team2
[root@host1 ~]# cat /etc/group | grep team2
team2:x:3000:
groupdel
[root@host1 ~]# groupdel team2
groupmod
修改组id
[root@host1 ~]# groupmod -g 4000 team2
修改组名
[root@host1 ~]# groupmod -n group2 team2
[root@host1 ~]# cat /etc/group | grep group2
group2:x:4000:
修改组密码
[root@host1 ~]# groupmod -p lab123 group2
gpasswd
设定组成员(注意,用该命令会把之前的成员关系全部更新,如果该组已经有成员关系,注意风险)
[root@host1 ~]# gpasswd -M kevin,stone,jack team1
chage
查看帮助信息
[root@host1 ~]# chage -h
Usage: chage [options] LOGIN
Options:
-d, --lastday LAST_DAY set date of last password change to LAST_DAY
-E, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
-h, --help display this help message and exit
-I, --inactive INACTIVE set password inactive after expiration
to INACTIVE
-l, --list show account aging information
-m, --mindays MIN_DAYS set minimum number of days before password
change to MIN_DAYS
-M, --maxdays MAX_DAYS set maximim number of days before password
change to MAX_DAYS
-R, --root CHROOT_DIR directory to chroot into
-W, --warndays WARN_DAYS set expiration warning days to WARN_DAYS
查看账号信息
[root@host1 ~]# chage -l jim
Last password change : Aug 09, 2018
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
下次登陆强制修改密码
[root@host1 ~]# chage -d 0 tom
[root@host1 ~]# chage -l tom
Last password change : password must be changed
Password expires : password must be changed
Password inactive : password must be changed
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
who
查看当前登录用户
[root@host1 ~]# who
(unknown) :0 2018-08-10 15:28 (:0)
root pts/0 2018-08-10 15:29 (192.168.1.100)
w
查看当前登录用户详细信息
[root@host1 ~]# w
17:19:38 up 1:40, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.1.100 15:29 53:46 0.81s 0.81s -bash
root pts/1 192.168.1.100 17:10 2.00s 0.17s 0.02s w