


























Disclaimer:
SSH keys use asymmetric cryptographic algorithms that generate a pair of separate keys (a key pair). A private and a public key.
We are using the command ssh-keygen to generate our secure key pair. There are 3 common algorithms to choose from.
We are going to create a private and public key with the name nameofthekey in the .ssh directory of the current user. You should choose a expressive name, which makes it easier to work with multiple keys. Please make sure that the directory ~/.ssh/ exists.
Important: Please do use a secure password for the key generation.
ssh-keygen -t rsa -b 4096 -f ~/.ssh/nameofthekeyssh-keygen -t ecdsa -b 521 -f key1 ~/.ssh/nameofthekeyssh-keygen -t ed25519 -f ~/.ssh/nameofthekeyssh-keygen # can be run as a standard user, man ssh-keygen for more information-t [dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | rsa] # choose Algorithm-b bits # number of bits to use-f /path/and/name-of-keypair # choose a name for the keysssh-keygen -t rsa -b 4096 -f ~/.ssh/nameofthekey
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in name
Your public key has been saved in name.pub
The key fingerprint is:
SHA256:8KkCBz2GFXusy6URXF4Z/8xVl+6dFhYV0MoDtqIqBfA kuser@pleasejustwork
The key's randomart image is:
+---[RSA 4096]----+
| o.. oo .o.B|
| . = = ... o =.|
| = B = o + + .|
| E = o o = = + |
| . = . S . + + +|
| + * o +.|
| * o . |
| . o |
| . |
+----[SHA256]-----+
This would give us 2 files: private key nameofthekey, and public key nameofthekey.pub.
nameofthekey.pub - public key
Example:
ssh-rsa ktLfCNsABzCw9wE4U3JS8mn1t8jw2Q01wRvCaexpuE2adZYxgw4sNJfBOp3SmLEYeF3rcP1u9ffb2J8FOqFWj3egwjVvVrlDHwi6Jr1aTxOmNlGtNHfJiKuJxD3HxPFAuSImsR5IZF6Bki0LxQGxM4jx8NgDFQ5BWO0tJ0pNzSJdXOLwW0jqbdqdEHELnYZLmll6oeJ9j1LZx6GY5vjYxzeCxZTrHoFQPE2vdYsx7ajIKDzQpNdM9zhYRO10OM kuser@pleasejustwork
nameofthekey - private, password protected
Example:
-----BEGIN OPENSSH PRIVATE KEY-----
iEnCTyTmiYVhFvUIYhlq07FZV3EaVpQalFqSRicpeaDqifcDLqdp5NAx11JT17iNhgRDMrTM7Pcs6kLFbXC8LWbhlJVTkhu9k5wIG9Ec6qBthyAzmnO7SpqFCtKAXmuG8uFJF9SeyLsXTFiIuK8UqfgG9SLvXSrhPFqSVWFVxQqmXiXL5MQ7iKOKAAAlwisfwrJ1DTNkd2C9nel7sorAU3gWQGh2beuEjzkRsYucR9lxO6jzLEejNSwyS7TNuOiEnCTyTmiYVhFvUIYhlq07FZV3EaVpQalFqSRicpeaDqifcDLqdp5NAx11JT17iNhgRDMrTM7Pcs6kLFbXC8LWbhlJVTkhu9k5wIG9Ec6qBthyAzmnO7SpqFCtKAXmuG8uFJF9SeyLsXTFiIuK8UqfgG9SLvXSrhPFqSVWFVxQqmXiXL5MQ7iKOKAAAlwisfwrJ1DTNkd2C9nel7sorAU3gWQGh2beuEjzkRsYucR9lxO6jzLEejNSwyS7TNuO
-----END OPENSSH PRIVATE KEY-----
It is important to have the correct permissions for your key. For 2 reasons: restrict the access of other users, and some servers require it, when the 'StrictModes' is enabled. Later more.
sudo chmod 700 ~/.ssh
sudo chmod 644 ~/.ssh/authorized_keys
sudo chmod 644 ~/.ssh/known_hosts
sudo chmod 644 ~/.ssh/config
sudo chmod 600 ~/.ssh/nameofthekey # private key
sudo chmod 644 ~/.ssh/nameofthekey.pub # public key
You need access to the destination server in one way or another to add the newly generated public key. There are multiple ways.
In the end, the public key must be added to the ~/.ssh/authorized_keys file. If it does not exist, it must be created. There can be multiple public keys in this file - one line per key, and there can be multiple authorized_keys, IF it is configured on the server.
Ask someone with access to add your public key to the ~/.ssh/authorized_keys file.
You most likely already have access to the server via ssh and normal password authentication. There are now multiple ways to add your public key to the server.
ssh-copy-id:ssh-copy-id -i ~/.ssh/nameofthekey.pub remote-user@remote-serverauthorized_keys file on the remote machine.Different way would be to copy the public key to the remote machine via scp / rsync, or something different, and redirect >> it to ~/.ssh/authorized_keys. Another way would be to connect to the server, and copy-paste the content of the public key to ~/.ssh/authorized_keys. Remember, if the path or file does not exist, just create it.
In the end, your chosen public key must be in the file ~/.ssh/authorized_keys before you should continue.
Important: Some tips on how to work on the configuration file on the remote machine.
We now have to edit the ssh server config file on the remote machine: /etc/ssh/sshd_config or in the config directory /etc/ssh/sshd_conf.d. It depends on your setup.
PubkeyAuthentication yessystemd:sudo systemctl reload sshdssh -i ~/.ssh/nameofthekey remote-user@remote-server # choose the private key!sshd_config file and add:StrictModes yessudo systemctl reload sshdImportant: Please test the connection once more!
If you successfully connected to the remote machine, you can proceed to turn off password authentication.
Last chance: make sure that you have tested the public key authentication, and / or have another option to access the machine.
sshd_config file and change one option:PasswordAuthentication noThis will disable the possibility to authenticate with a password, but you should still be able to log in with your public key, after reloading the config.
sudo systemctl reload sshdThis should be it!
More SSH hardening options can be found here.
-v / -vv / -vvvssh -vvv -i ~/.ssh/nameofthekey remote-user@remote-serversudo journalctl -u sshsudo grep ip.of.your.machine /var/log/auth.logLogLevel INFO # defaultLogLevel DEBUG # enable DEBUG modeDon't forget to turn it off again before it fills up your storage.
Nobody wants to enter their password for the private key every time they want to connect to a server. By using ssh-add - the OpenSSH auth agent - you can add your private key once for the session, and do not have to enter your private key password every time.
ssh-add -Lssh-add ~/.ssh/nameofthekey # choose the private key and enter the passwordssh-add -DCould not open a connection to your authentication agent.Just run eval "$(ssh-agent)" OR `eval ssh-agent` and right after exec ssh-agent bash. This restarts the agent and sets the correct environment variables from my understanding.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。