標籤:centos 大量新增使用者
在CentOS添加使用者的方法是多樣的,例如,使用useradd、passwd單個添加使用者;使用指令碼大量新增用 戶;使用檔案大量匯入使用者。本節介紹的是使用檔案大量匯入使用者的方法。
環境介紹:
[[email protected] ~]# uname -r
3.10.0-327.el7.x86_64
[[email protected] ~]# cat /etc/centos-release
CentOS Linux release 7.2.1511 (Core)
[[email protected] ~]#
建立使用者檔案,檔案格式需和/etc/passwd的格式一致。
[[email protected] ~]# cat useradds.txt
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1003:1003::/home/user3:/bin/bash
user4:x:1004:1004::/home/user4:/bin/bash
# /etc/passwd檔案格式說明: username:passwd:UID:GID:comment:home-directory:shell
2. 使用newusers匯入使用者,並驗證。
[[email protected] ~]# newusers useradds.txt
[[email protected] ~]# tail -5 /etc/passwd
tcpdump:x:72:72::/:/sbin/nologin
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1003:1003::/home/user3:/bin/bash
user4:x:1004:1004::/home/user4:/bin/bash
[[email protected] ~]#
在/etc/passwd檔案中已經顯示使用者被成功匯入。
[[email protected] ~]# ll /home
total 0
drwx------ 2 user1 user1 6 May 24 11:09 user1
drwx------ 2 user2 user2 6 May 24 11:09 user2
drwx------ 2 user3 user3 6 May 24 11:09 user3
drwx------ 2 user4 user4 6 May 24 11:09 user4
[[email protected] ~]#
查看/home目錄已經建立了使用者的家目錄。
3.使用chpasswd 匯入使用者密碼檔案。密碼檔案格式為 username:passwd
[[email protected] ~]# cat passwd.txt
user1:qwe123
user2:qwe123
user3:qwe123
user4:qwe123
[[email protected] ~]#
如上所示,先建立使用者密碼檔案。
[[email protected] ~]# chpasswd < passwd.txt
[[email protected] ~]#
使用chpasswd 匯入密碼檔案。由於chpasswd命令只能從standard input讀入,因此需要使用重新導向,用檔案內容來代替鍵盤輸入。
[[email protected] ~]# tail -5 /etc/shadow
tcpdump:!!:16930::::::
user1:$6$j9xvrnk7u$RkUW5pp5eZ6uZHE80QuF.OoudakhW0m5Bi2y42qhY/wXwyXt6qSrekI4qbc6QVlT9P2XdXc8CzpyZ0nNKdHnE1:16945:0:99999:7:::
user2:$6$Z0e6nuRnU5/$KQvkQCG2p2wkpbg8JJN4WDc6hPjewtjAV8JO3Zue6VgYIeR34YNMOXDfVz5QylB5VMMrnk7nbAOTKFLjrVpnT1:16945:0:99999:7:::
user3:$6$MDMIBLT4I$Cw/F/ZPeCrnfRYyxhMR/387tNxFCrSJtwATb5V9l1uwl0hn/rt3PRK.ds0dHIR2YM0P469Z8PGBEdvCkXpu7q/:16945:0:99999:7:::
user4:$6$Wf6Yg/IDssC/$aQCdVqJ9tkX7OzIjKWyGKe1De2rZ0.JGbyrIYwT6pUXACiDKcBAARTghC1YgHONOfA6B2b3erZ70d5TKDJQ0g1:16945:0:99999:7:::
[[email protected] ~]#
查看/etc/shadow檔案,使用者密碼匯入成功.
#/etc/shadow檔案格式說明:
username:passwd:change-date:minimum-password-lifetime:maximum-password-lifetime:warning:expire:inactive:reserve
4. 使用chown 更改複製到使用者家目錄以及家目錄本身的所有者和所屬組。
[[email protected] ~]# chown -R user4:user4 /home/user4
[[email protected] ~]# ll -d /home/user4
drwxr-xr-x. 3 user4 user4 74 May 9 16:50 /home/user4
5. 使用cp -a /etc/skel/. /home/user4 複製使用者環境設定檔。
[[email protected] user1]# ls -a /home/user4
. ..
[[email protected] user1]# cp -a /etc/skel/. /home/user4
[[email protected] user1]# ls -a /home/user4
. .. .bash_logout .bash_profile .bashrc .mozilla
預設情況下,使用文本匯入方式建立的使用者在家目錄下沒有環境設定檔,需要手工從/etc/skel複製到家目錄下。
6. 驗證使用者是否可以成功登入系統。
[[email protected] ~]$
[[email protected] ~]$ touch file1
[[email protected] ~]$ ls -l
total 0
-rw-rw-r-- 1 user4 user4 0 May 24 11:47 file1
[[email protected] ~]$
如所示,使用者已可成功登入。
本文出自 “sunhairuirs” 部落格,請務必保留此出處http://sunhairuirs.blog.51cto.com/745792/1782435
在CentOS下使用檔案大量新增使用者