Automate Ubuntu User creation with SSH Key access using a shell script
Tags: linux, ubuntu, ssh, devops
Published: 2023-07-02 17:46:34

---
title: Automate Ubuntu User creation with SSH Key access using a shell script
published: true
description: This blog will teach you about the shell script to automate new user creation in Ubuntu with administrative privileges and SSH Key access
tags: linux, ubuntu, ssh, DevOps
# cover_image: https://unsplash.com/photos/4Mw7nkQDByk
# Use a ratio of 100:42 for best results.
# published_at: 2023-06-21 21:29 +0000
---

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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u42aw4cwr2k97umehxqx.png)



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.

{% embed https://gist.github.com/eswarijayakumar/2dc66ddef4aec95ee4e5961b9b251fce %}

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

```
sudo adduser --disabled-password --gecos "" testuser
```
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
```
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
```
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
```

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
```
This command changes the ownership of the .ssh folder to the user “testuser”.

```
sudo sh -c "echo $public_key > /home/testuser/.ssh/authorized_keys"
```
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"
```
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](https://www.linkedin.com/in/eswari-jayakumar/) to learn about more interesting topics.