Ostatnio aktywny 10 months ago

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

timmy zrewidował ten Gist 10 months ago. Przejdź do rewizji

Brak zmian

timmy zrewidował ten Gist 10 months ago. Przejdź do rewizji

Brak zmian

timmy zrewidował ten Gist 1 year ago. Przejdź do rewizji

1 file changed, 25 insertions

create_user_and_configure_sudo.sh(stworzono plik)

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