网站首页 > 博客文章 正文
由于最近总收到服务器正在被尝试暴力登录的短信,便下定决心改为秘钥登录了。
大致分三步。
背景介绍
用户:username
本地系统:macOS
服务器:CentOS
服务器账号:root
第一步、生成本地秘钥
1、输入下面命令然后三个回车就可以
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/username/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/username/.ssh/id_rsa.
Your public key has been saved in /Users/username/.ssh/id_rsa.pub.
一般会生成到当前用户的主目录
下面代表生成成功了
Your identification has been saved in /Users/username/.ssh/id_rsa.
Your public key has been saved in /Users/username/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Xxxxxxxxxxxxxxx
The key's randomart image i
xxxxxxxxxxxxxx
如果本地已经有了就得注意了,重新生成后,公钥和私钥会发生变化,导致登录不了服务器。
千万要注意。
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/username/.ssh/id_rsa):
/Users/username/.ssh/id_rsa already exists.
Overwrite (y/n)? n
2、这个时候直接使用cat命令查看公钥文件内容就可以了。
$ cat ~/.ssh/id_rsa.pub
xxxxxxxxxxxx
第二步、将本地的公钥放入到服务器
服务器上的文件路径为
# ll -ll /root/.ssh/authorized_keys
-rw------- 1 root root 1551 1月 18 14:19 /root/.ssh/authorized_keys
使用vim把第二步显示的公钥内容添加到文件后面
第三步、关闭密码登录并重启服务
1、别搞错,是sshd_config 不是ssh_config。多一个d字母的文件
$ls /etc/ssh/
moduli ssh_host_dsa_key ssh_host_ecdsa_key.pub ssh_host_rsa_key
ssh_config ssh_host_dsa_key.pub ssh_host_ed25519_key ssh_host_rsa_key.pub
sshd_config ssh_host_ecdsa_key
2、修改登录配置
PasswordAuthentication yes
修改为
PasswordAuthentication no
4、重启ssh的服务
$ systemctl restart sshd
5、非法登录验证
$ssh root@1.1.1.1(示例ip)
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
6、正常登陆
$ssh root@1.1.1.1(示例ip)
会进入到root目录
再也不用担心坏孩子尝试暴力破解了。
但一定要把本地的私钥在保存一份到别处备份,不然丢了后。服务器登录就得想别的办法解决了。比如通过云平台修改root密码,然后重启。或者找IDC吧。
如果本文对你有帮助,请转发,说不定你的朋友也需要它。
关注我,掌握更多编程技巧成倍提升工作效率。
猜你喜欢
- 2024-10-02 K8s 网络问题自己能解决吗?(k8s网络不稳定)
- 2024-10-02 重学计算机组成原理(七)- 程序无法同时在Linux和Windows下运行
- 2024-10-02 AOP编程_Android优雅权限框架(2)Demo完全解析
- 2024-10-02 微信小程序云数据库update更新数据为0问题解决
- 2024-10-02 su切换用户:su failed to execute /bin/bash;Permission denied
- 2024-10-02 大数据------安装错误集锦(详细解决教程)
- 2024-10-02 gradle4.1后not resolve com.android.tools.build:gradle:3.0.1
- 2024-10-02 纳税申报系统常见问题(纳税申报系统常见问题有哪些)
- 2024-10-02 mediaDevices.getUserMedia提示DOMException:Permission denied by
- 2024-10-02 终于有人把所有的Java异常处理方法给总结出来了
你 发表评论:
欢迎- 07-08Google Cloud Platform 加入支持 Docker 的容器引擎
- 07-08日本KDDI与Google Cloud 签署合作备忘录,共探AI未来
- 07-08美国Infoblox与Google Cloud合作推出云原生网络和安全解决方案
- 07-08GoogleCloud为Spanner数据库引入HDD层,将冷存储成本降低80%
- 07-08谷歌推出Cloud Dataproc,缩短集群启动时间
- 07-08Infovista与Google Cloud携手推进射频网络规划革新
- 07-08比利时Odoo与Google Cloud建立增强合作,扩大全球影响力
- 07-08BT 和 Google Cloud 通过 Global Fabric 加速 AI 网络
- 最近发表
-
- Google Cloud Platform 加入支持 Docker 的容器引擎
- 日本KDDI与Google Cloud 签署合作备忘录,共探AI未来
- 美国Infoblox与Google Cloud合作推出云原生网络和安全解决方案
- GoogleCloud为Spanner数据库引入HDD层,将冷存储成本降低80%
- 谷歌推出Cloud Dataproc,缩短集群启动时间
- Infovista与Google Cloud携手推进射频网络规划革新
- 比利时Odoo与Google Cloud建立增强合作,扩大全球影响力
- BT 和 Google Cloud 通过 Global Fabric 加速 AI 网络
- NCSA和Google Cloud合作开发AI驱动的网络防御系统,加强泰国网络空间的安全性
- SAP将在沙特阿拉伯 Google Cloud 上推出BTP服务
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- googlecloud (64)
- messagesource (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)