|
1 | 1 | #!/bin/bash
|
2 | 2 |
|
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 |
6 | 6 | fi
|
7 | 7 |
|
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) |
12 | 9 |
|
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 |
15 | 15 |
|
| 16 | +echo "=> Done!" |
16 | 17 | 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:" |
21 | 19 | echo ""
|
22 |
| -echo "Please remember to change the above password as soon as possible!" |
| 20 | +echo " ssh -p <port> $USER@<host>" |
23 | 21 | 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