By default, ssh-keygen will create a 2048-bit RSA key pair, which is secure enough for most use cases (you may optionally pass in the -b 4096 flag to create a larger 4096-bit key). After entering the command, you should see the following prompt. It can create RSA keys for use by SSH protocol version 1 and RSA or DSA keys for use by SSH protocol version 2. He type of key to be generated is specified with the -t option. If invoked without any arguments, ssh-keygen will generate an RSA key for use in SSH protocol 2 connections. Step 1: Generate SSH Public/Private Key Pair on CentOS/RHEL Desktop. On your CentOS/RHEL desktop (not your server), enter the following command in a terminal window. Ssh-keygen -t rsa -b 4096. Where:-t stands for type. The above command generates an RSA type keypair. RSA is the default type.-b stands for bits. By default, the key is 3072 bits long. Sshknownhosts file format The /etc/ssh/sshknownhosts and /.ssh/knownhosts files contain host public keys for all known hosts. The global file should be prepared by the administrator (optional), and the per-user file is maintained automatically: whenever the user connects from an unknown host, its key is added to the per-user file.
Centos 7 Ssh Key Authentication
Introduction & Description
Centos 7 Generate Ssh Host Keys
Do not give out, store remotely or otherwise expose your private key to the outside world or you defeat the purpose entirely of using encrypted keys. Doing so is the equivalent to locking the door to your house and leaving the keys in the handle for anyone to use/take.
We’ll be using RSA in this example however, you’re perfectly welcome and able to use DSA if you so choose. The difference is RSA, by default, uses a 2048 bit key and canbe up to 4096 bits, while DSA keys must be exactly 1024 bits as specified by FIPS 186-2. It is recommended to use a 4096 bit key as a matter of habit in today’s world where personal and private digital security is often in question, never view yourself or your systems as invulnerable and always take the strongest precautions that are available to you.
Centos 8 Generate Ssh Host Keys
With that said we’ll give the following command to create our public/private keypair:
Doing the Work
- Create your public and private keypair using ssh-keygen:
- Copy your ~/.ssh/example_id_rsa.pub on the local system to ~/.ssh/authorized_keys on the remote system ising ssh-copy-id:
- Attempt to login
- Setting up ssh for automatic passwordless login with keys using ssh-agent and ssh-add:
- Add private key identity to the local authentication agent, so we don’t need to enter our password everytime.
- Connect to the remote system
(you will have a public key that you copy to the computers you’ll be accessing, and a private key that does not leave your system ever.)cd ~/.ssh
ssh-keygen -t rsa -b 4096
2 4 6 8 10 12 14 16 18 20 | Enter file inwhich tosave the key(/home/warren/.ssh/id_rsa):example_id_rsa Enter same passphrase again: Your identification has been saved inexample_id_rsa. Your publickey has been saved inexample_id_rsa.pub. 80:b9:33:07:27:22:cb:5a:be:ae:07:d1:79:de:23:28warren@quetzal +--[RSA4096]----+ |o| |ooo..=.| |Eo.o+o| |..| +-----------------+ |
Private Keyfile: example_id_rsa
Public Keyfile: example_id_rsa.pub
chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys
note: If you’re using a laptop which has the possibility of being lost or stolen or you’re using several systems, you may consider using separate public/private keypairs and simply update/add to the authorized_keys file on the target systems. Remember that the private key should never leave the machine it was created on. If the laptop is then lost or stolen you can simply remove the reference to the key on the target machines authorized_keys file. You’ll need to use a naming system that allows you to quickly identify which key belongs to which host(s) as well.
Here are some simple examples:
Enter file in which to save the key (/home/user/.ssh/id_rsa):
id_rsa.dev
id_rsa.laptop
id_rsa.desktop
id_rsa.work
[user@localhost .ssh]$ ssh-copy-id -i example_id_rsa.pub 192.168.0.2
user@192.168.0.2’s password:
Now try logging into the machine, with “ssh ‘192.168.0.2’”, and check in:
~/.ssh/authorized_keys
to make sure we haven’t added extra keys that you weren’t expecting.
[user@localhost .ssh]$ ssh 192.168.0.2
Enter passphrase for key ‘/home/user/.ssh/example_id_rsa’:
Last login: Tue Mar 23 16:04:23 2010 from foo.comcast.net
[user@remotehost]$
add these lines at the bottom of your .bash_profile:vi ~/.bash_profile
SSHAGENT=/usr/bin/ssh-agent
SSHAGENTARGS=”-s”
if [ -z “$SSH_AUTH_SOCK” -a -x “$SSHAGENT” ]; then
eval
$SSHAGENT $SSHAGENTARGS
trap “kill $SSH_AGENT_PID” 0
fi
Next, logout/login or give the command:source ~/.bash_profile
[user@localhost ~]$ ssh-add
Enter passphrase for /home/user/.ssh/example_id_rsa:
Identity added: /home/user/.ssh/id_rsa (/home/user/.ssh/example_id_rsa)
[user@localhost ~]$ ssh 192.168.0.2
Last login: Tue Mar 23 15:57:10 2010 from foo.comcast.net
[user@remotehost ~]$
Summary
You should now be able to use the above sequence to login passwordless to any system you’ve copied your example_id_rsa.pub / authorized_keys file to. Login, use the ssh-add command, give your passphrase once and you should be able to login passwordless. You will be added to the ssh-agent for the remainder of your session until you logout, you’ll need to re-verify your passphrase with each new login session. This verification is needed only on the first use after reboot to verify you are the owner of the key.