SSH into Docker with Root User
Nihal Maskey
2 min read
Tools
- Docker
OS:
- Ubuntu
Steps:
1. Pull ubuntu image
docker pull ubuntu
- Pulls the Ubuntu Docker image from the repository.
2. Run Ubuntu Image with Name and Exposing Port:
docker run -id --name ubuntu_ssh_ -p 2222:22 ubuntu
Launches the Ubuntu Docker image in detached mode.
Exposes port 22 of the Docker container to local port 2222.
Names the Docker container "ubuntu_ssh".
3. Execute Command to Docker Container:
docker exec -it ubuntu_ssh bash
- Opens a bash terminal within the Docker container.
4. Commands Inside Docker Container:
- Update:
apt update
- Updates the package list within the Ubuntu container.
- Install SSH:
apt install openssh-server
- Installs the OpenSSH server within the Ubuntu container.
- View Status of SSH:
service ssh status
- Checks the status of the SSH service, which should indicate that "sshd is not running".
- Add Password for Root User:
passwd root
- Sets a password for the root user when prompted.
- Install Vim:
apt install vim -y
- Installs the Vim text editor within the Ubuntu container.
- Update SSH Config File to Enable Root Login:
vi /etc/ssh/sshd_config
- Edit the SSH configuration file to enable root login by changing
PermitRootLogin
toyes
.
- Start SSH Service:
service ssh start
Starts the SSH service within the Ubuntu container.
If ssh service was already started, restart the service
service ssh restart
5. Exit out of Ubuntu Container:
exit
- Exits the bash terminal within the Ubuntu container.
6. SSH Connection to Ubuntu Container:
ssh root@localhost -p 2222
Initiates an SSH connection to the Ubuntu container running locally on port 2222.
Enter the password set for the root user when prompted.
You are now connected to the Ubuntu container via SSH!
0
Subscribe to my newsletter
Read articles from Nihal Maskey directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by