DEV Community

Eswari Jayakumar
Eswari Jayakumar

Posted on

Automate Ubuntu User creation with SSH Key access using a shell script

This blog will teach you about the shell script to automate new user creation in Ubuntu with administrative privileges. Also, you will learn about how to set up SSH access with an authorized key for the same user.

1. Generate an SSH key in the local machine
Before executing the script in the target machine, Generate an SSH key in the local machine from where you are planning the access the target machine.

In your local machine, Open Terminal and execute the below command

ssh-keygen -t rsa
Enter fullscreen mode Exit fullscreen mode

Image description

Now, Execute the command and generate public and private keys. As per the above screenshot, the public key gets saved in the file testuserrsa.pub. Copy the contents of the file and provide it in the below script for the variable PUBLIC_KEY

2. Execute the script in the target machine
Below is the script which is used to automate Ubuntu user creation.

Let us see a detailed line-by-line explanation of the commands.

sudo adduser --disabled-password --gecos "" testuser
Enter fullscreen mode Exit fullscreen mode

This command will create a new user named “testuser” without setting any password ( — disabled-password). It does not ask for any additional information(--gecos “”) and creates a user with default settings.

sudo usermod -aG sudo testuser
Enter fullscreen mode Exit fullscreen mode

This command simply adds the user to the “sudo” group which grants admin privileges to the user. So, the user can run commands with root privileges using the “sudo” command.

sudo mkdir -p /home/testuser/.ssh && 
sudo touch /home/testuser/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

By this command, a new directory named .ssh is created inside the home directory of the user. i,e. /home/testuser. Then, a new file named “authorized_keys” is created within the .ssh directory.

sudo chmod 700 /home/testuser/.ssh && 
sudo chmod 600 /home/testuser/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

With this command, you are setting appropriate permissions for the .ssh directory and authorized_keys file. Using chmod 700 command, the owner will have all the permissions over the directory. Using chmod 600 command, the Owner will have read and write permissions for the authorized_keys file, no access for the group or everyone else.

sudo chown -R testuser /home/testuser/.ssh
Enter fullscreen mode Exit fullscreen mode

This command changes the ownership of the .ssh folder to the user “testuser”.

sudo sh -c "echo $public_key > /home/testuser/.ssh/authorized_keys"
Enter fullscreen mode Exit fullscreen mode

With the help of this command, the public SSH key is written into the authorized_keys file in the .ssh folder of testuser’s home directory.

sudo sh -c "echo 'testuser ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/testuser-user"
Enter fullscreen mode Exit fullscreen mode

On executing this command, this creates a new file “testuser-user”inside /etc/sudoers.d directory and makes the entry “testuser ALL=(ALL) NOPASSWD:ALL” inside that file. In this way, the testuser will be granted permission to execute any command with sudo privilege without requiring any password.

Download the above script file and execute it either manually or using any automation tools, the user will be created without any external user prompts.

In this way, you can automate Ubuntu user creation with admin privileges and SSH key access.

Thanks for reading !

If you like this article, do follow me and connect with me in LinkedIn to learn about more interesting topics.

Top comments (0)