Skip to content

Commit c32a745

Browse files
author
Kirill Müller
committed
support key-based authentication via ROOT_KEY env var only
1 parent 7521361 commit c32a745

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed

run.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/bash
2-
if [ ! -f /.root_pw_set ]; then
3-
/set_root_pw.sh
4-
fi
2+
3+
set -e
4+
5+
/set_root_pw.sh
56
exec /usr/sbin/sshd -D

set_root_pw.sh

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
11
#!/bin/bash
22

3-
if [ -f /.root_pw_set ]; then
4-
echo "Root password already set!"
5-
exit 0
3+
if [ -z "${ROOT_KEY}" ]; then
4+
echo "=> Please pass your public key in the ROOT_KEY environment variable"
5+
exit 1
66
fi
77

8-
PASS=${ROOT_PASS:-$(pwgen -s 12 1)}
9-
_word=$( [ ${ROOT_PASS} ] && echo "preset" || echo "random" )
10-
echo "=> Setting a ${_word} password to the root user"
11-
echo "root:$PASS" | chpasswd
8+
USER=$(whoami)
129

13-
echo "=> Done!"
14-
touch /.root_pw_set
10+
echo "=> Adding SSH key for the user ${USER}"
11+
mkdir -p ~/.ssh
12+
chmod go-rwx ~/.ssh
13+
echo "${ROOT_KEY}" > ~/.ssh/authorized_keys
14+
chmod go-rw ~/.ssh/authorized_keys
1515

16+
echo "=> Done!"
1617
echo "========================================================================"
17-
echo "You can now connect to this Debian container via SSH using:"
18-
echo ""
19-
echo " ssh -p <port> root@<host>"
20-
echo "and enter the root password '$PASS' when prompted"
18+
echo "You can now connect to this container via SSH using:"
2119
echo ""
22-
echo "Please remember to change the above password as soon as possible!"
20+
echo " ssh -p <port> $USER@<host>"
2321
echo "========================================================================"
24-
25-
if [ -n "${ROOT_KEY}" ]; then
26-
echo "=> Adding an SSH key for the root user"
27-
mkdir -p /root/.ssh
28-
chmod go-rwx /root/.ssh
29-
echo "${ROOT_KEY}" > /root/.ssh/authorized_keys
30-
chmod go-rw /root/.ssh/authorized_keys
31-
cat /root/.ssh/authorized_keys
32-
fi

0 commit comments

Comments
 (0)