SSH public key authentication: Difference between revisions
imported>Brogolem35 No edit summary |
imported>Almino m Just to make things easier to understand |
||
| Line 1: | Line 1: | ||
To setup a public key based SSH connection from <code> | To setup a public key based SSH connection from <code>your-machine</code> (client) to <code>another-machine</code> (server): | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
[user@ | [user@your-machine] $ ssh-keygen -f ~/.ssh/another-machine | ||
[user@ | [user@your-machine] $ ssh-copy-id -i ~/.ssh/another-machine another-machine-host-or-ip | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Now the public key is stored on the <code> | Now the public key is stored on the <code>another-machine</code> in <code>/home/user/.ssh/authorized_keys</code> | ||
On | On <code>your-machine</code>, we stored the key file in the non-standard path <code>~/.ssh/another-machine</code>, so we must tell the SSH client to use the key file: | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
[user@clientmachine] $ ssh -i ~/.ssh/ | [user@clientmachine] $ ssh -i ~/.ssh/another-machine another-machine-host-or-ip | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 19: | Line 19: | ||
<syntaxhighlight> | <syntaxhighlight> | ||
Host | Host another-machine | ||
HostName 192.168.1.105 | HostName 192.168.1.105 # another-machine-host-or-ip | ||
#Port 22 | #Port 22 | ||
#User user | #User user | ||
| Line 26: | Line 26: | ||
# Prevent using ssh-agent or another keyfile, useful for testing | # Prevent using ssh-agent or another keyfile, useful for testing | ||
IdentitiesOnly yes | IdentitiesOnly yes | ||
IdentityFile ~/.ssh/ | IdentityFile ~/.ssh/another-machine | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== SSH server config == | == SSH server config == | ||
Optionally, on the NixOS-based <code> | Optionally, on the NixOS-based <code>another-machine</code>, we can set <code>passwordAuthentication = false;</code> to require public key authentication for better security. | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 48: | Line 48: | ||
users.users."user".openssh.authorizedKeys.keys = [ | users.users."user".openssh.authorizedKeys.keys = [ | ||
"ssh-rsa AAAAB3Nz....6OWM= user" # content of authorized_keys file | "ssh-rsa AAAAB3Nz....6OWM= user" # content of authorized_keys file | ||
# note: ssh-copy-id will add user@ | # note: ssh-copy-id will add user@your-machine after the public key | ||
# but we can remove the "@ | # but we can remove the "@your-machine" part | ||
]; | ]; | ||
</syntaxhighlight> | </syntaxhighlight> | ||