Docker - Ubuntu SSH Setup
Luke 我什麼都不會

Docker - Ubuntu SSH Setup

Host: Use the same port -> just change to the SSH port that does not conflict with NAS.

Environment Settings for Ubuntu Container

Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
FROM ubuntu:latest
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:your_password' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

# Other installations
RUN apt-get upgrade -y
RUN apt-get install -y sudo nano vim curl git

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

Docker Build and Push to Docker Hub

1
2
3
docker build -t ubuntu_ssh .
docker tag ubuntu_ssh wulukewu/ubuntu_ssh:latest
docker push wulukewu/ubuntu_ssh:latest

Install OpenSSH-Server

1
2
3
4
5
6
7
apt-get update 
apt-get install -y openssh-server
mkdir /var/run/sshd
echo 'root:your_password' | chpasswd
sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
/usr/sbin/sshd -D

Pull and Run the ubuntu_ssh Image

Pull ubuntu_ssh from Docker Hub

1
sudo docker pull wulukewu/ubuntu_ssh:latest

Run ubuntu_ssh in Host

1
sudo docker run -d --net=host --name ubuntu_ssh wulukewu/ubuntu_ssh:latest

Access the Ubuntu Container

1
sudo docker exec -i -t ubuntu_ssh /bin/bash

Hostname Configuration

Change Hostname

1
2
3
sudo nano /etc/hostname
sudo nano /etc/hosts
sudo reboot

Display the Current Hostname

1
hostname

SSH Configuration

Change SSH Port

1
2
sudo nano /etc/ssh/sshd_config
service ssh restart

User Management

Add a New Sudo User

1
adduser user_name

Add the User to the Sudo Group

1
2
usermod -aG sudo user_name
usermod -aG root user_name

Change User Password

1
sudo passwd user_name

Test Sudo Access

1
su - user_name

References

Powered by Hexo & Theme Keep