Operations 3 min read

Turn a Single CentOS Server into a LAN Gateway with iptables SNAT

This guide shows how to configure one CentOS machine as a NAT gateway using iptables, enabling nine other LAN servers to access the Internet via SNAT or MASQUERADE, with step‑by‑step commands, kernel settings, and client configuration tips.

Open Source Linux
Open Source Linux
Open Source Linux
Turn a Single CentOS Server into a LAN Gateway with iptables SNAT

Overview

A cluster of ten servers forms a LAN, but only one server has an external network interface. The goal is to let the other nine servers reach the Internet through this host.

Experiment Environment Introduction

We use a CentOS host as the gateway. The topology is illustrated below.

Network topology diagram
Network topology diagram

Configure Gateway Server

# step 1: stop and permanently disable firewalld
systemctl stop firewalld
systemctl disable firewalld

# step 2: install iptables‑services
yum -y install iptables-services

# step 3: enable kernel forwarding
# edit /etc/sysctl.conf and add:
net.ipv4.ip_forward = 1
sysctl -p   # apply changes

# step 4: add NAT rule (MASQUERADE for dynamic external IP)
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE

# step 5: save iptables rules (otherwise they disappear after reboot)
service iptables save

The MASQUERADE target is used when the external IP is assigned dynamically.

You can also set a static external IP with SNAT:

iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to 39.96.81.141
iptables rule example
iptables rule example

Client Access Internet

After configuring the gateway, set each LAN host’s default gateway to the internal IP address of the gateway server. Ensure DNS settings are correct, otherwise name resolution will fail.

For DNS configuration on Linux servers, refer to the linked guide.

DNS tip illustration
DNS tip illustration
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

gatewaySNATiptablesCentOSLinux networkingLANMASQUERADE
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.