Get the App
SLTechnology News&Howtos  ›  Development  › 

How to set SSH key in CentOS8

Shulou Source: shulou.com Published: 2022-06-01 20:08:48 09月10日 Update

Xiaobian to share with you how to set SSH key in CentOS8, I believe most people still do not know how, so share this article for everyone's reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

environment

Client: CentOS8 192.168.43.137

Server: CentOS8 192.168.43.139

Create SSH public and private keys

Generate a new 4096-bit SSH key pair by typing:

[root@localhost ~]# ssh-keygen -t rsa -b 4096Generating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa):Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:SHA256:ycOtSDK8ud2kd6EH7OxoQuc1BFb1HJ3T/kvAQJt0LrI root@localhost.localdomainThe key's randomart image is:+---[RSA 4096]----+| ... oo.o o || o .+=.+ .|| . . . +=. o || . o.oo .o .|| + .oSE. . .|| .*..= o. ..|| .oo.+ o+ . . .|| .oo== o . || .o+ooo |+----[SHA256]-----+

To verify that a new SSH key pair has been generated, use ls -l to see if the ~/.ssh directory has the file you just generated:

[root@localhost ~]# ll ~/.ssh/total 8-rw------- 1 root root 3389 May 13 08:26 id_rsa-rw-r--r-- 1 root root 752 May 13 08:26 id_rsa.pub

To copy the public key to the remote server, use the ssh-copy-id utility and enter the root password for the remote server:

[root@localhost ~]# ssh-copy-id root@192.168.43.139/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"The authenticity of host '192.168.43.139 (192.168.43.139)' can't be established.ECDSA key fingerprint is SHA256:7O1oIOooh5NZG87aC3v1Zz/vcTXkjOhQBnlkY0CD4y0.Are you sure you want to continue connecting (yes/no)? yes/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keysPassword:Number of key(s) added: 1Now try logging into the machine, with: "ssh 'root@192.168.43.139'"and check to make sure that only the key(s) you wanted were added.

You can also copy the public key using the following command:

[root@localhost .ssh]# cat ~/.ssh/id_rsa.pub |ssh root@192.168.43.139 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys" Log in to the server using a key

Log in to the ssh server using the following command:

[root@localhost ~]# ssh 192.168.43.139Last login: Tue May 12 12:33:41 2020 from 192.168.43.137 Set SSH key in CentOS 8 Set SSH key in CentOS8 Close password authentication

Log in to the server and turn off password authentication:

[root@localhost ~]# ssh 192.168.43.139Last login: Tue May 12 12:33:41 2020 from 192.168.43.137[root@localhost ~]# vim /etc/ssh/sshd_config Search for three items and change the option to NoPasswordAuthentication noChallengeResponseAuthentication noUseAuthentication no PAM

Restart sshd service:

[root@localhost ~]# systemctl restart sshd The above is "How to set SSH key in CentOS8" All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Tags: Keys services servers commands passwords articles generation login public key content settings authentication input utility not much public and private can make most of the clients clients Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info Redmi Linux MySQL