Following are the key points described later in this article:
If you already have a running container, and you would like to put SSH on it and allow other docker container to connect via SSH, following is a set of instructions to install SSH:
##
## Install the openssh-server and epel-release
##
yum -y install openssh-server epel-release
yum -y install pwgen
rm -f /etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_rsa_key
ssh-keygen -q -N "" -t dsa -f /etc/ssh/ssh_host_ecdsa_key
ssh-keygen -q -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key
sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
sed -i "s/UsePAM.*/UsePAM yes/g" /etc/ssh/sshd_config
ssh-keygen -A
##
## Create Set Root Password Script. Name it as set_root_pw.sh. Save it in a folder
##
#!/bin/bash
if [ -f /.root_pw_set ]; then
echo "Root password already set!"
exit 0
fi
PASS=${ROOT_PASS:-$(pwgen -s 12 1)}
_word=$( [ ${ROOT_PASS} ] && echo "preset" || echo "random" )
echo "=> Setting a ${_word} password to the root user"
echo "root:$PASS" | chpasswd
echo "=> Done!"
touch /.root_pw_set
echo "========================================================================"
echo "You can now connect to this CentOS container via SSH using:"
echo ""
echo " ssh -p root@"
echo "and enter the root password '$PASS' when prompted"
echo ""
echo "Please remember to change the above password as soon as possible!"
echo "========================================================================"
##
## Create run.sh file with following content and save it in same folder as the above
## set_root_pw.sh
##
#!/bin/bash
if [ "${AUTHORIZED_KEYS}" != "**None**" ]; then
echo "=> Found authorized keys"
mkdir -p /root/.ssh
chmod 700 /root/.ssh
touch /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
IFS=$'\n'
arr=$(echo ${AUTHORIZED_KEYS} | tr "," "\n")
for x in $arr
do
x=$(echo $x |sed -e 's/^ *//' -e 's/ *$//')
cat /root/.ssh/authorized_keys | grep "$x" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "=> Adding public key to /root/.ssh/authorized_keys: $x"
echo "$x" >> /root/.ssh/authorized_keys
fi
done
fi
if [ ! -f /.root_pw_set ]; then
/set_root_pw.sh
fi
exec /usr/sbin/sshd -D
If you do not have yum installed on your docker, download it using wget. Alternatively, the above works fine on Centos containers.
Once done with above, it is time to run the SSH Daemon.
Once done with above, it is time to expose port 22 from the container. Following is how you would expose the 22 port:
Once installed SSH on an existing container and exposed 22 using the steps mentioned, do the following in order to test SSH from another container:
Once done with above, go ahead and test SSH connection using SCP:
Hope you found this article useful and helpful in having one docker container connect to other docker container using SSH. Please feel free to share your comments.
Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…
In the ever-evolving landscape of agentic AI workflows and applications, understanding and leveraging design patterns…
In this blog, I aim to provide a comprehensive list of valuable resources for learning…
Have you ever wondered how systems determine whether to grant or deny access, and how…
What revolutionary technologies and industries will define the future of business in 2025? As we…
For data scientists and machine learning researchers, 2024 has been a landmark year in AI…