this post was submitted on 16 Feb 2025
18 points (100.0% liked)

Linux

52002 readers
1108 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

Here's the setup:

  • Fedora 41 Server host
  • Libvirt/QEMU
  • Alma 9 guest running ssh

My goal is to forward ports from the guest to the host, but change them. I set up a hook(as in the libvirt docs) and it worked on my last server. My hook looks like:

#!/bin/bash

if [ "${1}" = "Jellyfin" ]; then

   # Update the following variables to fit your setup
   GUEST_IP=192.168.101.4
   GUEST_PORT=22
   HOST_PORT=2222

   if [ "${2}" = "stopped" ] || [ "${2}" = "reconnect" ]; then
    /sbin/iptables -D FORWARD -o virbr1 -p tcp -d $GUEST_IP --dport $GUEST_PORT -j ACCEPT
    /sbin/iptables -t nat -D PREROUTING -p tcp --dport $HOST_PORT -j DNAT --to $GUEST_IP:$GUEST_PORT
   fi
   if [ "${2}" = "start" ] || [ "${2}" = "reconnect" ]; then
    /sbin/iptables -I FORWARD -o virbr1 -p tcp -d $GUEST_IP --dport $GUEST_PORT -j ACCEPT
    /sbin/iptables -t nat -I PREROUTING -p tcp --dport $HOST_PORT -j DNAT --to $GUEST_IP:$GUEST_PORT
   fi
fi

However, when I ssh to my server:2222, it doesn't work, "Connection refused." I can ssh from inside my server to my guest's ip address, so I know it's not an issue with ssh itself. The guest's iptables rules are:

-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT

so that's probably not the issue.

My server's iptables rules include:

-A FORWARD -d 192.168.101.4/32 -o virbr1 -p tcp -m tcp --dport 22 -j ACCEPT

, so it appears the forwarding happened, but an nmap scan reveals the port is closed:

2222/tcp closed EtherNetIP-1

I'm baffled by this issue. Any help would be greatly appreciated!

top 6 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 1 month ago (2 children)

Add or uncomment net.ipv4.ip_forward=1in /etc/sysctl.conf ans then sudo sysctl -p

[–] [email protected] 3 points 1 month ago

The one thing I always forget, no matter how many DNAT setups or whatever I write with iptables.

[–] [email protected] 3 points 1 month ago

sysctl net.ipv4.ip_forward returns:

net.ipv4.ip_forward = 1

So I'm pretty sure that this is already enabled. Thanks for your answer!

[–] [email protected] 3 points 1 month ago (2 children)

ssh -v can be help troubleshoot connection issues. Any firewalls involved on either end?

[–] [email protected] 2 points 1 month ago

No firewalls on the client, but iptables on host and guest. guest has no rules just allow all, and host rules are listed in the post.

[–] [email protected] 2 points 1 month ago

ssh -v returns:

OpenSSH_9.2p1 Debian-2+deb12u4, OpenSSL 3.0.15 3 Sep 2024
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to 192.168.86.73 [192.168.86.73] port 2222.
debug1: connect to address 192.168.86.73 port 2222: Connection refused
ssh: connect to host 192.168.86.73 port 2222: Connection refused