본문 바로가기

리눅스

도커 컨테이너 내에서 ssh 서버를 설정하는 방법

728x90

도커 컨테이너 내에서 ssh 서버를 설정하는 방법

Dockerfile 파일 작성

vim Dockerfile
# 베이스 이미지 사용
FROM ubuntu:22.04

# 메타데이터 설정
LABEL website="sangchul.kr"

# 환경 변수 설정
ARG DEBIAN_FRONTEND=noninteractive

ARG SSH_ROOT_PASSWORD=${SSH_ROOT_PASSWORD:-root}
ARG SSH_USER=${SSH_USER:-ubuntu}
ARG SSH_PASSWORD=${SSH_PASSWORD:-ubuntu}

ENV SSH_ROOT_PASSWORD=${SSH_ROOT_PASSWORD}
ENV SSH_USER=${SSH_USER}
ENV SSH_PASSWORD=${SSH_PASSWORD}
ENV TZ=Asia/Seoul

# 시간대 설정
RUN echo $TZ > /etc/timezone

# 패키지 소스 업데이트 및 필요한 패키지 설치
RUN sed -i 's/archive.ubuntu.com/mirror.kakao.com/g' /etc/apt/sources.list && \
    apt-get update -qq && \
    apt-get install -qq -y \
      apt-utils \    
      aptitude \
      curl \
      dnsutils \
      iputils-ping \
      net-tools \
      netcat \
      openssh-server \
      ssh \
      sudo \
      telnet \
      traceroute \
      vim && \
    apt-get clean -qq autoclean && \
    apt-get autoremove -qq --yes && \
    rm -rf /var/lib/apt/lists /var/lib/dpkg/info /tmp/* /var/tmp/*

# root 사용자 설정
RUN echo "root:$SSH_ROOT_PASSWORD" | chpasswd && \
    cp -rf /etc/skel/.bash* /root/. && \
    echo 'export PS1="\[\033[01;32m\]\u\[\e[m\]\[\033[01;32m\]@\[\e[m\]\[\033[01;32m\]\h\[\e[m\]:\[\033[01;34m\]\W\[\e[m\]$ "' >> ~/.bashrc && \
    ssh-keygen -A

# 추가 사용자 설정
RUN useradd -c "$SSH_USER" -m -d /home/$SSH_USER -s /bin/bash $SSH_USER && \
    usermod -aG sudo $SSH_USER && \
    echo "$SSH_USER:$SSH_PASSWORD" | chpasswd && \
    echo 'export PS1="\[\e[33m\]\u\[\e[m\]\[\e[37m\]@\[\e[m\]\[\e[34m\]\h\[\e[m\]:\[\033[01;31m\]\W\[\e[m\]$ "' >> /home/$SSH_USER/.bashrc && \
    echo "$SSH_USER ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

# SSH 서버 설정
RUN mkdir /var/run/sshd && \
    sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config && \
    sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/g' /etc/ssh/sshd_config && \
    sed -i 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config

# Expose SSH port
EXPOSE 22

# Start SSH server
CMD ["/usr/sbin/sshd", "-D"]
더보기

---

FROM ubuntu:22.04

LABEL website="sangchul.kr"

ARG DEBIAN_FRONTEND=noninteractive
ARG SSH_USER=${SSH_USER:-ubuntu}
ARG SSH_PASSWORD=${SSH_PASSWORD:-ubuntu}

ENV TZ=Asia/Seoul
ENV SSH_USER=${SSH_USER}
ENV SSH_PASSWORD=${SSH_PASSWORD}
ENV PS1A="\[\e[33m\]\u\[\e[m\]\[\e[37m\]@\[\e[m\]\[\e[34m\]\h\[\e[m\]:\[\033[01;31m\]\W\[\e[m\]$ "

RUN echo $TZ > /etc/timezone

RUN apt update \
    && apt upgrade -qq -y \
    && apt install -qq -y openssh-server \
        aptitude sudo ssh vim curl \
        net-tools iputils-ping traceroute netcat telnet dnsutils \
    && mkdir /var/run/sshd \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

USER root

RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config \
    && sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/g' /etc/ssh/sshd_config \
    && sed -i 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config

RUN mkdir -m 700 ~/.ssh \
    && echo 'PS1=$PS1A' >> ~/.bashrc \
    && echo 'export PS1="\[\e[33m\]\u\[\e[m\]\[\e[37m\]@\[\e[m\]\[\e[34m\]\h\[\e[m\]:\[\033[01;31m\]\W\[\e[m\]$ "' >> ~/.profile \
    && echo "alias ll='ls -alh'" >> ~/.bashrc \
    && echo "root:root" | chpasswd

RUN useradd -c "System Administrator" -m -d /home/$SSH_USER -s /bin/bash $SSH_USER \
    && usermod -aG sudo $SSH_USER \
    && echo 'PS1=$PS1A' >> /home/$SSH_USER/.bashrc \
    && echo 'export PS1="\[\e[33m\]\u\[\e[m\]\[\e[37m\]@\[\e[m\]\[\e[34m\]\h\[\e[m\]:\[\033[01;31m\]\W\[\e[m\]$ "' >> /home/$SSH_USER/.profile \
    && echo "alias ll='ls -alh'" >> /home/$SSH_USER/.bashrc \
    && mkdir -m 700 /home/$SSH_USER/.ssh \
    && chown $SSH_USER.$SSH_USER /home/$SSH_USER/.ssh \
    && echo "$SSH_USER ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers \
    && echo "$SSH_USER:$SSH_PASSWORD" | chpasswd

EXPOSE 22

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

---

이미지 빌드

docker build -t anti1346/ubuntu-sshd:22.04 .
docker build --tag anti1346/ubuntu2204:sshd --build-arg SSH_USER=ubuntu --build-arg SSH_PASSWORD=ubuntu .

도커 컨테이너 실행

docker run -d -p 2222:22 --name ssh-server anti1346/ubuntu-sshd:22.04

도커 컨테이너 접속

docker exec -it ssh-server bash

 

참고URL

- github : https://github.com/anti1346/ubuntu2204/tree/main/sshd

 

728x90