Skip to content

Instantly share code, notes, and snippets.

@lenchevsky
Created February 2, 2017 20:14
Show Gist options
  • Save lenchevsky/7eba11bd491e70105de3600ec9ec1292 to your computer and use it in GitHub Desktop.
Save lenchevsky/7eba11bd491e70105de3600ec9ec1292 to your computer and use it in GitHub Desktop.
Dockerfile for CentOS 7 with enabled systemd, sshd and deployed ssh keys
FROM centos/systemd
MAINTAINER Oleg Snegirev <[email protected]>
# Install packages
RUN yum -y install openssh-server sudo nano epel-release openssl certmonger; systemctl enable sshd.service
# Enable root and pos accounts
RUN echo 'root:33103255235331325230' | chpasswd
RUN adduser pos && \
echo 'pos:ol2432sn324231024113310' | chpasswd && \
usermod -aG wheel fmpos
# Configure SSHD
RUN mkdir -p /var/run/sshd ; chmod -rx /var/run/sshd
# http://stackoverflow.com/questions/2419412/ssh-connection-stop-at-debug1-ssh2-msg-kexinit-sent
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
# Bad security, add a user and sudo instead!
RUN sed -ri 's/#PermitRootLogin yes/PermitRootLogin yes/g' /etc/ssh/sshd_config
# http://stackoverflow.com/questions/18173889/cannot-access-centos-sshd-on-docker
RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config
RUN sed -ri 's/#UsePAM no/UsePAM no/g' /etc/ssh/sshd_config
# Deploy ssh keys
RUN mkdir /root/.ssh/ && \
echo "ssh-rsa AAAAB3Nz4........l9Ns5p989oHLcSGJ" > ~/.ssh/authorized_keys && \
chmod 700 ~/.ssh && \
chmod 600 ~/.ssh/authorized_keys
RUN mkdir /home/pos/.ssh/ && \
echo "ssh-rsa AAAAB3NzaC........9Ns5p989oHLcSGJ" > /home/pos/.ssh/authorized_keys && \
chmod 700 /home/pos/.ssh && \
chmod 600 /home/pos/.ssh/authorized_keys && \
chown -R pos:pos /home/pos/.ssh/
# Configure pos
RUN bash -c 'echo "pos ALL=(ALL:ALL) NOPASSWD: ALL" | (EDITOR="tee -a" visudo)'
EXPOSE 22
EXPOSE 3306
EXPOSE 8080
CMD ["/usr/sbin/init"]
@Ojvar
Copy link

Ojvar commented Dec 14, 2022

Line 12 should has a tiny bug :)

Use fmpos was not defined ;)
usermod -aG wheel fmpos -> usermod -aG wheel pos

Thanks

@asmkaiser
Copy link

asmkaiser commented Mar 28, 2024

Wanted to start sshd service by default , but seems like none of the services I wanted started during the docker started.

So I encountered following error

[root@3ce21840a428 /]# systemctl --user daemon-reload
**Failed to get D-Bus connection: Connection refused**
[root@3ce21840a428 /]# 
[root@3ce21840a428 /]# systemctl status dbus
**Failed to get D-Bus connection: No such file or directory**
[root@3ce21840a428 /]# 
[root@3ce21840a428 /]# systemctl status dbus.service
Failed to get D-Bus connection: No such file or directory

The following steps helped me to start the sshd service.

[root@3ce21840a428 /]# yum install dbus
[root@3ce21840a428 /]# systemctl daemon-reload
[root@3ce21840a428 /]# systemctl start sshd`

so I re-edited the containerfile as follows:

FROM centos/systemd

LABEL Maintainer="A S M KAWSAR HARUN"

ENV HTTP_PROXY=http://135.245.48.34:8000
ENV HTTPS_PROXY=http://135.245.48.34:8000


RUN echo "proxy=http://135.245.48.34:8000" >> /etc/yum.conf
RUN yum -y install openssh-server selinux-policy sudo nano epel-release openssl certmonger net-tools initscripts **dbus**
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_dsa_key -N ''
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_ed25519_key -N ''

RUN systemctl enable sshd.service
RUN mkdir -p /var/run/sshd ; chmod -rx /var/run/sshd

RUN sed -ri 's/#PermitRootLogin yes/PermitRootLogin yes/g' /etc/ssh/sshd_config
RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config
RUN sed -ri 's/#UsePAM no/UsePAM no/g' /etc/ssh/sshd_config

RUN systemctl enable sshd.service

EXPOSE 22
COPY .ssh/id_rsa.pub /root/.ssh/authorized_keys
RUN chmod 600 /etc/ssh/ssh_host* /root/.ssh/authorized_keys
RUN nohup /usr/sbin/sshd &
CMD ["/usr/sbin/init"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment