Skip to content

Instantly share code, notes, and snippets.

@JSONOrona
Created April 23, 2024 16:53
Show Gist options
  • Save JSONOrona/f354cdb70ba5c1973e03884bd36e4f4f to your computer and use it in GitHub Desktop.
Save JSONOrona/f354cdb70ba5c1973e03884bd36e4f4f to your computer and use it in GitHub Desktop.
Add sudo user in ubuntu
#!/bin/bash
# Ensure the script is run as root
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Check for required argument
if [ $# -ne 2 ]; then
echo "Usage: $0 <username> <password>"
exit 1
fi
USERNAME=$1
PASSWORD=$2
# Check if the user already exists
if id "$USERNAME" &>/dev/null; then
echo "User $USERNAME already exists."
exit 1
fi
# Add the user with a home directory
adduser --disabled-password --gecos "" "$USERNAME"
if [ $? -ne 0 ]; then
echo "Failed to add user $USERNAME."
exit 1
fi
# Set the user's password
echo "$USERNAME:$PASSWORD" | chpasswd
if [ $? -ne 0 ]; then
echo "Failed to set password for user $USERNAME."
userdel "$USERNAME"
exit 1
fi
# Add user to the sudo group
usermod -aG sudo "$USERNAME"
if [ $? -ne 0 ]; then
echo "Failed to add user $USERNAME to sudo group."
userdel "$USERNAME"
exit 1
fi
echo "User $USERNAME added successfully with sudo privileges."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment