Resolving “Permission Denied” Error when Creating Ubuntu Sudo User with AI

Paul Grieselhuber

Paul Grieselhuber

Oct 17, 2024
## 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](https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-22-04). 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...\...1x [email protected] 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: ![authorized\_keys file in a text editor.png](https://cms.rendr.io/uploads/authorized_keys_file_in_a_text_editor_b59524919a.png) 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: ![authorized\_keys file in a text editor-enter ssh.png](https://cms.rendr.io/uploads/authorized_keys_file_in_a_text_editor_enter_ssh_12a4619302.png) ### 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?
Paul Grieselhuber

Paul Grieselhuber

Founder, director

Paul has extensive background in software development and product design. Currently he runs Rendr.

Book a discovery call with our product experts.

Our team of web and mobile application experts look forward to discussing your next project with you.

Book a call 👋