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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM