Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
 

CentOS 7 컨테이너 내에서 openssh 서버 호스팅

지난 글에서 CentOS 7의 yum repository를 변경한 후 openssh 패키지를 설치했는데요,

CentOS 7에서 yum 사용 시 "Could not resolve host: mirrorlist.centos.org; Unknown error"
; https://www.sysnet.pe.kr/2/0/13981

$ cat centos7.repo
[base]
name=CentOS-$releasever - Base
# ...[생략: https://www.sysnet.pe.kr/2/0/13981#repo]... 

$ cat centos7.dockerfile
FROM centos:7

COPY ./centos7.repo /etc/yum.repos.d/CentOS-Base.repo

RUN yum install openssh-server -y

그런데 서비스 관련 명령을 내렸더니 이런 오류가 발생합니다.

# systemctl start sshd
Failed to get D-Bus connection: Operation not permitted

찾아보면, 뭔가 꽤나 복잡한 설정으로 --privileged 옵션까지 넣어가며 systemd를 활성화해야 하는 것 같습니다.

failed to get D-Bus connection: Operation not permitted
; https://serverfault.com/questions/824975/failed-to-get-d-bus-connection-operation-not-permitted

저렇게 하느니, 그냥 고전적으로 sshd 데몬을 실행하는 방법으로 변경하는 것도 나쁘지 않겠는데요, 따라서, 다음과 같이 Dockerfile을 보완하면 됩니다.

$ cat centos7.dockerfile
FROM centos:7

COPY ./centos7.repo /etc/yum.repos.d/CentOS-Base.repo

# 테스트를 위해 net-tools 포함
RUN yum install openssh-server net-tools -y
RUN ssh-keygen -A

EXPOSE 22

# 아래의 주석을 해제하면 sshd가 컨테이너 시작 시 자동으로 실행
# CMD ["/usr/sbin/sshd", "-D"]

컨테이너를 만들고 sshd를 직접 실행한 후,

$ docker build -f ./centos7.dockerfile . -t centos7_dotnet_img

$ docker run --rm --name centos7_dotnet_test -it -p 15022:22 centos7_dotnet_img /bin/bash
# /usr/sbin/sshd
# netstate -ano
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       Timer
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp6       0      0 :::22                   :::*                    LISTEN      off (0.00/0/0)
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
#

ssh 클라이언트로 docker 호스트 측의 포워딩된 포트로 접속하면 끝!

$ ssh root@192.168.100.50 -p 15022




그런데, 저렇게 하면 "ssh-keygen -A" 명령어로 인해 서버 키가 매번 새롭게 생성되므로 ssh 클라이언트는 그때마다 fingerprint를 등록해야 합니다. 처음 한번은 StrictHostKeyChecking 옵션을 통해 피할 수 있지만,

$ ssh -o StrictHostKeyChecking=accept-new root@192.168.100.50 -p 15022

이후 다시 서버 키가 바뀌면, 이런 식의 오류와 함께 접속이 거부됩니다.

$ ssh -o StrictHostKeyChecking=accept-new root@192.168.100.50 -p 15022
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ED25519 key sent by the remote host is
SHA256:G5b...[생략]...JCg.
Please contact your system administrator.
Add correct host key in C:\\Users\\testusr/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in C:\\Users\\testusr/.ssh/known_hosts:67
Host key for [192.168.100.50]:15022 has changed and you have requested strict checking.
Host key verification failed.

아마 저걸 피하는 옵션도 있을지 모르지만... 어쨌든, 그냥 컨테이너 측의 키를 고정시키는 것도 좋은 방법입니다. 방법은, 이미 "ssh-keygen -A" 명령으로 생성한 키 관련 파일들이 /etc/ssh에 있으므로, 이걸 호스트로 복사해,

$ docker cp centos7_dotnet_test:/etc/ssh .

$ ls -l ./ssh
total 616
-rw-r--r-- 1 testusr testusr 581843 Apr 11  2018 moduli
-rw------- 1 testusr testusr   3907 Apr 11  2018 sshd_config
-rw------- 1 testusr testusr    668 May 30 13:49 ssh_host_dsa_key
-rw-r--r-- 1 testusr testusr    610 May 30 13:49 ssh_host_dsa_key.pub
-rw------- 1 testusr testusr    227 May 30 13:49 ssh_host_ecdsa_key
-rw-r--r-- 1 testusr testusr    182 May 30 13:49 ssh_host_ecdsa_key.pub
-rw------- 1 testusr testusr    411 May 30 13:49 ssh_host_ed25519_key
-rw-r--r-- 1 testusr testusr    102 May 30 13:49 ssh_host_ed25519_key.pub
-rw------- 1 testusr testusr    985 May 30 13:49 ssh_host_key
-rw-r--r-- 1 testusr testusr    650 May 30 13:49 ssh_host_key.pub
-rw------- 1 testusr testusr   1679 May 30 13:49 ssh_host_rsa_key
-rw-r--r-- 1 testusr testusr    402 May 30 13:49 ssh_host_rsa_key.pub

$ rm ./ssh/moduli
$ rm ./ssh/sshd_config

키 이외의 파일은 제거하고, 다음부터 저 파일들을 재사용하도록 Dockerfile을 수정하면 됩니다.

$ cat centos7.dockerfile
FROM centos:7

