文章目录
前言
通过ssh连接CT容器
一、安装了SSH 服务
首先,进入容器并安装 OpenSSH
服务:
# 进入容器
pct enter <container_id>
# 在容器中安装 OpenSSH 服务
apt-get update
apt-get install openssh-server
二、启动 SSH 服务
安装完成后,启动 SSH 服务并设置其开机自启:
systemctl start ssh
systemctl enable ssh
三、检查容器的网络连接
确保容器的网络配置正确,并能访问你要从中进行 SSH 连接的设备。如果容器使用 DHCP 分配 IP,可以使用以下命令查看容器的 IP 地址:
ip addr show
四、配置防火墙
如果容器内部或 PVE 主机上启用了防火墙,确保 SSH 端口(通常是 22)已经打开。你可以使用 ufw
或 iptables
来配置防火墙规则。
五、通过 SSH 连接到容器
在你的工作站或 PVE 主机上,使用 SSH 连接到容器:
ssh <username>@<container_ip>
其中 <username>
是你在容器内要登录的用户名,<container_ip>
是容器的 IP 地址。
示例
假设容器的 IP 是 192.168.1.101
,并且你要用 root 用户登录,那么在你的终端中运行:
ssh root@192.168.1.101
如果配置正确,你将被要求输入密码,然后你就能进入容器的 SSH 会话了。
六、连接出错
Starting a new connection to: "10.8.0.12" port "22"
⚙️ Starting address resolution of "10.8.0.12"
⚙️ Address resolution finished
⚙️ Connecting to "10.8.0.12" port "22"
👤 Connection to "10.8.0.12" established
⚙️ Starting SSH session
⚙️ Remote server: SSH-2.0-OpenSSH_9.2p1 Debian-2+deb12u3
⚙️ Agreed KEX algorithm: ecdh-sha2-nistp256
⚙️ Agreed Host Key algorithm: ecdsa-sha2-nistp256
⚙️ Agreed server-to-client cipher: aes128-ctr MAC: hmac-sha2-256
⚙️ Agreed client-to-server cipher: aes128-ctr MAC: hmac-sha2-256
⚙️ Handshake finished
👤 Checking host key: d3:52:e7:6e:9b:c2:4e:fe:fb:65:c4:d7:42:67:10:bd
👤 Host "10.8.0.12":"22" is known and matches
👤 Authenticating to "10.8.0.12":"22" as "root"
❗ Disabling authentication using publickey: Private Key is empty
⚙️ Available client authentication methods: password,keyboard-interactive
⚙️ Authentication that can continue: publickey,password
👤 Authenticating using password method
❗ Authentication failed (password)
⚙️ Partial success: no
⚙️ Authentication that can continue: publickey,password
👤 Authenticating using password method
❗ Authentication failed (password)
⚙️ Partial success: no
⚙️ Authentication that can continue: publickey,password
👤 Authenticating using password method
❗ Authentication failed (password)
⚙️ Partial success: no
⚙️ Authentication that can continue: publickey,password
😨 No more authentication methods to try
解决:
SSH 登录到 CT 容器时遇到密码认证失败的问题,可能有以下几种原因和解决方法:
1. 确认密码正确性
-
确保你输入的密码是正确的。如果你不记得密码,可以尝试在容器内通过 PVE 主机来重置 root 密码:
pct enter <container_id> passwd
然后设置一个新的 root 密码。
2. SSH 配置文件问题
-
检查容器内的
/etc/ssh/sshd_config
文件,确保PermitRootLogin
设置为yes
。如果此项设置为no
或prohibit-password
,将导致 root 用户不能通过密码登录。PermitRootLogin yes
修改后,重启 SSH 服务:
systemctl restart ssh
3. 防火墙配置
- 确保没有防火墙规则阻止 SSH 连接。如果容器或 PVE 主机上有防火墙配置,确保端口 22 是开放的。
4. 重试连接
- 重新尝试连接时,确保使用正确的 IP 地址和端口号。
5. 禁用公钥认证
-
如果你仅使用密码认证,可以暂时禁用公钥认证以排除问题。在 SSH 配置文件
/etc/ssh/sshd_config
中设置:PubkeyAuthentication no PasswordAuthentication yes
然后重启 SSH 服务。
6. 日志检查
-
在 PVE 主机或容器内检查 SSH 登录失败的日志,查看是否有更多错误信息:
journalctl -u ssh
7. 更新 OpenVPN 配置
- 如果你是通过 OpenVPN 连接到容器,确保 OpenVPN 配置没有问题,并且 VPN 网络正确配置。
通过这些步骤,你可以排查并解决 SSH 登录失败的问题。
总结
通过ssh连接CT容器