Skip to content

Instantly share code, notes, and snippets.

@GarimaDamani
Last active April 6, 2020 07:58
Show Gist options
  • Save GarimaDamani/3824a2d4e55b47903a8fc926f8b7fa75 to your computer and use it in GitHub Desktop.
Save GarimaDamani/3824a2d4e55b47903a8fc926f8b7fa75 to your computer and use it in GitHub Desktop.
Linux create user with sudo access. args expected username, group name and public key. Requires root access to provide sudo.
#!/bin/bash
echo "Adding user $1 with sudo access"
adduser --disabled-password --gecos "" $1
cd /home/$1/
mkdir .ssh
cd /home/$1/.ssh/
touch authorized_keys
echo $2 >> authorized_keys
cd /home/$1/
chown -R $1:opsworks .ssh/
echo "$1 ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
echo "$1 added. Please ask them to try to login now"
# Usage
# sudo chown +x linux_add_user.sh && ./linux_add_user.sh garimadamani "ssh-rsa AAA xxxxxxx.local"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment