惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
罗磊的独立博客
J
Java Code Geeks
人人都是产品经理
人人都是产品经理
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
Jina AI
Jina AI
F
Fortinet All Blogs
H
Help Net Security
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Last Week in AI
Last Week in AI
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
C
Check Point Blog
GbyAI
GbyAI

秋水逸冰

丽萨主机:双ISP家宽住宅IP VPS/CN2 GIA精品网/G口大带宽 | 秋水逸冰 Linux Kernel 6.18.x 编译版 | 秋水逸冰 思考未来人与 AI 的关系 | 秋水逸冰 3X-UI 面板安装和使用教程 | 秋水逸冰 Xray-UI 面板安装和使用教程 | 秋水逸冰 WCMP (Windows + Caddy + MariaDB + PHP) Nginx for Windows | 秋水逸冰 Aria2 for Windows | 秋水逸冰 Curl for Windows | 秋水逸冰 Wget for Windows | 秋水逸冰 Windows 11 Enterprise LTSC DD 镜像 Windows Server 2025 Datacenter DD 镜像 重新编译 curl 以支持 HTTP3 | 秋水逸冰 tiny10 和 tiny11 23H2 的 Windows DD 镜像 从 Apache httpd 切换到 Caddy2 LCMP (Linux + Caddy + MariaDB + PHP) Windows 11 Pro for Workstations 22H2 DD 镜像 Shadowsocks 编译版 by Teddysun | 秋水逸冰 适用于 UEFI 启动的多个 Windows 系统 DD 镜像 Linux Kernel 5.15.x 编译版 | 秋水逸冰 制作适用于 WSL 的任意 Linux 镜像 Windows 11 Pro for Workstations 21H2 DD 镜像 Windows 10 Enterprise LTSC DD 镜像 Linux Kernel 5.14.x 编译版 | 秋水逸冰 Linux Kernel 5.13.x 编译版 | 秋水逸冰 Windows Server 2022 Datacenter DD 镜像 在 Windows 10 上安装 Rocky Linux 系统 Linux Kernel 5.12.x 编译版 | 秋水逸冰 Linux Kernel 5.11.x 编译版 | 秋水逸冰 在 Windows 10 上安装 CentOS 系统
SSH无密码登录及putty设置 | 秋水逸冰
秋水逸冰 · 2013-07-12 · via 秋水逸冰

技术 秋水逸冰 46572浏览 18评论

Linux

  一般Linux的VPS比Windows的便宜,所以手上的几个VPS清一色Linux发行版CentOS系统。拿到root权限的账号,系统随机生成的密码比较复杂,一般我们为了方便记忆都会改成自己能记得住的,然而这是不安全的。但凡用到密码的地方都只是看似安全。所以,在开通了Linux系统的VPS后,我们有必要对SSH登录做一些基本的安全设置。
  这其中利用公钥和私钥就能实现SSH无密码登录。按照如下步骤操作:
1、生成公钥和私钥
  Linux系统中绝大部分的发行版都是用OpenSSH,所以生成公钥私钥的时候最好用ssh-keygen命令,如果用putty自带的PUTTYGEN.EXE生成会不兼容OpenSSH,从而会导致登录时出现server refused our key错误。

  用root登录后运行命令如下。

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):    ##直接回车默认路径
Created directory '/root/.ssh'.
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:
04:e1:93:92:95:ba:55:21:58:05:7d:57:58:92:32:d3 root@vpn
The key's randomart image is:
+--[ RSA 2048]----+
|     oB*o. ..=o  |
|    .+.+o = E.   |
|    o.+... =     |
|    ...o         |
|     o  S        |
|    .            |
|                 |
|                 |
|                 |
+-----------------+

在/root/.ssh/目录下生成了2个文件,id_rsa为私钥,id_rsa.pub为公钥。私钥自己下载到本地电脑妥善保存,公钥则可以任意公开。

2、导入公钥
运行命令如下。

cat /root/.ssh/id_rsa.pub >>  /root/.ssh/authorized_keys

3、更改SSH配置文件
修改SSH的配置文件/etc/ssh/sshd_config,找到下面3行:

#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile	.ssh/authorized_keys

将前面的#去掉后保存。
重启SSH服务,运行命令:
service sshd restart

4、制作用于putty的私钥
将VPS上的/root/.ssh/id_rsa下载到本地,利用PUTTYGEN.EXE转换为putty用的ppk文件。
keygen1

点击File,Load private key,导入/root/.ssh/id_rsa文件。成功后的图片如下所示:
keygen2

点击Save private key按钮,生成一个后缀为ppk的文件,比如起名为teddysun.ppk,这个文件就是已经制作好的用于putty无密码登录的私钥了,妥善保存。

5、配置putty
最简便的一个方法是创建一个桌面快捷方式,双击即可无密码登录到VPS。
找到putty.exe文件,然后右键创建桌面快捷方式,再到桌面上编辑这个快捷方式,在目标一栏中,修改参数如下。

"C:\Program Files\PUTTY\PUTTY.EXE" -i "D:\key\teddysun.ppk" [email protected]

修改为存放私钥的完整路径,将xxx.xxx.xxx.xxx替换为你的VPS的IP地址即可。

现在,你可以双击建好的putty快捷方式,看看是否已经可以无密码登录了。

6、关闭SSH密码登陆
在上述第5步验证OK后,出于安全需要就可以关闭SSH的密码登录。
修改SSH的配置文件/etc/ssh/sshd_config,找到下面1行:

PasswordAuthentication yes

修改为:

PasswordAuthentication no

重启SSH服务,运行命令:
service sshd restart

搞定,收工。至此,就只能用私钥登录到VPS了,安全性大大增强了。最后,顺便说一句,别跟我说私钥弄丢了怎么办(我都说了要妥善保存的了)。

转载请注明:秋水逸冰 » SSH无密码登录及putty设置