I have my public / private key setup, but I don't know where it should go. Should both these files be placed in ~/.ssh/
for my user?
1 Answer
The private key stays home. The public key travels:
- Place your private key in
~/.ssh
(local host). It will usually be~/.ssh/id_rsa
. - Place your public key in
~/.ssh/authorized_keys
(remote host).
While the first step is usually done automatically when you create the key, the second can be accomplished through:
$ ssh-copy-id ~/.ssh/id_rsa.pub user@remotehost
If you have any residual configuration concerning the remote host, you might run into some trouble with this (authentication failing, or "too many authentication failures"). To get rid of this problem, you can force ssh-copy-id
to use password authentication with:
$ ssh-copy-id -o"PubkeyAuthentication no" ~/.ssh/id_rsa.pub user@remotehost
Of course, once your keys are correctly placed, there is no more need for a password:
$ ssh user@remotehost
You might also want to add some SSH configuration in ~/.ssh/config
to make it even simpler:
Host [custom name for the remote machine]
Hostname [remote hostname or IP]
User [remote username]
IdentityFile /home/[your local user]/.ssh/id_rsa
Thanks to this, you can just type the following to log into the remote host:
$ ssh [custom name for the remote machine]
Edit: if both machines (local and remote) are the same, you can simplify the procedure with:
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
This will append your public key to the authorized_keys
file. Of course, another way to log as someone else on your local machine is to use:
$ su [another user]
This will save you the SSH transaction where you might not need it. Password-less su
can be set up using sudo
:
$ sudo -iu [another user]
-
-
@MrDuk In this case, both the local and remote ends are the same machine. Therefore, you must have your public key inside
~/.ssh/authorized_keys
locally. For this, you can just usecat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
. I edited my answer. Oct 15, 2014 at 18:39 -
Maybe things have changed since your answer but I needed to add a
-i
to your first command, so that the whole command looks like$ ssh-copy-id -i ~/.ssh/id_rsa.pub user@remotehost
. Oct 17, 2017 at 16:15