|
1 | 1 | #!/bin/bash |
2 | 2 | # Script: ssh.sh |
3 | 3 | # Description: Configures the SSH service in the container, generates host keys (RSA, ECDSA, and ED25519), |
4 | | -# and updates the SSH configuration to use a persistent directory (/data/ssh). It also enables or disables |
| 4 | +# and updates the SSH configuration to use a persistent directory ($HOME/ssh). It also enables or disables |
5 | 5 | # password authentication based on the LGSM_PASSWORD variable. |
6 | 6 |
|
7 | | -if [ ! -d /data/ssh ]; then |
8 | | - mkdir -p /data/ssh |
| 7 | +SSH_PERSIST_DIR="$HOME/ssh" |
| 8 | +if [ ! -d "$SSH_PERSIST_DIR" ]; then |
| 9 | + mkdir -p "$SSH_PERSIST_DIR" |
9 | 10 | fi |
10 | 11 |
|
11 | | -if [ ! -f /data/ssh/ssh_host_rsa_key ]; then |
12 | | - ssh-keygen -t rsa -b 4096 -f /data/ssh/ssh_host_rsa_key -N '' |
| 12 | +if [ ! -f "$SSH_PERSIST_DIR/ssh_host_rsa_key" ]; then |
| 13 | + ssh-keygen -t rsa -b 4096 -f "$SSH_PERSIST_DIR/ssh_host_rsa_key" -N '' |
13 | 14 | fi |
14 | 15 |
|
15 | | -if [ ! -f /data/ssh/ssh_host_ecdsa_key ]; then |
16 | | - ssh-keygen -t ecdsa -f /data/ssh/ssh_host_ecdsa_key -N '' |
| 16 | +if [ ! -f "$SSH_PERSIST_DIR/ssh_host_ecdsa_key" ]; then |
| 17 | + ssh-keygen -t ecdsa -f "$SSH_PERSIST_DIR/ssh_host_ecdsa_key" -N '' |
17 | 18 | fi |
18 | 19 |
|
19 | | -if [ ! -f /data/ssh/ssh_host_ed25519_key ]; then |
20 | | - ssh-keygen -t ed25519 -f /data/ssh/ssh_host_ed25519_key -N '' |
| 20 | +if [ ! -f "$SSH_PERSIST_DIR/ssh_host_ed25519_key" ]; then |
| 21 | + ssh-keygen -t ed25519 -f "$SSH_PERSIST_DIR/ssh_host_ed25519_key" -N '' |
21 | 22 | fi |
22 | 23 |
|
23 | | -sed -i 's|#HostKey /etc/ssh/ssh_host_rsa_key|HostKey /data/ssh/ssh_host_rsa_key|' /etc/ssh/sshd_config |
24 | | -sed -i 's|#HostKey /etc/ssh/ssh_host_ecdsa_key|HostKey /data/ssh/ssh_host_ecdsa_key|' /etc/ssh/sshd_config |
25 | | -sed -i 's|#HostKey /etc/ssh/ssh_host_ed25519_key|HostKey /data/ssh/ssh_host_ed25519_key|' /etc/ssh/sshd_config |
| 24 | +sed -i "s|#HostKey /etc/ssh/ssh_host_rsa_key|HostKey $clear/ssh_host_rsa_key|" /etc/ssh/sshd_config |
| 25 | +sed -i "s|#HostKey /etc/ssh/ssh_host_ecdsa_key|HostKey $SSH_PERSIST_DIR/ssh_host_ecdsa_key|" /etc/ssh/sshd_config |
| 26 | +sed -i "s|#HostKey /etc/ssh/ssh_host_ed25519_key|HostKey $SSH_PERSIST_DIR/ssh_host_ed25519_key|" /etc/ssh/sshd_config |
26 | 27 |
|
27 | | -# Configure password authentication based on the LGSM_PASSWORD variable: |
28 | | -# If LGSM_PASSWORD has a value, enable PasswordAuthentication. |
29 | | -# If it is empty, disable it. |
30 | | -if [ -n "${LGSM_PASSWORD}" ]; then |
31 | | - sed -i 's|#PasswordAuthentication yes|PasswordAuthentication yes|' /etc/ssh/sshd_config |
| 28 | + |
| 29 | +# --- SOPORTE CAMBIO DINÁMICO DE PasswordAuthentication Y PUERTO SSH --- |
| 30 | +ssh_config_changed=0 |
| 31 | + |
| 32 | +# PasswordAuthentication |
| 33 | +desired_auth="no" |
| 34 | +[ -n "${LGSM_PASSWORD}" ] && desired_auth="yes" |
| 35 | +current_auth=$(grep -E '^[# ]*PasswordAuthentication[ ]+(yes|no)' /etc/ssh/sshd_config | tail -1 | awk '{print $2}') |
| 36 | +if [ -n "$current_auth" ]; then |
| 37 | + if [ "$current_auth" != "$desired_auth" ]; then |
| 38 | + sed -i "/^[# ]*PasswordAuthentication[ ]\+/c\PasswordAuthentication ${desired_auth}" /etc/ssh/sshd_config |
| 39 | + echo "[ssh.sh] PasswordAuthentication cambiado: $current_auth → $desired_auth" |
| 40 | + ssh_config_changed=1 |
| 41 | + else |
| 42 | + echo "[ssh.sh] PasswordAuthentication ya configurado en $desired_auth, sin cambios." |
| 43 | + fi |
32 | 44 | else |
33 | | - sed -i 's|#PasswordAuthentication yes|PasswordAuthentication no|' /etc/ssh/sshd_config |
| 45 | + echo "PasswordAuthentication ${desired_auth}" >> /etc/ssh/sshd_config |
| 46 | + echo "[ssh.sh] PasswordAuthentication agregado: $desired_auth" |
| 47 | + ssh_config_changed=1 |
34 | 48 | fi |
35 | 49 |
|
| 50 | +# Port |
36 | 51 | if [ -n "${SSH_PORT}" ]; then |
37 | | - sed -i "s|#Port 22|Port ${SSH_PORT}|" /etc/ssh/sshd_config |
| 52 | + current_port=$(grep -E '^[# ]*Port[ ]+[0-9]+' /etc/ssh/sshd_config | tail -1 | awk '{print $2}') |
| 53 | + if [ -n "$current_port" ]; then |
| 54 | + if [ "$current_port" != "$SSH_PORT" ]; then |
| 55 | + sed -i "/^[# ]*Port[ ]\+/c\Port ${SSH_PORT}" /etc/ssh/sshd_config |
| 56 | + echo "[ssh.sh] Puerto SSH cambiado: $current_port → $SSH_PORT" |
| 57 | + ssh_config_changed=1 |
| 58 | + else |
| 59 | + echo "[ssh.sh] Puerto SSH ya configurado en $SSH_PORT, sin cambios." |
| 60 | + fi |
| 61 | + else |
| 62 | + echo "Port ${SSH_PORT}" >> /etc/ssh/sshd_config |
| 63 | + echo "[ssh.sh] Puerto SSH agregado: $SSH_PORT" |
| 64 | + ssh_config_changed=1 |
| 65 | + fi |
| 66 | +else |
| 67 | + echo "[ssh.sh] SSH_PORT no definido, usando configuración por defecto." |
38 | 68 | fi |
39 | 69 |
|
40 | | -# Start the SSH service |
41 | | -service ssh start |
| 70 | +# Recargar/reiniciar solo si hubo cambios |
| 71 | +if [ "$ssh_config_changed" = "1" ]; then |
| 72 | + service ssh reload || service ssh restart |
| 73 | +fi |
| 74 | + |
| 75 | +# Start the SSH service si no está corriendo |
| 76 | +if ! pgrep -x "sshd" > /dev/null; then |
| 77 | + service ssh start |
| 78 | +fi |
0 commit comments