服务器配置 ssh 密钥登录

服务器配置 ssh 密钥登录

配置 服务器安全组策略,开放 ssh 22 端口,以 root 用户登录服务器。

配置 ssh key 登录

  1. ssh-keygen 生成公钥和私钥对

  2. 如果不需要其他设置,一直回车

  3. 可以在 ~/.ssh 目录下看到两个文件,即刚生成的私钥 id_rsa 和 公钥 id_rsa.pub(具体名称取决于你的命名)

  4. 将公钥文件上传到服务器

    ssh-copy-id -i ~/.ssh/id_rsa.pub song@ip

  5. 在本地的 ~/.ssh/config 文件中加入以下配置

    shell 复制代码
    Host test # 改为任意名 之后需要使用 ssh test 登陆服务器
      HostName ip # 服务器 ip
      User root # 用户名
      IdentityFile /Users/user/.ssh/id_rsa
  6. ssh root@ip 登录

其他机器登录

id_rsa 放置到 ~/.ssh 目录下,在 config 中加入配置,使用 ssh root@ip 登录

禁止 ssh 密码登录

shell 复制代码
sudo vi /etc/ssh/sshd_config

# 允许root登录,但是禁止root密码登录
PermitRootLogin prohibit-password

#禁用密码验证
PasswordAuthentication no
#启用密钥验证
PubkeyAuthentication yes

# 重启服务
service ssh restart

# 注意
RSAAuthentication yes 是初代ssh协议,大部分系统已经废除,不建议开启。
相关推荐
cuijiecheng20186 小时前
Linux下Beyond Compare过期
linux·运维·服务器
期待のcode7 小时前
前后端分离项目 Springboot+vue 在云服务器上的部署
服务器·vue.js·spring boot
AI 智能服务7 小时前
第6课__本地工具调用(文件操作)
服务器·人工智能·windows·php
草梅友仁7 小时前
墨梅博客 1.0.0 发布与更新 | 2026 年第 2 周草梅周报
github·ai编程·nuxt.js
周末程序猿9 小时前
杂谈|2025年总结
github
Controller-Inversion9 小时前
cdn协议
计算机网络·github
松涛和鸣10 小时前
49、智能电源箱项目技术栈解析
服务器·c语言·开发语言·http·html·php
凉、介10 小时前
SylixOS 中的 Unix Socket
服务器·c语言·笔记·学习·嵌入式·sylixos
RisunJan10 小时前
Linux命令-ipcs命令(报告进程间通信(IPC)设施状态的实用工具)
linux·运维·服务器