Resolving “Permission Denied” Error when Creating Ubuntu Sudo User with AI
Paul Grieselhuber
Introduction
During the process of installing Nginx on Ubuntu, one of the prerequisites is to set up a regular, non-root user with sudo privileges.
This process is defined in a Digital Ocean tutorial, installing Nginx and initial server setup with Ubuntu. By following the tutorial, we managed to easily create a user (Sammy) but encountered a permissions error when trying to login with as Sammy::
ssh sammy@your_server_ip
The error displayed was “Permission denied (publickey)” and there was nothing to cover a resolution in the tutorial, so we fixed it ourselves, with a little help from AI.
This is how.
Step 1. Verify the Public Key Exists on Local Machine
First, check if you have an SSH key on your local machine. The public key typically starts with ssh-ed25519 followed by a long string of characters.
Run this command:
cat ~/.ssh/id_ed25519.pub
You should see something like this:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM4XeQ...<blocked>...1x sammy@example.com
Copy the entire key (including the ssh-ed25519 part) for use in the next steps.
Step 2. Switch to the Server
Log in to your server as the root user:
ssh root@your_server_ip
Step 3. Switch to Sammy
Once logged in, switch to the Sammy user:
su - sammy
Step 4. Create the .ssh Directory
If the .ssh directory does not exist, create it:
mkdir -p ~/.ssh
Step 5. Set Permissions for the .ssh Directory
Set the correct permissions for the .ssh directory:
chmod 700 ~/.ssh
Step 6. Create or Open authorized_keys File
Open the authorized_keys file for editing:
nano ~/.ssh/authorized_keys
Step 7. Paste the Public Key
Paste the SSH public key you copied earlier into the authorized_keys file. To open the authorized_keys file in a text editor, enter this command into Terminal:
nano ~/.ssh/authorized_keys
You should see a screen like this:
Copy your SSH key and paste it into the first line, then click Ctrl + X, then Y, and Enter to save.
The text editor should look like this:
Step 8. Set Permissions for authorized_keys
Set the correct permissions for the authorized_keys file:
chmod 600 ~/.ssh/authorized_keys
Step 9. Try Logging In as Sammy
Now, log out of the server and attempt to log in as Sammy again:
ssh sammy@your_server_ip
Conclusion
We used ChatGPT to troubleshoot this issue and had it resolved within about 30 minutes. By having a live conversation, following instructions and sharing the results AI walked us through this process step by step until we were able to successfully login as the new user with sudo privileges. This error wasn’t covered in the original article so hopefully this is helpful to anyone encountering the same issue. If it doesn’t work for you, how about having a go with ChatGPT to see if it can help you resolve the problem?