天天看點

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号了。

Account expires : Oct 26, 2018

======================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)      

*************** 當你發現自己的才華撐不起野心時,就請安靜下來學習吧!***************