I want to be able to login to a remote via ssh without having to enter the password all the time.
- How do I set it up?
- Is a different command required to execute a password-less session?
I want to be able to login to a remote via ssh without having to enter the password all the time.
Answer
Execute this command (if you already have an SSH key, you can skip this step):
Then you'll need to copy the new key to your server:
After the key is copied, ssh into the machine as normal:
You can now login without entering a password from the particular machine you executed the commands at.
Example
Explanation
This assumes you already can successfully connect to your server via SSH.
You'll need to generate an SSH Keypair which will allow you to identify you as yourself without using a password. You can opt to protect keys with a passcode if you wish, but this can be left blank allowing totally password-less SSH access.
ssh-keygen
this will create anid_rsa
andid_rsa.pub
file. Thepub
file is what goes on the servers, the private key (id_rsa
) is what stays with you and is how you identify yourself.ssh-copy-id user@server
replacing user with your remote user and server with the machine DNS name or IP address. It'll prompt for your SSH password, enter it and if all completes successfully you'll be able to access the machine viassh user@server
without needing a password.References
Type the following commands:
ssh-keygen
Press Enter key till you get the prompt
ssh-copy-id -i root@ip_address
(It will once ask for the password of the host system)
ssh root@ip_address
Now you should be able to login without any password.
The way I usually do this is as follows:
ssh-keygen -t rsa
(When prompted for a password, leave it blank)
Then:
cat ~/.ssh/id_rsa.pub | ssh username@hostname 'cat >> .ssh/authorized_keys'
(This requires the folder .ssh to be in the home directory on the targeted hostname, with the authorized_keys file in it)
Of course, replace username with the desired username, and hostname with the desired hostname or IP address
After that, just SSH to that box just like you're used to.
I normally use
sshpass
for that, install it withsudo apt-get install sshpass
and use it like thishttps://help.ubuntu.com/community/SSH/OpenSSH/Configuring#disable-password-authentication
This solution is specifically for users using Windows to ssh into their remote machines including cloud images on AWS Cloud and GCE Cloud
Disclaimer
Recently used this solution to remote login new deployed vm images on GCE.
Tools used:
puttygen
puttygen downloadwinscp
winscp downloadSteps to perform:
How to do it:
1. Generate a key/pair or use existing private key
If you own a private key:
Open
puttygen
, press load button and select your private key (*.pem
) file.If you do not own a private key:
puttygen
,(from source 1, link given below)
2. Create a new 'authorized_keys' file (with
notepad
)Copy your public key data from the "Public key for pasting into OpenSSH authorized_keys file" section of the PuTTY Key Generator, and paste the key data to the
authorized_keys
file.Make sure there is only one line of text in this file.
3. Upload key to the Linux server
Upload authorized_keys file to home directory at remote machine.
4. Set proper permissions
Make
.ssh
directory (if not existing)Copy
authorized_keys
file to.ssh
directory.(this will replace any existed
authorized_keys
file, take note of this).If file existed, simply add contents of this file to the existing file.
Run commands to set permissions:
Now You will be able to
ssh
into remote machine without entering credentials every time.Further reading:
Generating and uploading SSH keys under Windows
OpsenSSH keys certificates authentication PEM PUB CRT
If you create a public/pricate keypair and log in using our newly created public key, you will not need to type your password. Depending on the configuration of your key-ring and/or ssh agent you might need to protect your key with a passphrase.
Here is one of many short howtos for you. It is of crucial importance to the safety of this method, that the generated private key remains private! You should never share it with anyone or allow access of it in any capacity.
This command generates a reasonably strong key in
~/.ssh/
:In
~/.ssh/
you will find your public key asid_rsa.pub
. Its contents should be appended to your serversauthorized_keys
file by transporting the via a transportable media (pen drive) or by shortly enabling password authentication on the server, then usingssh-copy-id ~/.ssh/id_rsa.pub username@server
and then disabling it again.If you chose to secure your key with a passphrase (in the first step), you can use
ssh-agent
or the Ubuntu keyring to secure that pharse locally so you don't have to type it all the time.To make some additions:
Mac by default doesn't have
ssh-copy-id
, you'll have to install it youself:find more here: https://github.com/beautifulcode/ssh-copy-id-for-OSX
if you've made a port-forwarding, the command should be like this:
note that the quotes are necessary.
Remote login/copy without giving a password
The applications
ssh
andscp
for remote login and remote copy, respectively, allow you to communicate with a remote host without giving a password. This requires that you follow an authentication procedure like the one described below. By client we mean the machine your are sitting on and by server we mean the machine you want to log onto without giving a password. The steps of the authentication procedure are:$HOME/.ssh
.$HOME/.ssh/authorized_keys
or$HOME/.ssh/authorized_keys2
on the server.There are three different types of authentication protocols. You specify the type when running ssh-keygen:
chmod 0700
to ensure that this file is not readable for others) and identity.pub (public key).ssh-keygen -t rsa
and results in filesid_rsa
(private key) andid_rsa.pub
(public key)ssh-keygen -t dsa
and results in filesid_dsa
(private key) andid_dsa.pub
(public key)When running ssh-keygen you can rely on default answers (implying that you do not give a passphrase). This makes the whole set-up simple, but also insecure.
You can specify the type of keys to be used by an option to ssh;
ssh -1
forces use of RSA1 keys (protocol version 1), whereasssh -2
forces ssh to try RSA or DSA keys only (protocol version 2). In the examples below, we generate and install RSA1 and DSA keys on the remote host such that you have more flexibility. You can make a config file in your.ssh
directory with the lineThis makes ssh try an RSA1 (protocol version 1) connection before RSA/DSA (protocol version 2).
Using RSA1 keys
Using DSA keys
This is all you have to do if you did not use a passphrase when generating the keys. You can test the connection by running ssh $remote and see if you can log in without giving a password (you may need to use
-1
or-2
as options to ssh). The procedure can, of course, be repeated for any machine you want to log onto.If you did use a passphrase, you will have to run the program
ssh-agent
to start a special shell, followed byssh-add
to register your key/passphrase combination withsshd
. See the man pages for these programs for more information.A script for automating password-free connections:
ssh-no-password.sh
copied from: http://folk.uio.no/hpl/scripting/doc/ssh-no-password.html
I'd like to add an answer for those who may find they should enter the password even they have read all answers here because you have set IdentitiesOnly as yes. And the answer here can save you much time to manage multiple keys, being keys for git or server.
After I have generated the key and copied it to the server:
I found it didn't work.
Then I went to check the
~/.ssh/config
file on the client, I saw this at the bottom:Then I add this above:
I can just log in by entering
ssh somename
.Then you can add multiple ssh keys using your favorite names, and you only need to add the settings like the above four lines to the config file.
Host is the name you'd like to enter when you connect the server later; the HostName is the server's ip; User is the user name you log in the server; and the identityfile is the file where you store the key you have generated.