Last active
October 10, 2021 11:22
-
-
Save aur3l14no/1aef0559135a6e13d2e15eb15462ca95 to your computer and use it in GitHub Desktop.
Setup SSH in containers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
container=$1 | |
port=$2 | |
pubkey=$3 | |
if [ $# -lt 3 ]; then | |
echo "Usage: ./setup_container_ssh.sh [container_name] [port] [pubkey]" | |
else | |
docker exec ${container} /bin/sh -c " | |
ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime | |
DEBIAN_FRONTEND=noninteractive apt update && apt install -y openssh-server | |
mkdir -m 700 -p /root/.ssh | |
echo ${pubkey} >/root/.ssh/authorized_keys | |
chmod 600 /root/.ssh/authorized_keys | |
# Change Port | |
sed -ri 's/#?Port\s.*$/Port ${port}/' /etc/ssh/sshd_config | |
# Enable pubkey auth | |
sed -ri 's/#?PubkeyAuthentication\s.*$/PubkeyAuthentication yes/' /etc/ssh/sshd_config | |
# Disable root login | |
sed -ri 's/#?PermitRootLogin\s.*$/PermitRootLogin prohibit-password/' /etc/ssh/sshd_config | |
# Disable password login | |
sed -ri 's/#?PasswordAuthentication\s.*$/PasswordAuthentication no/' /etc/ssh/sshd_config | |
# Restart the SSH server | |
service ssh restart | |
" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment