Question

SSH key working fine, but after logout from root and back for root again it ask for pass

I have a ssh key working fine, i can login as root user without password. So, i created a new user, when i log in it and be back for root again, root ask for password, but i don’t know because i’m using ssh key for login as root.

i try edit config /etc/ssh/sshd_config and updated the following line:

PermitRootLogin yes

to

PermitRootLogin without-password

but no success, i continue being ask for root password. Can someone help me out on this problem?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

@marcosmendes

Setting PermitRootLogin to without-password means that the root user must login using a public key. If you’re trying to run su root to become root from different users account, you will get prompted for a password.

You’d be better off creating a new user, setting up their environment, and adding them as a sudo user. Of course, you’re still going to be prompted to authenticate when you run sudo as well. If you weren’t then anyone that was able to login to that account would have free range to run root level commands without any secondary authentication.

Setting up a Sudo User

I’ll use myuser as the username of the new user in this example, so wherever you see myuser, you would simply substitute in the username of your choice.

1). Create a Home + .ssh Directories

mkdir -p /home/myuser/.ssh

2). Create a New User + Assign the Home Directory

useradd -d /home/myuser myuser

3). Create the authorized_keys File

touch /home/myuser/.ssh/authorized_keys

4). Setup Correct Permissions

chown -R myuser:myuser /home/myuser \
&& chmod 700 /home/myuser/.ssh \
&& chmod 644 /home/myuser/.ssh/authorized_keys

5). Add Public Key to authorized_keys

You’d simply paste in your public key, then hit CTRL+X and hit enter to save.

nano /home/myuser/.ssh/authorized_keys

6). Add a Password for myuser

passwd myuser

With the above setup, you can now SSH in using:

ssh myuser@DROPLETIP -i /path/to/local/private_key

If you setup a passphrase on the key itself, you’d enter it in and once logged in, you start off with just basic permissions. You can’t run root level commands until you prefix those commands with sudo.

If you try to run a root command, it’ll fail – i.e.

apt-get upgrade

You would need to use:

sudo apt-get upgrade

and when prompted, enter in the password for myuser – the command will then execute.

SSH Keys exist to get in you – after that, passwords do come in to play, especially when you’re using either su or sudo.

The point is to not have to login as root at all – you should login as the sudo user and escalate using the sudo prefix on each command from.

I would add/edit to this awesome post by noting that the steps above will lose your prompt settings and that you could do the exact same thing using useradd rather than adduser. See https://askubuntu.com/questions/345974/what-is-the-difference-between-adduser-and-useradd

So the steps without running into the blank prompt would look like this.

  1. Create user using adduser assigning to home directory. you will be prompted to create a password so you dont need the passwd myuser step above.
adduser --home /home/myuser myuser
  1. Create home/myuser/.ssh directory
mkdir /home/myuser/.ssh
  1. Create the authorized_keys File
touch /home/myuser/.ssh/authorized_keys

4). Setup Correct Permissions

chown -R myuser:myuser /home/myuser \
&& chmod 700 /home/myuser/.ssh \
&& chmod 644 /home/myuser/.ssh/authorized_keys

5). Add Public Key to authorized_keys

You’d simply paste in your public key, then hit CTRL+X and hit enter to save.

nano /home/myuser/.ssh/authorized_keys

With the above setup, you can now SSH in using:

ssh myuser@DROPLETIP -i /path/to/local/private_key and when you log in you will a proper colorized prompt rather as set in default root configs, if you want it.

Great post! Thanks for sharing.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel