红茶的个人站点

  • 首页
  • 专栏
  • 开发工具
  • 其它
  • 隐私政策
Awalon
Talk is cheap,show me the code.
  1. 首页
  2. 开发工具
  3. 正文

使用 SSH 连接 Ubuntu 服务器

2025年8月8日 11点热度 0人点赞 0条评论

安装 SSH 服务

更新源并安装服务端:

sudo apt update && sudo apt install openssh-server -y

启动服务:

sudo systemctl enable --now ssh
sudo systemctl status ssh

开启防火墙的 22 端口:

sudo ufw allow ssh 
sudo ufw enable 
sudo ufw status

查看 Ubuntu 服务器的 ip:

ifconfig

修改 ssh 配置:

sudo vim /etc/ssh/sshd_config

修改其中的选项:

PubkeyAuthentication yes                    # 启用公钥认证
AuthorizedKeysFile .ssh/authorized_keys     # 指定公钥文件路径
PermitRootLogin yes                         # 允许 root 登录(若需 root 连接)
PasswordAuthentication no                   # 可选:禁用密码登录(增强安全)

重启服务:

sudo systemctl restart ssh

配置密钥

生成 RSA 密钥对,在服务端或客户端都可以实现这一步,建议在客户端实现,然后拷贝公钥到服务端。

Linux 环境可以通过以下命令生成密钥对:

ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -N ""

Windows 环境可以使用 Putty 工具生成,具体可以看这篇文章。

如果是 Windows 客户端,可能还需要转换一个 pem 私钥,同样可以通过 Putty 工具实现。

有了密钥对后,将公钥文件(*.pub)拷贝到服务端,然后通过以下命令添加到授权列表:

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

这里假设公钥文件名称为 id_rsa.pub。

修改相关目录权限:

chmod 700 ~/.ssh 
chmod 600 ~/.ssh/authorized_keys

现在就可以在客户端通过 ssh 工具使用私钥连接服务端了。

参考资料

  • 使用 RSA 密钥进行 SSH 连接 - 红茶的个人站点

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: Linux SSH
最后更新:2025年8月8日

魔芋红茶

加一点PHP,加一点Go,加一点Python......

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2021 icexmoon.cn. ALL RIGHTS RESERVED.
本网站由提供CDN加速/云存储服务

Theme Kratos Made By Seaton Jiang

宁ICP备2021001508号

宁公网安备64040202000141号