在CentOS下使用檔案大量新增使用者

來源:互聯網
上載者:User

標籤: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] ~]# 


  1. 建立使用者檔案,檔案格式需和/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下使用檔案大量新增使用者

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.