#useradd命令:建立用户帐号和创建用户的起始目录,使用权限是超级用户

[root@VM_168_102_centos ~]# useradd test
[root@VM_168_102_centos ~]# id test
uid=502(test) gid=502(test) groups=502(test)
[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
test:x:502:502::/home/test:/bin/bash

useradd –u UID:创建用户时直接指定UID

[root@VM_168_102_centos ~]# useradd -u 888 test_1
[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
test_1:x:888:888::/home/test_1:/bin/bash

useradd –g GID:创建用户时直接指定GID,GID要事先存在

[root@VM_168_102_centos ~]# useradd -g 500 test_2
[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
test_2:x:889:500::/home/test_2:/bin/bash
[root@VM_168_102_centos ~]# id test_2
uid=889(test_2) gid=500(wanghan) groups=500(wanghan)
[root@VM_168_102_centos ~]# useradd -g 777 test_3
useradd: group '777' does not exist

useradd –G GID:创建用户时直接指定额外组,但组要事先存在

[root@VM_168_102_centos ~]# useradd -G 500 test_3
[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
test_3:x:890:890::/home/test_3:/bin/bash
[root@VM_168_102_centos ~]# id test_3
uid=890(test_3) gid=890(test_3) groups=890(test_3),500(wanghan)

useradd –d:创建用户时指定用户主目录,如果此目录不存在,则同时使用-m选项,可以创建主目录

[root@VM_168_102_centos ~]# useradd -d /home/qidian -m test_6      
[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
test_6:x:893:893::/home/qidian:/bin/bash
[root@VM_168_102_centos ~]# ls /home
abc  ceshi  openstack  qidian  test  test_1  test_2  test_3  wanghan

说明: –m:创建用户时,强制给用户创建主目录

useradd –M:创建用户,但不创建家目录

[root@VM_168_102_centos ~]# ls /home
abc  ceshi  openstack  qidian  test  test_1  test_2  test_3  wanghan
[root@VM_168_102_centos ~]# useradd -M test_7
[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
test_7:x:895:895::/home/test_7:/bin/bash
[root@VM_168_102_centos ~]# ls /home
abc  ceshi  openstack  qidian  test  test_1  test_2  test_3  wanghan

useradd -c:创建用户指定一段注释性描述,在/etv/passwd中查看

[root@VM_168_102_centos ~]# useradd -c wanghan test_9                                                 
[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
test_9:x:897:897:wanghan:/home/test_9:/bin/bash

useradd –s:创建用户时指定默认shell,应该指定/etc/shells文件出现的shell

[root@VM_168_102_centos ~]# useradd -s tcsh test_10
useradd: invalid shell 'tcsh'
[root@VM_168_102_centos ~]# useradd -s /bin/tcsh test_10
[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
test_10:x:898:898::/home/test_10:/bin/tcsh

#groupadd命令:创建一个新组

[root@VM_168_102_centos ~]# groupadd hp
[root@VM_168_102_centos ~]# tail -n 1 /etc/group
hp:x:2001:

groupadd –g GID:创建新组并指定GID

[root@VM_168_102_centos ~]# groupadd -g 2000 wudi
[root@VM_168_102_centos ~]# tail -n 1 /etc/group
wudi:x:2000:
[root@VM_168_102_centos ~]#

#userdel命令:删除用户,默认保留家目录

[root@VM_168_102_centos ~]# userdel test_9
[root@VM_168_102_centos ~]# tail /etc/passwd
test_1:x:888:888::/home/test_1:/bin/bash
test_2:x:889:500::/home/test_2:/bin/bash
test_3:x:890:890::/home/test_3:/bin/bash
test_4:x:891:891::/home/abc:/bin/bash
openstack:x:892:892::/home/openstack:/bin/bash
test_6:x:893:893::/home/qidian:/bin/bash
wanghan11:x:894:894::/tmp/wanghan:/bin/bash
test_7:x:895:895::/home/test_7:/bin/bash
test_8:x:896:896::/home/test_8:/bin/bash
test_10:x:898:898::/home/test_10:/bin/tcsh
[root@VM_168_102_centos ~]# ls /home
abc  ceshi  openstack  qidian  test  test_1  test_10  test_2  test_3  test_8  test_9  wanghan

userdel -r:删除用户同时删除其家目录

[root@VM_168_102_centos ~]# userdel -r test_8
[root@VM_168_102_centos ~]# tail /etc/passwd
test:x:502:502::/home/test:/bin/bash
test_1:x:888:888::/home/test_1:/bin/bash
test_2:x:889:500::/home/test_2:/bin/bash
test_3:x:890:890::/home/test_3:/bin/bash
test_4:x:891:891::/home/abc:/bin/bash
openstack:x:892:892::/home/openstack:/bin/bash
test_6:x:893:893::/home/qidian:/bin/bash
wanghan11:x:894:894::/tmp/wanghan:/bin/bash
test_7:x:895:895::/home/test_7:/bin/bash
test_10:x:898:898::/home/test_10:/bin/tcsh
[root@VM_168_102_centos ~]# ls /home
abc  ceshi  openstack  qidian  test  test_1  test_10  test_2  test_3  test_9  wanghan

#groupdel命令:删除群组,若组中仍包括某些用户,则必须先删除这些用户后,方能删除群组。

[root@VM_168_102_centos ~]# groupdel user1
groupdel: cannot remove the primary group of user 'user1'
[root@VM_168_102_centos ~]# groupdel wudi
[root@VM_168_102_centos ~]# cat /etc/group | grep "wudi"
[root@VM_168_102_centos ~]#

#passwd命令:设定用户密码

管理员修改自身密码:

[root@VM_168_102_centos ~]# passwd
Changing password for user root.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

管理员修改其他用户密码:

[root@VM_168_102_centos ~]# passwd wanghan
Changing password for user wanghan.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

用户修改自身密码:

[wanghan@VM_168_102_centos rott]$ passwd
Changing password for user wanghan. 
Changing password for wanghan.
(current) UNIX password:    //输入当前密码
New password:           
Retype new password: 
passwd: all authentication tokens updated successfully.

passwd –l:锁定用户密码,用户不能修改密码

[root@VM_168_102_centos ~]# passwd -l wanghan
Locking password for user wanghan.
passwd: Success
[root@VM_168_102_centos ~]# su wanghan
[wanghan@VM_168_102_centos rott]$ passwd
Changing password for user wanghan.
Changing password for wanghan.
(current) UNIX password: 
passwd: Authentication token manipulation error

passwd –u:解除用户密码锁定

[root@VM_168_102_centos ~]# passwd -u wanghan
Unlocking password for user wanghan.
passwd: Success
[root@VM_168_102_centos ~]# su wanghan
[wanghan@VM_168_102_centos rott]$ passwd
Changing password for user wanghan.
Changing password for wanghan.
(current) UNIX password: 
New password:

passwd –S:显示用户密码简要信息

[root@VM_168_102_centos ~]# passwd -S wanghan
wanghan PS 2014-08-12 0 99999 7 -1 (Password set, SHA512 crypt.)

#usermod命令:用来修改用户帐号的各项设定

usermod –u UID:修改用户UID

[root@VM_168_102_centos ~]# id openstack
uid=892(openstack) gid=892(openstack) groups=892(openstack)
[root@VM_168_102_centos ~]# usermod -u 588 openstack
[root@VM_168_102_centos ~]# id openstack
uid=588(openstack) gid=892(openstack) groups=892(openstack)

usermod –g GID:修改用户GID

[root@VM_168_102_centos ~]# id wanghan
uid=500(wanghan) gid=2000(wudi) groups=2000(wudi)
[root@VM_168_102_centos ~]# usermod -g 2001 wanghan
[root@VM_168_102_centos ~]# id wanghan
uid=500(wanghan) gid=2001(hp) groups=2001(hp)

usermod –G GID:修改用户附加组,默认覆盖原有附加组,同时使用-a选项则是再额外添加

[root@VM_168_102_centos ~]# id user1
uid=899(user1) gid=899(user1) groups=899(user1),2000(wudi)
[root@VM_168_102_centos ~]# usermod -G 2001 user1
[root@VM_168_102_centos ~]# id user1
uid=899(user1) gid=899(user1) groups=899(user1),2001(hp)
[root@VM_168_102_centos ~]# usermod -a -G 2000 user1
[root@VM_168_102_centos ~]# id user1
uid=899(user1) gid=899(user1) groups=899(user1),2000(wudi),2001(hp)

usermod –c:修改用户帐号备注信息

[root@VM_168_102_centos ~]# useradd -c hello user2
[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
user2:x:900:900:hello:/home/user2:/bin/bash
[root@VM_168_102_centos ~]# usermod -c test user2
[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
user2:x:900:900:test:/home/user2:/bin/bash

usermod -d:修改用户家目录,默认不会迁移家目录,同时使用-m则可迁移

[root@VM_168_102_centos ~]# usermod -d /tmp/wanghan user1  
[root@VM_168_102_centos ~]# tail /etc/passwd
test_2:x:889:500::/home/test_2:/bin/bash
test_3:x:890:890::/home/test_3:/bin/bash
test_4:x:891:891::/home/abc:/bin/bash
openstack:x:588:892::/home/openstack:/bin/bash
test_6:x:893:893::/home/qidian:/bin/bash
wanghan11:x:894:894::/tmp/wanghan:/bin/bash
test_7:x:895:895::/home/test_7:/bin/bash
test_10:x:898:898::/home/test_10:/bin/tcsh
user1:x:899:899::/tmp/wanghan:/bin/bash
user2:x:900:900:test:/home/user2:/bin/bash
[root@VM_168_102_centos ~]# usermod -d /tmp/user2/ -m user2
[root@VM_168_102_centos ~]# tail /etc/passwd
test_2:x:889:500::/home/test_2:/bin/bash
test_3:x:890:890::/home/test_3:/bin/bash
test_4:x:891:891::/home/abc:/bin/bash
openstack:x:588:892::/home/openstack:/bin/bash
test_6:x:893:893::/home/qidian:/bin/bash
wanghan11:x:894:894::/tmp/wanghan:/bin/bash
test_7:x:895:895::/home/test_7:/bin/bash
test_10:x:898:898::/home/test_10:/bin/tcsh
user1:x:899:899::/tmp/wanghan:/bin/bash
user2:x:900:900:test:/tmp/user2/:/bin/bash

usermod –s:修改用户shell

[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
user2:x:900:900:test:/tmp/user2/:/bin/bash
[root@VM_168_102_centos ~]# usermod -s /bin/tcsh user2
[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
user2:x:900:900:test:/tmp/user2/:/bin/tcsh

usermod -l:修改用户帐号名称

[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
user2:x:900:900:test:/tmp/user2/:/bin/tcsh
[root@VM_168_102_centos ~]# usermod -l user3 user2
[root@VM_168_102_centos ~]# tail -n 1 /etc/passwd
user3:x:900:900:test:/tmp/user2/:/bin/tcsh

usermod –L:锁定用户帐号密码,使其无效

[root@VM_168_102_centos ~]# usermod -L wanghan
[root@VM_168_102_centos ~]# su wanghan
[wanghan@VM_168_102_centos /root]$ passwd
Changing password for user wanghan.
Changing password for wanghan.
(current) UNIX password: 
passwd: Authentication token manipulation error

usermod –U:解除锁定用户帐号密码

[root@VM_168_102_centos ~]# usermod -U wanghan
[root@VM_168_102_centos ~]# su wanghan
[wanghan@VM_168_102_centos /root]$ passwd
Changing password for user wanghan.
Changing password for wanghan.
(current) UNIX password: 
New password:

#groupmod命令:修改群属性定义

groupmod –g GID:修改用户GID

[root@VM_168_102_centos ~]# cat /etc/group | grep "hp"
hp:x:2004:user1
[root@VM_168_102_centos ~]# groupmod -g 2008 hp
[root@VM_168_102_centos ~]# cat /etc/group | grep "hp"
hp:x:2008:user1

groupmod -n:修改群组名称

[root@VM_168_102_centos ~]# cat /etc/group | grep "2008"
hx:x:2008:user1
[root@VM_168_102_centos ~]# groupmod -n hp hx
[root@VM_168_102_centos ~]# cat /etc/group | grep "2008"
hp:x:2008:user1

#chage命令:修改帐号和密码的有效期限

chage –l:查看帐号密码的有效期限

Last password change                    : Aug 13, 2014  //最近一次修改密码
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  //用户密码到期前,提前收到警告信息的天数

chage –m:设定密码可更改的最小天数

[root@VM_168_102_centos ~]# chage -m 5 wanghan
[root@VM_168_102_centos ~]# chage -l wanghan
Last password change                    : Aug 13, 2014
Password expires                    : Aug 23, 2014
Password inactive                    : never
Account expires                        : never
Minimum number of days between password change        : 5

chage –M:设定密码有效的最大天数

[root@VM_168_102_centos ~]# chage -M 10 wanghan
[root@VM_168_102_centos ~]# chage -l wanghan
Last password change                    : Aug 13, 2014
Password expires                    : Aug 23, 2014
Password inactive                    : never
Account expires                        : never
Minimum number of days between password change        : 5
Maximum number of days between password change        : 10
Number of days of warning before password expires    : 7

chage -E:设定帐号的到期日期,0是立即过期,-1永不过期

[root@VM_168_102_centos ~]# chage -E 0 wanghan
[root@VM_168_102_centos ~]# chage -l wanghan
Last password change                    : Aug 13, 2014
Password expires                    : Aug 23, 2014
Password inactive                    : never
Account expires                        : Jan 01, 1970  //帐号过期日期
[root@VM_168_102_centos ~]# chage -E -1 wanghan
[root@VM_168_102_centos ~]# chage -l wanghan
Last password change                    : Aug 13, 2014
Password expires                    : Aug 23, 2014
Password inactive                    : never
Account expires                        : never  //帐号永不过期

chage –W:设定用户密码到期前,提前收到警告信息的天数

[root@VM_168_102_centos ~]# chage -W 5 wanghan
[root@VM_168_102_centos ~]# chage -l wanghan
Last password change                    : Aug 13, 2014
Password expires                    : Aug 23, 2014
Password inactive                    : never
Account expires                        : never
Minimum number of days between password change        : 5
Maximum number of days between password change        : 10
Number of days of warning before password expires    : 5

#id命令:显示用户ID及所属群组ID

[root@VM_168_102_centos ~]# id
uid=0(root) gid=500(wanghan) groups=500(wanghan)
[root@VM_168_102_centos ~]# id wanghan
uid=500(wanghan) gid=2008(hp) groups=2008(hp)

id -u:显示用户UID,跟-n一起使用则显示用户名

[root@VM_168_102_centos ~]# id
uid=0(root) gid=0(root) groups=0(root)
[root@VM_168_102_centos ~]# id -u
0
[root@VM_168_102_centos ~]# id -u -n 
root

id -g:显示基本组ID,跟-n一起使用则显示其基本组名

[root@VM_168_102_centos ~]# id wanghan
uid=500(wanghan) gid=2008(hp) groups=2008(hp)
[root@VM_168_102_centos ~]# id -g wanghan
2008
[root@VM_168_102_centos ~]# id -g -n wanghan
hp

id –G:显示所有组ID,跟-n一起使用则显示所有组名

[root@VM_168_102_centos ~]# id user1
uid=899(user1) gid=899(user1) groups=899(user1),2008(hp)
[root@VM_168_102_centos ~]# id -G user1
899 2008
[root@VM_168_102_centos ~]# id -G -n  user1
user1 hp