Remote login via ssh-keys.

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
2,395
Reaction score
2,084
Credits
18,416
This has been talked about before, and a lot of old timers know this already.

How do I login to a remote Linux system without needing to type in my password every time?

Obviously you need two computers to do this. Your local computer and the remote computer.
You will need an account on both computers as well. You will need to know the password of the account on the remote
computer as well. The account doesn't have to match the same username as the account on the local computer.

First, make sure you have a local public ssh key. I do this like this.

ssh-keygen -t ecdsa -b 384

This will give some output to your screen, and you'll see a little square, something like a QR code.
It will ask for something called a "passphrase". Don't enter anything here, just press "enter".
In fact I recommend using the defaults for all the questions.
Now there are lots of articles about what kind of encryption key to use. RSA or DSA or Elliptical DSA or others.
The -t is the "type" of key. rsa or dsa or ecdsa for example. The -b is the "bits" used to encrypt the key.

Common values for rsa and dsa are 1024, 2048, and 4096. I've never used higher, but it's probably possible.
Common values for elliptical keys are 256, 384 and 512. Less bits may seem less secure, but elliptical keys
use a "curving" algorithm, so they are very secure.

Most current distro's include a utility called "ssh-copy-id". It's pretty simple to use.

From your local system...

ssh-copy-id myusername@theremotecomputer for example...

ssh-id-copy [email protected]

The first time you do this, it will ask for your password.

Now log back out, and login again.

ssh [email protected]

viola' - no password needed.
 
Last edited:


It was brought to my attention, some distro's don't have ssh tuned on by default.
Many do not allow ssh as root.

Since most common distro's today use systemd.
You can use this command to enable sshd.

systemctl enable sshd

This will automatically start sshd every time you restart your computer.
..and then to start the service.

systemctl start sshd

To check the status of the service and see if it is running...

systemctl status -l sshd

( lowercase L )

==========================================

Now what if I want to ssh as root. This really isn't recommended for beginners.
Disclaimer: Not responsible for what people do as "root".
Some distro's disable this by default, ( probably a good idea )
But now that you've been given warning... this is how you do it :)

cd /etc/ssh

edit the sshd_config file. ( prefer vi, but anything will work ) you will need root escalation to do this.
Find the line that looks similar to this.

#PermitRootLogin no

or sometimes...

#PermitRootLogin prohibit-password

and change it to this.

PermitRootLogin yes

Be sure to remove the # at the beginning of the line.
Save the file.

systemctl restart sshd

..and now you can ssh as root.
 
Since most common distro's today use systemd.
You can use this command to enable sshd.

systemctl enable sshd

This will automatically start sshd every time you restart your computer.
..and then to start the service.

systemctl start sshd
If you run the following it will enable and start them all in one command.
Code:
systemctl enable sshd --now
 

Members online


Top