By 蓉爸 RungBa Created: 2023-07-19 Revised: 2023-07-19 --- **Ref:** - **[How to Create Multiple User Accounts in Linux (2023-07-14)](https://www.tecmint.com/create-multiple-user-accounts-in-linux/)** - **[newusers - For Creating Multiple Users at Once in Batch Mode (2023-05-02)](https://stackdiary.com/linux-docs/newusers/)** - **[Use sudo Command in Non-Interactive Mode (2021-11-01)](https://www.baeldung.com/linux/sudo-non-interactive-mode)** --- ## 一、 ### 1. 使用指令建立使用者 ### 2. 快速建立多個使用者 - **2-1. 建立 Python 程式 create_users.py,並用此程式來建立 users.txt** **create_users.py** ```python= import os min = 1 ## max = 40 ## userid = '10' groupid = userid usercmt = 'AIoT Users' homedir = '/home/aiot' shell = '/bin/bash' userline = '' while min < max+1: uname = 'aiot' + '{idx:02}'.format(idx=min) pwd = uname uid = userid + '{idx:02}'.format(idx=min) gid = uid hdir = homedir + '{idx:02}'.format(idx=min) userline = uname + ':' + pwd + ':' + uid + ':' + gid \ + ':' + usercmt + ':' + hdir + ':' + shell print(userline) min += 1 ``` **產生的 users.txt 內容範例** ```text= aiot01:aiot01:1001:1001:AIoT Users:/home/aiot01:/bin/bash aiot02:aiot02:1002:1002:AIoT Users:/home/aiot01:/bin/bash ... ``` - **2-2. 程式用法** **注意:使用 newusers 指令時,users.txt 的檔名,似乎有限制。例如若執行 newusers users01-30.txt,就會無法建立成功!!** ```bash= ## 方法一 python3 create_users.py > users.txt chmod 600 users.txt sudo newusers users.txt ## 方法二 sudo bash -c "python3 create_users.py | newusers" ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up