Gentoo Archives: gentoo-user

From: Victor Ivanov <vic.m.ivanov@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Configure sshd to listen on specific interfaces?
Date: Thu, 27 Aug 2020 14:48:27
Message-Id: f762c17b-bc6e-08aa-cdeb-662c725eb440@gmail.com
In Reply to: [gentoo-user] Configure sshd to listen on specific interfaces? by Grant Edwards
1 On 27/08/2020 14:40, Grant Edwards wrote:
2 > I do _not_ want it to listen on 0.0.0.0.
3 >
4 > I want it to listen on 127.0.0.1 and on whatever IP addresses are
5 > assigned to two specified interfaces.
6
7 As far as I'm aware, I don't think OpenSSH allows for listening on a
8 specific interface.
9
10 You can, however, work around this in a rather unusual way via
11 ip/nftables and DNAT.
12
13 You will need to enable IP[v6] forwarding via sysctl (or sysctl.conf):
14
15 net.ipv4.ip_forward=1
16 net.ipv4.conf.<IFACE>.route_localnet=1
17
18 The latter option is critical as, by default, the kernel will not allow
19 you to route to 127.0.0.0/8 as a security precaution.
20
21
22 iptables
23 ========
24
25 (1) iptables -t nat -A PREROUTING -i <IFACE> -p tcp --dport 22 -j DNAT
26 --to-destination 127.0.0.1:22
27 [2] ip6tables -t nat -A PREROUTING -i <IFACE> -p tcp --dport 22 -j DNAT
28 --to-destination [::1]:22
29
30
31 nftables (json-like)
32 ====================
33
34 table inet nat {
35 chain prerouting {
36 type nat hook prerouting priority dstnat; policy accept;
37 iif "<IFACE>" tcp dport 22 dnat ip to 127.0.0.1:22
38 iif "<IFACE>" tcp dport 22 dnat ip6 to [::1]:22
39 }
40 }
41
42
43 nftables (cmdline)
44 ==================
45
46 [1] nft add table inet nat
47 [2] nft add chain inet nat prerouting { type nat hook prerouting
48 priority dsnat\; }
49 (3) nft add rule inet nat prerouting iif <IFACE> tcp dport 22 dnat ip to
50 127.0.0.1:22
51 [4] nft add rule inet nat prerouting iif <IFACE> tcp dport 22 dnat ip6
52 to [::1]:22
53
54 As always, 1-2 are not required if you already have a relevant table/chain.
55
56 Adjust as needed for multiple IFACEs.
57
58 - V

Attachments

File name MIME type
signature.asc application/pgp-signature

Replies

Subject Author
[gentoo-user] Re: Configure sshd to listen on specific interfaces? Grant Edwards <grant.b.edwards@×××××.com>
Re: [gentoo-user] Configure sshd to listen on specific interfaces? "Matt Connell (Gmail)" <matthewdconnell@×××××.com>