最終更新 10 months ago

這個 Bash 腳本用於根據輸入的使用者名稱,建立一個新的使用者帳號,並提示輸入密碼後設定該密碼,然後將該使用者加入 sudo 群組,同時更新 sudoers 檔案以允許該使用者無密碼執行 sudo 指令,方便日後進行系統管理。

修正履歴 3f761f5823e864f43dc3c4cbd41f21e77881a192

create_user_and_configure_sudo.sh Raw
1#!/bin/bash
2
3# Check if a username was provided as an argument
4if [ $# -ne 1 ]; then
5 echo "Error: Please provide a username as an argument."
6 exit 1
7fi
8
9# Get the username from the argument
10username="$1"
11
12# Prompt for the user's password
13read -s -p "Enter password for $username: " password
14echo
15
16# Add the user and set the password
17sudo adduser --home /home/$username --gecos "" --disabled-password $username
18echo "$username:$password" | sudo chpasswd
19
20# Add the user to the sudo group
21sudo usermod -aG sudo $username
22
23# Configure the sudoers file to allow the user to use sudo without a password
24echo "$username ALL=(ALL:ALL) ALL" | sudo tee -a /etc/sudoers
25echo "$username ALL=NOPASSWD: ALL" | sudo tee -a /etc/sudoers