- 批量添加使用者
- 系統指令法
vim newusers.s 格式同/etc/passwd,:為分割符号
test1:x:1003:1003::/home/test1:/bin/bash
username | password | UID | GID(private group) | Fullname | home dir | shell |
test1 | x | 1003 | 1003 | /home/test1 | /bin/bash |
執行指令
newusers newusers.s 不複制home目錄下的模版内容(/etc/skel)
- shell腳本法
首先建立一個使用者賬号檔案,把使用者名寫在這個文本檔案裡。注意:一個使用者名占一行。
vim users.txt
cerana1:13888298736
cerana2:13888298737
cerana3:13888298738
cerana4:13888298739
cerana5:13888298740
建立shell 腳本,然後用for循環把建立的使用者名周遊出來:
#!/bin/bash
#把shell腳本和賬号檔案建立在同一個目錄下
for line in `cat users.txt`
do
username=$(echo $line | awk -F: '{print $1}')
password=$(echo $line | awk -F: '{print $2}')
useradd $username
echo "User $username
#passwd --stdin表示不互動,直接輸入密碼
echo $password | passwd --stdin $username
#強制使用者下次登陸後修改密碼
chage -d 0 $username
#設定賬号30天後過期
chage -M 30 $username
done
echo "Finish!"
有加就有删
#!/bin/bash
for line in `cat users.txt`
do
username=$(echo $line | awk -F: '{print $1}')
userdel -r $username
echo "User $username
done
echo "Finish!"
列出本地所有一般使用者的賬号
getent passwd | awk -F: '$3>=1000 {print $0}'
getent group | awk -F: '$3>=1000 {print $0}'
- UID&GID
使用者 | UID or GID |
root | |
系統使用者 | 1 ~ 499 |
擴充的系統使用者 | 500 ~ 999 |
- 注意事項
- /etc/shadow 中密碼字段為!!,表示該使用者還沒有設定密碼。
- userdel 一般不删除private group,要删除需要借助groupdel
- 鎖定和解鎖本地使用者
鎖定使用者
usermod -L username 或者 passwd -l username
解鎖使用者
usermod -U username 或者 passwd -u username
删除使用者密碼
passwd -d username
-
Password Aging Policy
~ By default, passwords do not expire
~ Modify default expiration settings in /etc/login.defs
~ To modify password aging for existing users, use the chage command
chage -l user 檢視user的賬号日期資訊
chage -d 0 user 強制使用者在下次登陸時修改密碼
chage -m 1 user 定義密碼最小更改周期(機關:天)
chage -M 7 user 定義密碼最大更改周期(99999表示永不過期)
chage -I 3 user 密碼過期3天後,如果還未更改密碼,賬号将被鎖定。
chage -E 2018-08-21 user 設定賬号到期日