create_user_and_configure_sudo.sh
· 728 B · Bash
原始檔案
#!/bin/bash
# Check if a username was provided as an argument
if [ $# -ne 1 ]; then
echo "Error: Please provide a username as an argument."
exit 1
fi
# Get the username from the argument
username="$1"
# Prompt for the user's password
read -s -p "Enter password for $username: " password
echo
# Add the user and set the password
sudo adduser --home /home/$username --gecos "" --disabled-password $username
echo "$username:$password" | sudo chpasswd
# Add the user to the sudo group
sudo usermod -aG sudo $username
# Configure the sudoers file to allow the user to use sudo without a password
echo "$username ALL=(ALL:ALL) ALL" | sudo tee -a /etc/sudoers
echo "$username ALL=NOPASSWD: ALL" | sudo tee -a /etc/sudoers
| 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 |