LInux下设置账号有效时间 以及 修改用户名(同时修改用户组名和家目录)

在linux系统中,默认创建的用户的有效期限都是永久的,但有时候,我们需要对某些用户的有效期限做个限定!
比如:公司给客户开的ftp账号,用于客户下载新闻稿件的。这个账号是有时间限制的,因为是付费的。合同到期了,这个账号就要求停用。

废话不多说,直接说下操作记录:

需求:
创建lzwb账号,用于下载/home/hqsb里面的新闻稿件,这个账号的合同到期时间是2018年10月26号

1)创建账号lzwb
[root@dev ~]# useradd lzwb -d /home/hqsb -s /sbin/nologin

2)默认情况下,这个账号建立后,有效期限是永久的。注意下面命令结果:
Last password change: 表示账号创建时的时间
Account expires: 表示账号到期时间
命令格式:chage -l username 查看用户的到期时间情况
[root@dev ~]# chage -l lzwb
Last password change : Oct 26, 2016
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

3)按照需求,修改账号的到期时间
命令格式:usermod -e "到期时间" username 修改系统用户的时间
[root@dev ~]# usermod -e "Oct 26,2018" lzwb

再次查看,发现lzwb的有效时间截止到2018年的10月26号了。
[root@dev ~]# chage -l lzwb
Last password change : Oct 26, 2016
Password expires : never
Password inactive : never
Account expires : Oct 26, 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

======================Linux 下修改用户名(同时修改用户组名和家目录)=====================

1) 修改用户名
# usermod -l new_username old_username

比如将kevin用户名修改为shibo
[root@localhost ~]# useradd kevin
[root@localhost ~]# cat /etc/passwd|grep kevin
kevin:x:501:502::/home/kevin:/bin/bash

[root@localhost ~]# usermod -l shibo kevin

查看修改后的用户名
[root@localhost ~]# cat /etc/passwd|grep shibo
shibo:x:501:502::/home/kevin:/bin/bash
[root@localhost ~]# cat /etc/passwd|grep kevin
shibo:x:501:502::/home/kevin:/bin/bash
[root@localhost ~]# su - kevin
su: user kevin does not exist
[root@localhost ~]# su - shibo
[shibo@localhost ~]$ 

发现上面修改, 只会更改用户名,而其他的东西,比如用户组,家目录,UID 等都保持不变。

特别注意:
如果修改的用户名在登录状态中, 需要从要改名的帐号中登出并杀掉该用户的所有进程,要杀掉该用户的所有进程可以执行下面命令:
[root@localhost ~]# pkill -u kevin
[root@localhost ~]# pkill -9 -u kevin

2) 修改用户家目录
同时更改家目录,我们需要在执行 usermod 命令的同时加上 -d 选项

如上将kevin用户修改为shibo后, shibo用户的家目录还是之前的/home/kevin,
现在要将shibo用户的家目录由/home/kevin 改为 /data/shibo
[root@localhost ~]# cat /etc/passwd|grep shibo
shibo:x:501:502::/home/kevin:/bin/bash

[root@localhost ~]# ls /data/shibo
ls: cannot access /data/shibo: No such file or directory

[root@localhost ~]# usermod -d /data/shibo shibo

[root@localhost ~]# cat /etc/passwd|grep shibo  
shibo:x:501:502::/data/shibo:/bin/bash

3) 更改用户 UID 
如上将kevin用户修改为shibo后, shibo用户的uid和gid都没有改变
现在想要将shibo用户的UID改为 1000 
[root@localhost ~]# cat /etc/passwd|grep shibo
shibo:x:501:502::/data/shibo:/bin/bash

[root@localhost ~]# usermod -u 1000 shibo

[root@localhost ~]# cat /etc/passwd|grep shibo
shibo:x:1000:502::/data/shibo:/bin/bash

4) 修改用户组名
现在要把shibo用户的用户组由kevin改为shibo, 这就要用到groupadd命令
[root@localhost ~]# cat /etc/group|grep kevin 
kevin:x:502:
[root@localhost ~]# cat /etc/group|grep shibo 
[root@localhost ~]#
[root@localhost ~]# cat /etc/passwd|grep shibo
shibo:x:1000:502::/data/shibo:/bin/bash

[root@localhost ~]# groupmod -n shibo kevin

[root@localhost ~]# cat /etc/group|grep shibo 
shibo:x:502:
[root@localhost ~]# cat /etc/group|grep kevin
[root@localhost ~]# 
[root@localhost ~]# cat /etc/passwd|grep shibo
shibo:x:1000:502::/data/shibo:/bin/bash

这时候shibo用户的群组已经是shibo了, 现在要把shibo用户的gid由502 改为 2000
[root@localhost ~]# cat /etc/group|grep shibo 
shibo:x:502:
[root@localhost ~]# cat /etc/passwd|grep shibo
shibo:x:1000:502::/data/shibo:/bin/bash

[root@localhost ~]# groupmod -g 2000 shibo

[root@localhost ~]# cat /etc/group|grep shibo
shibo:x:2000:
[root@localhost ~]# cat /etc/passwd|grep shibo
shibo:x:1000:2000::/data/shibo:/bin/bash

[root@localhost ~]# id shibo
uid=1000(shibo) gid=2000(shibo) groups=2000(shibo)
posted @ 2016-10-26 19:19  散尽浮华  阅读(13234)  评论(0编辑  收藏  举报