数据人成长交流的一站式服务平台

网站首页 > 数据编程 正文

SSH密钥方式登陆服务器配置说明 ssh密钥认证免密登录

nandi88 2024-10-13 07:03:18 数据编程 6 ℃

前言

1、如何不用密码登陆服务器呢,答案是密钥,公钥放在服务器上,私钥放在客户端,实现安全免密码连接

2、本文以Ubunt uServer 20.04 服务器10.0.100.11root账户为例

一、服务器上,启用SSH服务

Ubuntu自带有openssh-client,仅需安装服务端

1、安装openssh-server软件包

sudo apt-get update
sudo apt-get install openssh-server

2、验证 SSH 是否正在运行

#安装完成后,SSH 服务将会自动启动
sudo systemctl status ssh

显示绿色active(running)表示运行正常

3、打开 SSH 端口

sudo ufw allow 22

二、客户端,开启SSH密钥登陆

1、检查现有的SSH密钥对

ls -al ~/.ssh/id_*.pub
#如果存在现有密钥,则可以使用这些密钥并跳过下一步,或者备份旧密钥并生成一个新密钥。

2、生成一个新的SSH密钥对

#邮件地址更换为你自己的
ssh-keygen -t rsa -b 4096 -C "your_email@domain.com"
#按下Enter以接受默认文件位置和文件名:
Enter file in which to save the key (/home/yourusername/.ssh/id_rsa):

ssh-keygen工具将要求您输入安全密码。是否要使用密码取决于您自己,如果您选择使用密码,您将获得额外的安全保护

在大多数情况下,开发人员和系统管理员在不使用密码的情况下使用SSH,因为它们对于全自动流程很有用

#输入安全密码,如果您不想使用密码,可直接按Enter
Enter passphrase (empty for no passphrase):
#验证生成了SSH密钥
ls ~/.ssh/id_*
#结果应如下
/root/.ssh/id_rsa  /root/.ssh/id_rsa.pub

3、复制公钥到服务器

#最简单方法是使用命令ssh-copy-id
ssh-copy-id root@10.0.100.11
#输入服务器root密码
root@10.0.100.11's password:

4、SSH连接服务器

ssh root@10.0.100.11
#连接过程如下,是否继续选yes,之后要求输入服务器root密码
The authenticity of host '10.0.100.11 (10.0.100.11)' can't be established.
ECDSA key fingerprint is SHA256:P4Fl2smA/h+p..
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

Warning: Permanently added '10.0.100.11' (ECDSA) to the list of known hosts.
root@10.0.100.11's password: 
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-65-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Sun 31 Jan 2021 02:48:08 AM UTC

  System load:  0.0                Processes:               128
  Usage of /:   18.3% of 39.12GB   Users logged in:         1
  Memory usage: 5%                 IPv4 address for enp4s1: 10.0.100.11
  Swap usage:   0%

 * Introducing self-healing high availability clusters in MicroK8s.
   Simple, hardened, Kubernetes for production, from RaspberryPi to DC.

     https://microk8s.io/high-availability

21 updates can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable

Last login: Sun Jan 31 02:42:33 2021 from 192.168.11.5

三、服务器,禁用SSH密码登陆

如果需要为服务器提升安全性,在确保您可以不使用密码登录服务器后,可以禁用SSH的密码身份验证。

1、修改SSH配置文件

vi /etc/ssh/sshd_config
#搜索以下指令并进行如下修改
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no

2、保存文件并重启SSH服务

sudo systemctl restart ssh
最近发表
标签列表