Ubuntu VPS 配置 SSH 服务的详细指南
卡尔云官网
www.kaeryun.com
在为 Ubuntu 虚拟机(VPS)配置 SSH 服务时,确保 SSH 正常工作对于远程访问和管理是非常重要的,以下是详细的配置步骤,帮助你顺利设置 up-to-date 的 SSH 服务。
步骤 1:启用 SSH 服务
确保 SSH 服务在系统中启用,在终端中输入以下命令:
sudo systemctl enable ssh
如果系统提示你确认是否要禁用某个服务,请输入 y
确认。
步骤 2:检查 SSH 端口配置
默认情况下,SSH 服务使用端口 22 连接,检查防火墙规则以确保端口 22 是开放的,在 Ubuntu 中,可以使用以下命令查看防火墙规则:
sudo service iptables -L -n
确保SSH服务的端口未被禁用,如果被禁用,可以添加以下规则:
sudo iptables -t nat -A POSTROUTING -o 127.0.0.1/0 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT sudo iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT
然后重启防火墙:
sudo service iptables save sudo service iptables restart
步骤 3:生成 SSH 公钥
生成一个 SSH 公钥,在终端中输入:
sudo ssh-keygen -t rsa -b 4096
按回车键,系统会提示你选择公钥的存储位置,选择 ~/.ssh/id_rsa.pub
,然后输入密码。
生成的公钥会自动保存到指定路径。
步骤 4:配置 SSH 公钥文件
在 Ubuntu 中,SSH 服务使用用户的公钥文件来验证身份,将公钥文件设置为系统默认的公钥文件路径:
sudo nano ~/.ssh/config
并替换为你的公钥路径:
[ssh] ... HostKeyChecking off
[ssh] ... KeyFile ~/.ssh/id_rsa.pub
保存并退出。
步骤 5:启用 SSH 代理
为了提高连接速度,启用 SSH 代理,在终端中输入:
sudo ssh-copy-id ~/.ssh/id_rsa sudo nano ~/.ssh/config
在 ssh-copy-id
的默认路径下,添加以下内容:
[ssh-copy-id] ... ForwardAgent yes Userpass Key
保存并退出。
步骤 6:配置 Web 服务器(Apache)
确保 Web 服务器(如 Apache)能够信任 SSH 的证书,在 Apache 的 wwwroot.conf
文件中添加以下内容:
<include> /etc/ssl/certs/* .cer /etc/ssl/certs/* .crt /etc/ssl/certs/keys/* .key </include> <define> ssl.keauthash off ssl.keauthost off ssl.keauthello off ssl.keauthello-timeout 30 ssl.keauthello-maxtries 3 ssl.keauthello-mode off ssl.keauthello-file /dev/null ssl.keauthello-file-rotate 0 ssl.keauthello-file-size 4096 </define>
步骤 7:验证 SSH 连接
你可以从 VPS 的控制面板访问 SSH 服务,在控制面板中,找到“SSH”选项,输入你的公钥路径:
ssh -i ~/.ssh/id_rsa.pub user@vps_name
如果连接成功,SSH 服务已经配置好。
通过以上步骤,你已经成功配置了 Ubuntu VPS 的 SSH 服务,确保每一步都仔细检查,以避免配置错误,如果连接失败,请检查防火墙规则、公钥路径和配置文件是否正确。
卡尔云官网
www.kaeryun.com