InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Networking tutorials

  • DIG quick start tutorial
  • Dig - list all dns records
  • How to view wifi connection speed on Mac
  • Linux iptables - Nat port forwarding using PREROUTING
  • Linux iptables and ip6tables examples
  • Ngrep - quick start guide
  • Use nc to check if a remote port is reachable
  • Use nc to listen to tcp or udp port
  • Use nc to test HTTP url redirection
  • add more DNS servers to Wi-Fi interface on Mac
  • locally override website host to IP mapping
  • use nc to print headers from browser
 
  • Home
  • > Tutorials
  • > Networking

Linux iptables – Nat port forwarding using PREROUTING

on Sep 23, 2018

One can use iptables to forward a specific port to another port using NAT PREROUTING chain. This can be used to make a server available on a different port for users.

  1. Add NAT forwarding using PREROUTING chain
    $ sudo iptables -t nat -A PREROUTING -p tcp --dport 81 -j REDIRECT --to-port 80
    
  2. Add NAT forwarding using PREROUTING chain on a specific interface
    $ sudo iptables -t nat -A PREROUTING -p tcp -d INTERFACE_IP --dport 443 -j REDIRECT --to-port 8443
    
  3. List iptable nat PREROUTE rules
    ## -n (numeric)
    $ sudo iptables -t nat -L PREROUTING -n  --line-numbers
    Chain PREROUTING (policy ACCEPT)
    num  target     prot opt source               destination         
    1    REDIRECT   tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:81 redir ports 80
    2    REDIRECT   tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:82 redir ports 80
    
  4. List all rules with line numbers
    $ sudo iptables -n -L -v --line-numbers
    
  5. Delete a specific rule
    $ sudo iptables -t nat -D PREROUTING [NUM]
    

Suggested posts:

  1. Ubuntu – reload iptables rules during boot
  2. Centos – save and restore iptables
  3. How to use ssh port forwarding to surf a site from different location
  4. How to connect to mysql server using ssh port forwarding
  5. Linux iptables and ip6tables examples
  6. Linux – ssh agent forwarding when using bastion host
  7. Find which process is listening on a port on Linux
  8. Use netcat (nc) to listen on tcp or udp port
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Linux, Networking, Tutorials, Unix

Follow InfoHeap

facebook
twitter
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

Copyright © 2021 InfoHeap.

Powered by WordPress