You should take a look at this answer which describes configuring ssh
for your user (editing ~/.ssh/config
) and other details.
The steps are:
- Generate your
ssh
key.
- Add the Host to your
~/.ssh/config
file.
- Add your Public (
.pub
) key to the remote user's ~/.ssh/authorized_keys
- This is most easily done with
ssh-copy-id
command.
ssh
is very particular about the permissions of ~/.ssh/
and the files found within. ssh-copy-id
handles everything for you.
- Try connecting to the Host:
ssh host
ssh host -vvv # Verbose output for troubleshooting
Are you trying to use ssh-agent
because your keys are protected by a password? I would recommend working on manually connecting without ssh-agent
and getting that working. After you have your key working, you can work on solving any of the ssh-agent
-specific issues.
To troubleshoot, be sure to use ssh
in a verbose mode, and also monitor (tail -f
) the remote server's /var/log/auth.log
file. On newer systems, you may have to use journalctl
(journalctl -u sshd | tail -f
).
Once you've gotten the key working in general, you can look into ssh-agent
documentation, such as this set of setup instructions. Typically the steps are as follows:
- Generate your keys (as you have already done).
- Install the keys (as you have already done).
- Start
ssh-agent
eval ssh-agent
- Only once, not each connection or anything.
- You can have this happen automatically, depending on when and for which user.
- Add your key to
ssh-agent
:
ssh-add ~/.ssh/private_key
Be sure to look into other ssh-agent
configuration options, such as the duration your keys will remain unlocked.
Boiling it down a bit, your problem is most likely one of these:
- You say you added
ssh-agent
starting, and the addition of your id_rsa
key (in the past), but now that you've generated a new key, that key will also need to be ssh-add
'd.
- Check that
ssh-agent
is actually running on your system if you have problems after adding the key.
ps aux | ssh-agent
- You're adding the key to your local file, not the remote user's file.
- Think of it like you're adding a password, so you need to add the password on the system which should accept it.
- Your remote host is localhost, but this assumes you'll want to be able to work on remote hosts in the future. However, it still needs to be in the correct
$HOME
directory for the correct user.
- You say it works a single time? Does it work twice if you try two times in a shorter timeframe, say one minute? I am trying to understand if your
ssh-agent
is just set up to lock your key in a shorter timeframe than you've tested.
- When appending to the
~/.ssh/authorized_keys
file (>>
), you initially created the file, and this would be with incorrect permissions.
- Do not do the following without securing access, and making backups to any remote files.
- If your 'remote host' is localhost, it very likely has files you want to keep, such as your keys, and should be backed up before removal.
- Delete the entire remote
~/.ssh/
directory and use ssh-copy-id
to properly key your user on the remote host.
- This would show up on the remote host's
auth.log
and specify that the file permissions are incorrect.
- If you continue to experience issues after using
ssh-copy-id
to create the directory and files, post the permissions of ~/.ssh
and your generated key files.
- After you
ssh
into the remote host, you lose ssh-agent
, unless you have configured ssh-agent
, and ssh
via ~/.ssh/config
to ForwardAgent yes
. If you wish to forward ssh-agent
, you'll have to configure it to allow that, and understand the security implications with that decision. It should also be noted that ForwardAgent
may be designed to run on different machines, and it may not be possible to get forwarding working locally because of complications with ssh-agent
already running.
ssh localhost
from the previous SSH session? In other words, are you doingssh localhost
, logged in successfully, and then in the same terminal without logging out, doingssh localhost
again? – marcelm Oct 07 '18 at 23:45ssh
. It threw me off too, at first, but we can also assume that the OP, or other readers, will eventuallyssh
into remote hosts. – earthmeLon Oct 08 '18 at 00:01sudo
sometimes implement access viassh
keys to be able to switch users. – earthmeLon Oct 08 '18 at 00:08ssh localhost
, but I'd prefer to hear the actual reason from them ;) – marcelm Oct 08 '18 at 00:26