Skip to content

Instantly share code, notes, and snippets.

@ghostdevv
Last active July 9, 2025 23:11
Show Gist options
  • Select an option

  • Save ghostdevv/cd86d94e2a6c97e23d2c05063a2d7fe5 to your computer and use it in GitHub Desktop.

Select an option

Save ghostdevv/cd86d94e2a6c97e23d2c05063a2d7fe5 to your computer and use it in GitHub Desktop.
create-user-script
#!/bin/bash
# Check if script is run with root privileges
if [ "$EUID" -ne 0 ]; then
echo "Please run this script as root or with sudo"
exit 1
fi
# Function to validate username
validate_username() {
local username=$1
if [[ ! $username =~ ^[a-z_][a-z0-9_-]*$ ]]; then
echo "Invalid username. Username must start with a letter or underscore and contain only lowercase letters, numbers, underscores, or hyphens."
return 1
fi
return 0
}
# Function to check if user already exists
check_user_exists() {
local username=$1
if id "$username" &>/dev/null; then
echo "User $username already exists!"
return 1
fi
return 0
}
# Function to validate SSH key
validate_ssh_key() {
local key="$1"
if ! echo "$key" | ssh-keygen -lf - &>/dev/null; then
echo "Invalid SSH public key!"
return 1
fi
return 0
}
# Main script
echo "Linux User Creation Script"
echo "========================="
# Get username
read -p "Enter username: " username
# Validate username
if ! validate_username "$username"; then
exit 1
fi
# Check if user exists
if ! check_user_exists "$username"; then
exit 1
fi
# Get SSH public key
echo "Please paste the SSH public key (press Ctrl+D when done):"
ssh_key=$(cat)
# Validate SSH key
if ! validate_ssh_key "$ssh_key"; then
exit 1
fi
# Ask about sudo access
while true; do
read -p "Add user to sudo group? (y/n): " sudo_access
case $sudo_access in
[Yy]* ) add_sudo=true; break;;
[Nn]* ) add_sudo=false; break;;
* ) echo "Please answer y or n.";;
esac
done
# Create user with home directory
useradd -m "$username"
# Create .ssh directory and set permissions
user_home=$(eval echo ~"$username")
mkdir -p "$user_home/.ssh"
echo "$ssh_key" > "$user_home/.ssh/authorized_keys"
# Set correct ownership and permissions
chown -R "$username:$username" "$user_home/.ssh"
chmod 700 "$user_home/.ssh"
chmod 600 "$user_home/.ssh/authorized_keys"
# Add user to sudo group if requested
if [ "$add_sudo" = true ]; then
usermod -aG sudo "$username"
echo "$username ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/$username > /dev/null && sudo chmod 0440 /etc/sudoers.d/$username
sudo_status="User has been added to sudo group"
else
sudo_status="User has standard privileges (no sudo access)"
fi
# Set shell to bash
usermod -s /bin/bash "$username"
# Disable password authentication
passwd -d "$username"
passwd -l "$username"
# Add to docker group
sudo usermod -aG docker "$username"
echo "User $username has been created successfully!"
echo -e "\nUser Information:"
echo "Username: $username"
echo "Home Directory: $user_home"
echo "Groups: $(groups $username)"
echo -e "\nNotes:"
echo "- $sudo_status"
echo "- SSH key has been installed"
echo "- Password authentication is disabled (SSH key only)"
echo "- Default shell is set to /bin/bash"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment