gpt4 book ai didi

docker - 在 docker 容器中执行真正的 SSH

转载 作者:行者123 更新时间:2023-12-01 00:29:38 25 4
gpt4 key购买 nike

我正在尝试使用本指南:

https://docs.docker.com/engine/examples/running_ssh_service/#run-a-test_sshd-container

要对正在运行的容器执行普通 SSH,首先尝试获取运行容器的 ip 地址:

$ sudo docker inspect 4b928aca09ae

"NetworkSettings": {
"Bridge": "",
"SandboxID": "74ce28dd93a6fa6158ebc3f4ec73f1dba74ddbfc183f4ebf9cca77ced2571174",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"22/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "32768"
}
]
},
"SandboxKey": "/var/run/docker/netns/74ce28dd93a6",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "5927380d426ce9d5684dc17982c7f01af38d850218aa476d6ed8f457c8a39165",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.8",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:08",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "9c02e185366bb205bded7cab97766cb0e91315a4317affedc98f299831d5ceae",
"EndpointID": "5927380d426ce9d5684dc17982c7f01af38d850218aa476d6ed8f457c8a39165",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.8",
"IPPrefixLen": 16,

基于上述,我试图:
$ ssh root@172.17.0.1 -p 32768
root@172.17.0.1's password:
Permission denied, please try again.
root@172.17.0.1's password:

和:
$ ssh root@172.17.0.8 -p 32768
ssh: connect to host 172.17.0.8 port 32768: Connection refused

和:
$ ssh root@172.17.0.8
root@172.17.0.8's password:
Permission denied, please try again.
root@172.17.0.8's password:

和:
$ ssh root@localhost -p 32768
root@localhost's password:
Permission denied, please try again.
root@localhost's password:

使用指南中的密码: screencast但它不起作用。

还验证了它正在运行:
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b928aca09ae eg_sshd "/usr/sbin/sshd -D" 3 days ago Up 33 minutes 0.0.0.0:32768->22/tcp test_sshd

如果我使用 SSH 连接到容器:
$ sudo docker exec -i -t 4b928aca09ae /bin/bash

相反,root 用户似乎没有 .ssh 文件夹:
root@4b928aca09ae:~# pwd
/root
root@4b928aca09ae:~# ls -la
total 20
drwx------ 2 root root 4096 Nov 30 22:01 .
drwxr-xr-x 49 root root 4096 Nov 30 22:01 ..
-rw------- 1 root root 46 Nov 30 22:01 .bash_history
-rw-r--r-- 1 root root 3106 Oct 22 2015 .bashrc
-rw-r--r-- 1 root root 148 Aug 17 2015 .profile

我也尝试运行:
root@4b928aca09ae:~# ps -A | grep sshd
1 ? 00:00:00 sshd

虽然不太确定该怎么做。

有什么建议?

最佳答案

您需要使用主机 IP 地址而不是 docker ip,因为 -p 32768 是映射端口,它不在容器内运行。所以使用 ssh root@hostip -p 映射端口

关于docker - 在 docker 容器中执行真正的 SSH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47518011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com