내 로컬 컴퓨터에서 docker를 사용하여 SSH 랩 설정

내 로컬 컴퓨터에서 docker를 사용하여 SSH 랩 설정

docker ubuntu 컨테이너를 사용하여 로컬 컴퓨터에 SSH 랩을 설정하고 있습니다. 컨테이너를 대화형으로 시작하고 다른 도구를 사용하여 컨테이너에 open ssh를 설치했습니다. 컨테이너: 22를 내 로컬 호스트: 2222로 전달했습니다.

docker pull ubuntu
docker run -it --name ubunut-ssh-lab -p 2222:22 ubuntu /bin/bash
apt-get update
apt-get install -y openssh-server

그런 다음 sshd_config에서 PubKeyAuth를 yes로 활성화하고 로컬 시스템의 pub 키를 컨테이너 디렉터리 .ssh/authroized_keys 파일에 복사했습니다. 서비스가 다시 시작되었습니다. 나는 아직도 얻는다root@localhost: 권한이 거부되었습니다(공개 키).

-v(verbose) 인수를 사용하여 ssh 연결을 실행합니다.

ssh -v -i id_ubuntu_lab -p 2222 root@localhost  
OpenSSH_9.3p2, LibreSSL 3.3.6
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: include /etc/ssh/ssh_config.d/* matched no files
debug1: /etc/ssh/ssh_config line 54: Applying options for *
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to localhost port 2222.
debug1: Connection established.
debug1: identity file id_ubuntu_lab type 0
debug1: identity file id_ubuntu_lab-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_9.3
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.9p1 Ubuntu-3ubuntu0.4
debug1: compat_banner: match: OpenSSH_8.9p1 Ubuntu-3ubuntu0.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to localhost:2222 as 'root'
debug1: load_hostkeys: fopen /Users/sourabhdhingra/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: [email protected]
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:QanR0+0tbO3ombtQ17EvYU/yUoWTXJtBdZz7pPRHD7U
debug1: load_hostkeys: fopen /Users/sourabhdhingra/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: checking without port identifier
debug1: load_hostkeys: fopen /Users/sourabhdhingra/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: hostkeys_find_by_key_hostfile: hostkeys file /Users/sourabhdhingra/.ssh/known_hosts2 does not exist
debug1: hostkeys_find_by_key_hostfile: hostkeys file /etc/ssh/ssh_known_hosts does not exist
debug1: hostkeys_find_by_key_hostfile: hostkeys file /etc/ssh/ssh_known_hosts2 does not exist
The authenticity of host '[localhost]:2222 ([::1]:2222)' can't be established.
ED25519 key fingerprint is SHA256:QanR0+0tbO3ombtQ17EvYU/yUoWTXJtBdZz7pPRHD7U.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[localhost]:2222' (ED25519) to the list of known hosts.
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: get_agent_identities: bound agent to hostkey
debug1: get_agent_identities: ssh_fetch_identitylist: agent contains no identities
debug1: Will attempt key: id_ubuntu_lab RSA SHA256:XghbmDcG+wgAFNV4/BdCxjwRtsnlBsmq9BiKmxEj5hU explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,[email protected],ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected]>
debug1: kex_input_ext_info: [email protected]=<0>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: id_ubuntu_lab RSA SHA256:XghbmDcG+wgAFNV4/BdCxjwRtsnlBsmq9BiKmxEj5hU explicit
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
root@localhost: Permission denied (publickey).

위의 로그와 오류가 발생했습니다! 분석을 위해 여기에 sshd_config 파일을 첨부합니다. 여기서 무엇이 잘못되었는지 확인하고 도와주세요.

PermitRootLogin without-password
PubkeyAuthentication yes
AuthorizedKeysFile  .ssh/authorized_keys .ssh/authorized_keys2
PasswordAuthentication no
PermitEmptyPasswords yes

이것이 출력이다ls -la

drwxr-xr-x 2 root root    4096 Oct 22 13:54 .
drwxr-xr-x 1 root root    4096 Oct 22 13:44 ..
-rw------- 1  501 dialout  602 Oct 22 13:54 authorized_keys

참고: sshd_config에는 주석 처리되지 않은 줄만 포함했습니다.

여기서 문제가 무엇입니까? 연결할 수 없습니다?

답변1

/rootdocker Ubuntu 컨테이너를 사용하는 경우 이 디렉터리는 기본적으로 이미지에 존재하지 않을 수 있습니다.

이 경우 cd ~docker를 사용하여 대화형 세션을 시도하면 다음과 같은 결과가 나타납니다.

root@4e56fee1ea11:/# cd ~
bash: cd: /root: No such file or directory

그러니 추가 조치를 취해주세요!

mkdir /root
mv .ssh /root
service ssh restart

그런 다음 로컬 컴퓨터에서 로그인하십시오.

ssh -p 2222 root@localhost

당신의 기여에 감사드립니다!

관련 정보