Last active
February 11, 2025 07:16
-
-
Save tkirkland/edeafee444cff9b2e071c5715a6bc0f8 to your computer and use it in GitHub Desktop.
Quick script to add an admin user on Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # rev .2 2/11/25 | |
| # Check if the script is run as root | |
| if [ "$EUID" -ne 0 ]; then | |
| echo "Please run as root." | |
| exit 1 | |
| fi | |
| # Ensure a username is provided | |
| if [ -z "$1" ]; then | |
| echo "Usage: $0 <username>" | |
| exit 1 | |
| fi | |
| echo "Updating package list..." | |
| apt update -y >/dev/null 2>&1 && apt upgrade -y >/dev/null 2>&1 | |
| USERNAME="$1" | |
| # Check if the user already exists | |
| if id "$USERNAME" &>/dev/null; then | |
| echo "User $USERNAME already exists." | |
| exit 1 | |
| fi | |
| # Check if ssh is installed | |
| if ! dpkg -l | grep -q "ssh"; then | |
| echo "ssh is not installed. Installing..." | |
| apt install -y ssh >/dev/null 2>&1 | |
| else | |
| echo "ssh is installed." | |
| fi | |
| # Check if vim is installed | |
| if ! dpkg -l | grep -q "vim"; then | |
| echo "vim is not installed. Installing..." | |
| apt install -y vim >/dev/null 2>&1 | |
| else | |
| echo "vim is installed." | |
| fi | |
| # Create the new user | |
| # -m: Create the user's home directory | |
| # -s /bin/bash: Set the user's default shell to bash | |
| if ! useradd -m -s /bin/bash "$USERNAME"; then | |
| echo "Failed to create user $USERNAME." | |
| exit 1 | |
| fi | |
| # Set a password for the new user | |
| echo "Set a password for $USERNAME:" | |
| passwd "$USERNAME" || { | |
| echo "Failed to set password for $USERNAME." | |
| exit 1 | |
| } | |
| # Check if the .ssh directory exists in /root | |
| if [ -d /root/.ssh ]; then | |
| echo ".ssh directory exists in /root so we'll copy it to the home directory of $USERNAME." | |
| cp -r /root/.ssh "/home/$USERNAME/" || { | |
| echo "Failed to copy .ssh directory to the home directory of $USERNAME." | |
| exit 1 | |
| } | |
| if ! chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/.ssh"; then | |
| echo "Failed to change ownership of .ssh directory to $USERNAME." | |
| exit 1 | |
| fi | |
| echo "Copied .ssh directory to the home directory of $USERNAME." | |
| else | |
| echo ".ssh directory does not exist in /root." | |
| fi | |
| # Create a sudoers file for the new user | |
| echo "$USERNAME ALL=(ALL) NOPASSWD: ALL" >"/etc/sudoers.d/$USERNAME" | |
| chmod 440 "/etc/sudoers.d/$USERNAME" | |
| # Add the user to common admin groups | |
| ADMIN_GROUPS="sudo adm cdrom dip plugdev lxd lpadmin sambashare" | |
| for group in $ADMIN_GROUPS; do | |
| if getent group "$group" &>/dev/null; then | |
| usermod -aG "$group" "$USERNAME" | |
| fi | |
| done | |
| echo "Enabling 'hushlogin' for $USERNAME." | |
| touch "/home/$USERNAME/.hushlogin" | |
| echo "User $USERNAME has been created and added to the following groups: $ADMIN_GROUPS." | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment