SoFunction
Updated on 2025-04-14

Docker pulls Ubantu and ssh connection method

docker pulls Ubantu and ssh connection

Pull

docker pull ubuntu:22.04

Enter the container

First, make sure your container is running. If the container has been started, you can use docker exec to enter the container.

Assuming the container name is my_ubuntu_container, you can use the following command:

docker exec -it my_ubuntu_container bash

Update package list

apt-get update

Install OpenSSH Server

apt-get install -y openssh-server

Start SSH service

After installing the OpenSSH server, create the necessary directories and start the SSH service:

mkdir /var/run/sshd
service ssh start

Set root password

Set the root user's password so that you can log in to the container via SSH:

echo 'root:root' | chpasswd

The password set here is root, you can modify it as needed.

Configure SSH to allow root login

You may need to modify the SSH configuration to allow root users to log in with their password.

Edit SSH configuration file

nano /etc/ssh/sshd_config

Find PermitRootLogin and make sure it is set to yes:

PermitRootLogin yes

Restart SSH service

service ssh restart

Exposed ports

If you are connecting the container's SSH service outside the Docker container, you need to make sure that the container's SSH port is mapped to the host's port.

Assuming that the port of the host is 2222, you can run the following command to map the port:

docker run -d -p 2222:22 my_ubuntu_container

If the container is already running, the port can be mapped with the following command:

docker container run -p 2222:22 -d <container_id_or_name>

If ssh starts automatically

docker run -d -p 2222:22 --name unruffled_tu ubuntu:20.04 /usr/sbin/sshd -D

Connect to container via SSH

ssh root@localhost -p 2222

Enter the password root to enter the container.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.