Tech

Add Sudo User on Ubuntu Remote Machine

The sudo user is someone who have privilege to run a command as the superuser (root-user) or another user. Usually linux administrators use to sudo command to perform day to day operation.

User management is a critical part of maintaining a secure system. Ineffective user and privilege management often lead many systems into being compromised. Therefore, it is important that you understand how you can protect your server through simple and effective user account management techniques.
Please go through Ubuntu docs to read more about User management and security users: https://ubuntu.com/server/docs/security-users



Lets create user with sudo rights on ubuntu remote machine:
1. SSH to your ubuntu machine as root. (Please go through this link, if you want to create free ubuntu machine on oracle cloud.)

$ ssh -i <your-private-key-file> <user-name>@<your-public-ip-address>

2. We will add the new user account as root user. Now run below command to create user.

$ adduser <new_user>

3. we need to add the this new-user to the sudo group. On Ubuntu 20.04, sudo group members can by default use sudo command.
Run below command to add

$ usermod -aG sudo <new_user>

4. Now new user can do SSH to this machine either by password or through key
We will generate new set of keys for user

# Below command will generate ssh keys
$ ssh-keygen

# it will ask you to save ssh key in /your_home/.ssh/id_rsa
# you can select the location of ssh keys
# also when prompted for passphrase pass some password for your ssh key(optional) we can leave it empty

# Below two files will be saved
#Your identification has been saved in /your_home/.ssh/id_rsa
#Your public key has been saved in /your_home/.ssh/id_rsa.pub

# Now private key should be with user (who will be be doing SSH) and public key needs to be saved to user's home directory

$ cat /your_home/.ssh/id_rsa.pub | "mkdir -p <new-user-home>/.ssh && touch <new-user-home>/.ssh/authorized_keys && chmod -R go= <new-user-home>/.ssh && cat >> <new-user-home>/.ssh/authorized_keys"

# if you're logged in as root and you're creating user with sudo rights for yourself and you want to use same key as sudo then:
# Since your public key is already in the root account’s ~/.ssh/authorized_keys file on the server, we can copy that file and directory #structure to our new user account in our existing session.

#copy ssh keys to <new_user>
$ rsync --archive --chown=<new_user>:<new_user> ~/.ssh /home/<new_user>




Hurray!! you are done. User can do SSH to machine using below command.

$ ssh -i <your-private-key-file> <new_user>@<your-public-ip-address>

Leave a Reply

Your email address will not be published. Required fields are marked *