COPY ./centos7.repo /etc/yum.repos.d/CentOS-Base.repo
COPY ./ssh /etc/ssh

RUN yum install openssh-server -y
# RUN ssh-keygen -A

EXPOSE 22

# CMD ["/usr/sbin/sshd", "-D"]




그런데, 아직 문제가 하나 더 있습니다. 클라이언트 측에서 ssh 접속을 시도하면 사용자 로그인을 해야 하는데요, 기본적으로 Docker 컨테이너는 root 계정만 존재합니다. 별도 사용자를 추가하는 것은 번거로우니 root 계정을 활용해야 하는데요, 따라서 암호를 고정할 필요가 있습니다.

이를 위해 root 계정의 암호를 명시적으로 설정하는 단계를 추가해, 최종적으로 다음과 같은 Dockerfile로 정리됩니다.

$ cat centos7.dockerfile
FROM centos:7

COPY ./centos7.repo /etc/yum.repos.d/CentOS-Base.repo
COPY ./ssh /etc/ssh

RUN yum install openssh-server -y

RUN echo 'my_temp_pass' | passwd --stdin root
# ubuntu 계열에서는 이렇게 변경
# RUN echo 'root:my_temp_pass' | chpasswd

EXPOSE 22

CMD ["/usr/sbin/sshd", "-D"]




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 8/2/2025]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  [2]  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13958정성태6/21/20252498닷넷: 2339. C# - Phi-4-multimodal 모델의 GPU 가속 방법 (ORT 사용)파일 다운로드1
13957정성태6/20/20252943닷넷: 2338. C# / Foundry Local - Phi-4-multimodal 모델을 사용하는 방법 [1]
13956정성태6/19/20252700개발 환경 구성: 751. Triton Inference Server의 Python Backend 프로세스
13955정성태6/18/20252826오류 유형: 965. Hugging Face 모델 다운로드 시 "requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: ..." 오류
13954정성태6/18/20252143닷넷: 2337. C# - Hugging Face에 공개된 LLM 모델을 Foundry Local에서 사용하는 방법파일 다운로드1
13953정성태6/16/20252263스크립트: 78. 파이썬 - 소스 코드의 파일 경로를 지정한 모듈 로드
13952정성태6/15/20252619닷넷: 2336. C# - IValueTaskSource로 인해 주의가 필요한 ValueTask 호출파일 다운로드1
13951정성태6/15/20252608오류 유형: 964. Outlook - 일정이 "You cannot make changes to contents of this read-only folder." 오류 메시지로 삭제가 안 되는 경우
13950정성태6/12/20253449닷넷: 2335. C# - 간단하게 구현해 보는 IValueTaskSource 예제파일 다운로드1
13949정성태6/11/20253365오류 유형: 963. SignTool - "Error: SignerSign() failed." (-2146869243/0x80096005)
13948정성태6/10/20252355오류 유형: 962. 파이썬 - Linux 환경 + TCP 서버 소켓을 사용하는 프로세스 종료 후 재실행하는 경우 "OSError: [Errno 98] Address already in use" 오류 발생
13947정성태6/9/20253098개발 환경 구성: 750. 파이썬 - Azure App Service에 응용 프로그램 배포 후의 환경
13946정성태6/9/20252973개발 환경 구성: 749. 파이썬 - Azure App Service에 응용 프로그램 배포하기 전의 환경
13945정성태6/7/20252348오류 유형: 961. 파이썬 + conda - mysqlclient 사용 시 "NameError: name '_mysql' is not defined" 에러
13944정성태6/7/20256118오류 유형: 960. The trust relationship between this workstation and the primary domain failed. - 네 번째 이야기
13943정성태6/6/20252964개발 환경 구성: 748. Windows + Foundry Local - 로컬에서 AI 모델 활용 [1]
13942정성태6/5/20252538오류 유형: 959. winget 설치 시 "0x80d02002 : unknown error"
13941정성태6/2/20252197닷넷: 2334. C# - cpuid 명령어를 이용한 CPU 제조사 문자열 가져오기파일 다운로드1
13940정성태6/1/20252846C/C++: 188. C++의 32비트 + Release 어셈블리 코드를 .NET으로 포팅할 때 주의할 점파일 다운로드1
13939정성태5/29/20253355오류 유형: 958. NVIDIA Triton Inference Server - version `GLIBCXX_3.4.32' not found (required by /opt/tritonserver/backends/python/triton_python_backend_stub)
13938정성태5/29/20252512개발 환경 구성: 747. 파이썬 - WSL/docker에 구성한 Triton 예제 개발 환경
13937정성태5/24/20252662개발 환경 구성: 746. Windows + WSL2 환경에서 (tensorflow 등의) NVIDIA GPU 인식
13936정성태5/23/20252409개발 환경 구성: 745. Linux / WSL 환경에 Miniconda 설치하기
13935정성태5/20/20252319오류 유형: 957. 파이썬 - pip 사용 시 "ImportError: cannot import name 'html5lib' from 'pip._vendor'" 오류
13934정성태5/20/20253237스크립트: 77. 파이썬 - 'urllib.request' 모듈의 명시적/암시적 로딩 차이
13933정성태5/19/20252362오류 유형: 956. Visual Studio 2022가 17.12 버전부터 업데이트 되지 않는다면?
1  [2]  3  4  5  6  7  8  9  10  11  12  13  14  15  ...