Score:1

How to allow SSH only from WireGuard and one IP

ug flag

This is production server and I need to add rules very carefully, I don't sure what is correct rule.
Currently my UFW rules allow SSH connection from any IP and from WireGuard, also my own IP (YYY.YYY.YYY.YYY) allow me connect to any ports. Currently any other IPs can connect through VPN and directly.

 # sudo ufw status numbered
 Status: active
 To                         Action      From
 --                         ------      ----
 [ 1] XXXXX/udp                  ALLOW IN    Anywhere
 [ 2] OpenSSH                    ALLOW IN    Anywhere
 [ 3] Anywhere                   ALLOW IN    YYY.YYY.YYY.YYY
 [ 4] Anywhere on eth0           ALLOW FWD   Anywhere on wg0
 [ 5] XXXXX/udp (v6)             ALLOW IN    Anywhere (v6)
 [ 6] OpenSSH (v6)               ALLOW IN    Anywhere (v6)
 [ 7] Anywhere (v6) on eth0      ALLOW FWD   Anywhere (v6) on wg0

This is my WireGuard config on server

 #cat /etc/wireguard/wg0.conf
 [Interface]
 PrivateKey = .............................=
 Address = 10.8.0.1/24, fd..:....:....::1/64
 ListenPort = XXXXX
 SaveConfig = true
 PostUp = ufw route allow in on wg0 out on eth0
 PostUp = iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE
 PostUp = ip6tables -t nat -I POSTROUTING -o eth0 -j MASQUERADE
 PreDown = ufw route delete allow in on wg0 out on eth0
 PreDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
 PreDown = ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

I need to forbid direct connection from other IPs, any IPs except my IP can connect only with VPN.

For example I have a lot of opened JAVA ports and one opened SMB port, this is need for my application:

#sudo netstat -tulpn | grep LISTEN
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      557/systemd-resolve
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      641/sshd: /usr/sbin
tcp6       0      0 :::37111                :::*                    LISTEN      1005/java
tcp6       0      0 :::36069                :::*                    LISTEN      630/java
tcp6       0      0 :::2181                 :::*                    LISTEN      1005/java
tcp6       0      0 :::9092                 :::*                    LISTEN      630/java

I need to save JAVA opened ports and SSH ports for my IP YYY.YYY.YYY.YYY, but need to closed this ports from other IPs outside of VPN. And unfortunately SMB port need to open to any IP, this need to this server.

What UFW rules I need to add (or maybe changed Wareguard config) in order to allow only one IP (YYY.YYY.YYY.YYY) to connect directly and any other IPs can connect only from VPN and can connect to any server ports only with WireGuard.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.