Shubham S Nimje logo
Shubham Nimje
SSH Key

How to Create Sudo User & Set Up SSH Key

How to Create Sudo User & Set Up SSH Key
2 min read
#SSH Key

How to Create Sudo User & Set Up SSH Key

Step 1: Access Remote Server via SSH

Before we begin, ensure you have SSH access to your remote server. Use the following syntax to connect:

ssh -p PORT USERNAME@HOSTIP

For example:

ssh -p 22 root@216.32.44.12

Step 2: Create a New User

To create a new user, use the adduser command:

adduser User_Name

For example:

adduser raj

Step 3: Make New User a Sudo User

Grant sudo privileges to the new user with the usermod command:

usermod -aG sudo User_Name

For example:

usermod -aG sudo raj

Step 4: Verify Sudo User Status

Confirm that the user has been added to the sudo group:

groups User_Name

For example:

groups raj

Step 5: Exit from Remote Server

Once the user setup is complete, exit from the remote server:

exit

Step 6: Generate SSH Key on Local Machine

Open your command line terminal and generate an SSH key for the new user:

ssh-keygen -f ~/.ssh/key_name -t ed25519

For example:

ssh-keygen -f ~/.ssh/raj_ed25519 -t ed25519

Step 7: Copy SSH Public Key to Remote Server

For Linux/macOS:

ssh-copy-id -p PORT -i ~/.ssh/keyname.pub Remote_Server_Username@Remote_Server_Host_IP

For example:

ssh-copy-id -p 22 -i ~/.ssh/raj_ed25519.pub raj@216.32.44.12

Step 8: Copy Public Key to Remote Server (for Windows)

If you’re using Windows, follow these steps:

  1. Ensure the remote server user has a .ssh folder in their home directory:

    ssh -P PORT Remote_Server_Username@Remote_Server_Host_IP "mkdir /home/Remote_Server_Username/.ssh"

    For example:

    ssh -P 22 raj@216.32.44.12 "mkdir /home/raj/.ssh"
  2. Copy the public key to the remote server’s .ssh/authorized_keys file:

    scp -P PORT SSH_PUBLIC_KEY_PATH Remote_Server_Username@Remote_Server_Host_IP:/home/Remote_Server_Username/.ssh/authorized_keys

    For example:

    scp -P 22 C:\Users\R\.ssh\raj_ed25519.pub raj@216.32.44.12:/home/raj/.ssh/authorized_keys

By following these steps, you’ll successfully create a sudo user and set up SSH key authentication, enhancing security and accessibility for your remote server.