要在 Linux 下大量建立 FTP 使用者,用指令一個一個 adduser 未免太慢。系統內建了兩個專門處理批量任務的工具:newusers 與 chpasswd。
先釐清一個前提:多數 FTP 伺服器(如 vsftpd、proftpd)其實是直接使用系統帳號做認證的,所以「批量建 FTP 帳號」在本質上就是批量建系統帳號。
每一列按照密碼檔案的格式書寫,善用編輯器的複製貼上功能大量產生。例如建立一個名為 new_alist 的檔案:
stu001::600:600:stu:/home/stu001:/bin/bash stu002::601:601:stu:/home/stu002:/bin/bash stu003::602:602:stu:/home/stu003:/bin/bash stu004::603:603:stu:/home/stu004:/bin/bash stu005::604:604:stu:/home/stu005:/bin/bash stu006::605:605:stu:/home/stu006:/bin/bash
欄位依序為:帳號、密碼(留空)、UID、GID、全名、家目錄、登入 shell。
newusers < new_alist pwunconv
pwunconv 的作用是關閉 shadow 密碼機制,讓下一步能直接寫入密碼欄。
建立 new_plist,格式為「帳號:密碼」:
stu001:密碼 stu002:密碼 stu003:密碼 stu004:密碼 stu005:密碼 stu006:密碼
chpasswd < new_plist pwconv
完成後,這批帳號就能立即用來登入 FTP。幾個實務提醒:
/sbin/nologin,並在 vsftpd 設定中啟用對應的 PAM 支援。/etc/ssh/sshd_config 用 AllowUsers 或 DenyUsers 明確控管。