1 ssh
Dennis Buchhorn edited this page 2023-06-26 16:03:35 +00:00

SSH

Start sshd listening only on specific DHCP interface

Tested on Debian 12

Prerequisites:

  • jq

First disable automatic start of sshd:

sudo systemctl disable ssh.service
sudo systemctl stop ssh.service

Create if-up script:

sudo nano /etc/network/if-up.d/start_sshd

With following content:

Note: Change enp6s18 to the interface name you want.

#!/bin/sh

if [ "$IFACE" = "enp6s18" ]; then
    IPV4_ADDR=$(ip -j -4 addr list $IFACE | jq --raw-output .[].addr_info[].local)
    mkdir -p /run/sshd
    chmod 755 /run/sshd
    /usr/sbin/sshd -oListenAddress=$IPV4_ADDR
fi

After that make this script executable:

sudo chmod +x /etc/network/if-up.d/start_sshd

Now reboot and the sshd service should start listening only on the interface with the given IPv4 address via DHCP.