Use iptables for local port forwarding in Linux

Source: Internet
Author: User

Use iptables for local port forwarding in Linux

Port 16688 of the local machine is not open. How to Use iptables in Linux to forward the local port to an opened port is as follows:

1. view port usage

[Root @ bkjia ~] # Netstat-ntal
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
Tcp 0 0 127.0.0.1: 9000 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 3306 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 80 0.0.0.0: * LISTEN
Tcp 0 0 192.168.2.177: 53 0.0.0.0: * LISTEN
Tcp 0 0 192.168.2.35: 53 0.0.0.0: * LISTEN
Tcp 0 0 127.0.0.1: 53 0.0.0.0: * LISTEN
Tcp 0 0 0.0.0.0: 22 0.0.0.0: * LISTEN
Tcp 0 0 127.0.0.1: 25 0.0.0.0: * LISTEN
Tcp 0 0 127.0.0.1: 953 0.0.0.0: * LISTEN
Tcp 0 0 192.168.2.35: 22 192.168.00005: 49574 ESTABLISHED
Tcp 0 0: 8080: * LISTEN
Tcp 0 0: 22: * LISTEN
Tcp 0 0: 1: 25: * LISTEN
Tcp 0 0: 1: 953: * LISTEN

2. Enable gate forwarding

Edit/etc/sysctl. conf and set

Net. ipv4.ip _ forward = 1

Run:

# Sysctl-p

3. For example, add a * nat rule to connect to port 1668 of the server and forward it to port 22.

* Nat
: Prerouting accept [0: 0]
: Postrouting accept [0: 0]
: Output accept [0: 0]

-A prerouting-p tcp-I eth0-d 192.168.2.35 -- dport 16688-j DNAT -- to 192.168.2.35: 22

COMMIT

4. telnet Test

5. The above is only the case where the port is used outside, but the port 43999 is not used in the server.

[Root @ bkjia ~] # Ssh 192.168.2.35-p 16688
Ssh: connect to host 192.168.2.35 port 16688: Connection refused

6. Here we can also set local forwarding.

Iptables-t-a output-p tcp-d 192.168.2.35 -- dport 16688-j DNAT -- to 127.0.0.1: 22

7. perform a test from the local machine. The connection is also available, although the port is not enabled.

Ssh 192.168.2.35-p 16688
The authenticity of host' [192.168.2.35]: 16688 ([192.168.2.35]: 16688) 'Can't be established.
RSA key fingerprint is 13: 02: d6: d5: 49: e7: 05: ce: 48: c8: 56: d9: b8: b2: 06: 38.
Are you sure you want to continue connecting (yes/no )?

For more iptables tutorials, see the following:

Disable the default firewall in CentOS 7.0 and enable the iptables firewall.

Iptables examples

Linux Firewall iptables

Basic use of iptables backup, recovery, and firewall scripts

Detailed description of firewall iptables usage rules in Linux

Iptables firewall settings in Linux

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.