How to Manually Add SSH Keys for Key-Based Authentication in a Docker Container

Umar Farooque Khan
2 min read2 days ago

--

Certainly! Here’s a manual way to add SSH keys for SSH key-based authentication to a Docker container. This process involves adding your public key to the container’s SSH configuration so that you can securely log in without using a password.

Steps to Manually Add SSH Keys to a Docker Container

  1. Generate SSH Keys (if you don’t have them already)
  2. If you don’t have SSH keys yet, generate them using the following command on your local machine:
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa
  1. This will generate two files:
  • ~/.ssh/id_rsa: Private key (keep this safe and never share it).
  • ~/.ssh/id_rsa.pub: Public key (this is what you’ll add to the container).
  1. Get the Public Key
  2. To view and copy the public key (id_rsa.pub), you can use the following command:
cat ~/.ssh/id_rsa.pub
  1. Copy the entire content of the file; it will start with ssh-rsa and end with your email address.
  2. Access the Running Docker Container
  3. If you already have the Docker container running, you can access it using docker exec.
  4. Run the following command to access the container via bash (replace container_name with the actual container name or ID):
docker exec -it container_name bash
  1. This will give you a terminal inside the container.
  2. Create SSH Directory and Authorized Keys File
  3. Now that you’re inside the container, follow these steps to add the SSH public key to the container’s authorized_keys file.
  4. Create the .ssh directory (if it doesn't already exist) inside the root user's home directory:
mkdir -p /root/.ssh
  1. Set correct permissions for the .ssh directory:
chmod 700 /root/.ssh
  1. Create or edit the authorized_keys file:
nano /root/.ssh/authorized_keys
  1. Paste the public key you copied earlier (from ~/.ssh/id_rsa.pub on your host machine) into the authorized_keys file.
  • Just paste the public key into the file.
  • Ensure that it’s all on one line.
  1. Save the file and exit the editor (in nano, press CTRL + X, then Y to confirm changes, and Enter to save).
  2. Set proper permissions for the authorized_keys file:
chmod 600 /root/.ssh/authorized_keys
  1. Exit the Container
  2. Once you’ve added the SSH key, you can exit the container:
exit
  1. SSH into the Container Using Your Private Key
  2. Now you can log into the Docker container using your private key.
  3. Run the following command from your local machine:
ssh -i ~/.ssh/id_rsa root@localhost -p 2222
  • -i ~/.ssh/id_rsa: Specifies the path to your private key.
  • root@localhost: You are logging in as the root user.
  • -p 2222: The port the container’s SSH server is listening on (assuming you mapped port 2222 to port 22 in the Docker run command).

If everything is set up correctly, you should be logged into the container without needing a password.

Conclusion

This is the manual process to add SSH key authentication to a Docker container. By copying your public key into the container’s authorized_keys file, you can securely access your Docker container using SSH key authentication without needing to enter a password.

--

--

Umar Farooque Khan
Umar Farooque Khan

Written by Umar Farooque Khan

Experienced software developer with a passion for clean code and problem-solving. Full-stack expertise in web development. Lifelong learner and team player.

No responses yet