Binding IP and MAC Addresses on Linux to Prevent ARP Attacks
On a LAN, ARP (Address Resolution Protocol) is the basic protocol that maps IP addresses to MAC addresses, but it has no authentication — any host can claim to be any IP. This built-in "trust" is exactly what feeds many LAN attacks.
How ARP spoofing works
Let's start with the attack mechanics. On a LAN, hosts use ARP to resolve IP addresses to MAC addresses. Normally the gateway broadcasts a reply: "192.168.1.1 is at aa:bb:cc:dd:ee:ff". An attacker skips the rules and answers first with a forged reply: "192.168.1.1 is at <attacker's MAC>". The switch then forwards traffic meant for the gateway to the attacker, and a man-in-the-middle position is established.
A classic case: a shared office server whose gateway entry is spoofed. All inbound and outbound traffic passes through the attacker, exposing passwords, session cookies, and cleartext protocol content. Even with no sensitive data, such an attack causes link flapping that looks like a network outage and is exhausting to troubleshoot.
Static binding: lock IP to MAC
The defense is straightforward: pin the IP-to-MAC mapping for critical targets (gateway, DNS, database servers) so those ARP entries are static (PERM) and no longer accept updates broadcast on the wire — forged replies simply lose effect.
When binding is needed
The criterion is simple: if you share a layer-2 network (the same switch or subnet) with others, and untrusted devices exist there — employee laptops, customer-brought devices in an office network — binding is worth doing. Protect the gateway, DNS, file servers, and databases first. Cloud servers are usually exempt because tenants are isolated by virtualization, but mixed server rooms and self-managed office servers benefit a lot.
To be clear, static binding only treats the symptom: it protects the segment between the host and the gateway, but cannot stop a gateway impersonated by another host or re-learning after the ARP table is cleared. A more robust approach is enabling DHCP Snooping and DAI on the switch to drop forged ARP frames at the source.
1. Immediate Binding Command
arp -s 192.168.1.1 a1:d0:00:a3:30:08
This binds IP 192.168.1.1 to MAC a1:d0:00:a3:30:08 and takes effect immediately. Note that binding does not affect normal communication — traffic to that IP still uses the original physical link; it simply stops accepting forged updates. Some distributions have replaced arp with ip neigh; the two are equivalent. Modern systems also support the ip command:
ip neigh add 192.168.1.1 lladdr a1:d0:00:a3:30:08 nud permanent
2. Auto-Bind at Boot
Static entries are lost after a reboot, so write them into a boot script. On CentOS/RHEL, edit /etc/rc.local:
vi /etc/rc.local
Add the following content:
arp -s 192.168.1.1 a1:d0:00:a3:30:08
After saving, make sure /etc/rc.local has execute permission (some systems do not grant it by default):
chmod +x /etc/rc.local
On systemd systems, /etc/rc.local is managed by rc-local.service, so also make sure the service is enabled:
systemctl enable rc-local
Besides rc.local, you can also put the binding into the network interface configuration so it re-runs every time the NIC comes up. On Debian/Ubuntu add a post-up hook in /etc/network/interfaces; on CentOS append a command line at the end of /etc/sysconfig/network-scripts/ifcfg-eth0. The effect is equivalent and does not depend on whether rc-local.service starts, which suits setups with strict boot-order requirements.
3. Verify the Binding
arp -n
The corresponding entry will show a PERM (permanent) flag, meaning it is a static binding that will not be overwritten by ARP protocol updates. With ip, you can check for the permanent state:
ip neigh show
4. Remove a Static Binding
arp -d 192.168.1.1
The equivalent ip command is:
ip neigh del 192.168.1.1 dev eth0
Notes
- Make sure the IP-to-MAC mapping is correct before binding; a wrong binding will break communication for that IP;
- Static ARP entries are lost after a reboot, so write them into
/etc/rc.localor configure the network interface for auto-binding at boot; - This method mainly protects against ARP spoofing on the same LAN segment and is less effective against cross-segment attacks;
- For more thorough protection, combine it with switch port security, DHCP Snooping, and DAI (Dynamic ARP Inspection) to drop forged ARP frames at the switch layer.
- Still losing connectivity after binding the gateway:Check that you bound the right interface and the real MAC; cross-check with
ip neigh. Another possibility is that the switch also enforces port binding, and the two policies conflict and drop packets; - Do I need to bind every host on the server:No. Protect the gateway and core services; too many entries become hard to maintain;
- Does this apply to cloud servers:Usually not — clouds isolate tenants with virtual switches, but it is very practical for physical servers in self-managed server rooms;
- The gateway changed its MAC:Delete the old entry with
arp -dfirst, then bind the new address; otherwise communication keeps failing. LAN security is an easily overlooked part of server operations. For more server security hardening content, see the security hardening category, such as server security hardening and server initial security setup. > Original post: https://www.cnblogs.com/cqzhuomi/articles/17283225.html (cnblogs.com CQZHUOMI, repost) > Reference: ip-neighbour man page — https://man7.org/linux/man-pages/man8/ip-neighbour.8.html
Configuration & Integration Examples
Example 1: Read the neighbour table and add static bindings
ip neigh show dev eth0 # current entries and their state
# pin the gateway and critical peers: nud permanent means it never ages out
ip neigh replace 192.0.2.1 lladdr 02:00:5e:10:00:01 nud permanent dev eth0
ip neigh replace 192.0.2.10 lladdr 02:00:5e:10:00:02 nud permanent dev eth0
ip neigh show 192.0.2.1 # confirm permanent is in effect
# the legacy net-tools spelling, equivalent output
arp -s 192.0.2.1 02:00:5e:10:00:01
A static entry only means "this host trusts that MAC"; an attacker can still forge replies claiming the gateway IP, which is why binding belongs together with the checks below.
Example 2: Tighten ARP learning and verify it
# arp_ignore=1: answer ARP only for addresses actually configured on the interface
sysctl -w net.ipv4.conf.eth0.arp_ignore=1
# arp_announce=2: announce with the most appropriate source address
sysctl -w net.ipv4.conf.eth0.arp_announce=2
# persist the settings
printf 'net.ipv4.conf.eth0.arp_ignore=1\nnet.ipv4.conf.eth0.arp_announce=2\n' \
>> /etc/sysctl.d/99-arp.conf
sysctl --system
ip neigh flush dev eth0 && ip neigh show dev eth0 # flush, then see what survives
After ip neigh flush entries are relearned, but permanent ones are not aged out — which makes this the fastest way to confirm a binding was written correctly.
Three Ways to Make Bindings Persistent, and Checking After a Reboot
A static entry lives only in memory and disappears on reboot, so it needs something that runs at boot. The three common paths fire at different stages: /etc/rc.local is managed by rc-local.service and needs chmod +x plus systemctl enable rc-local; a post-up hook in /etc/network/interfaces runs when the interface comes up; on CentOS-family hosts the command is appended to /etc/sysconfig/network-scripts/ifcfg-eth0 and runs with the interface as well.
Verification takes two steps: confirm the state in the current session, then reboot and check again. An entry marked permanent in ip neigh show means the binding was written; after ip neigh flush dev eth0 only the static entries should remain, which proves they are exempt from aging. If neither holds, the binding command ran before the interface was ready and the startup order needs attention.
| Persistence path | Distribution | When it runs | How to verify |
|---|---|---|---|
/etc/rc.local |
CentOS/RHEL with rc-local.service | After the service starts | systemctl status rc-local and the PERM flag in arp -n |
post-up in /etc/network/interfaces |
Debian/Ubuntu | When the interface comes up | Whether ip neigh show reports permanent |
Command appended to ifcfg-eth0 |
CentOS family | When the interface comes up | ip neigh show dev eth0 after a reboot |
| Current session only | Temporary troubleshooting | Immediately | arp -n right after arp -s |
Limitations
- The effect is equivalent and does not depend on whether
rc-local.servicestarts, which suits setups with strict boot-order requirements. - This article reflects public information as of 2023-04-03; terms, prices and quotas follow the provider's current published terms.
- Third-party capability and limit statements follow their official documentation; no further commitment is implied here.
- Licensing, filing and data-residency decisions must be verified against the regulations of your operating region.
FAQ
What network setup does static ARP binding require?
Both ends on the same Layer 2 segment. Binding needs root, and the gateway MAC must be verified first with ip neigh, because a wrong hardware address silently drops return traffic. Cloud instances behind a virtual switch rarely need it; the fix is 1 command per protected address.
Why does traffic still fail after binding the gateway?
Usually a wrong MAC or a conflicting switch policy. If the bound address is not the gateway's real hardware address, outbound packets get dropped; if the switch also enforces port security, both layers can reject the same frame. Bind 1 test host first and compare ip neigh output.
How does ARP binding work alongside DAI?
They cover different hops. Static entries protect only the local host and vanish on reboot unless written into rc.local or the interface config, while DHCP Snooping plus DAI on the switch drops forged replies at the source. Run both, and keep entries to 5 critical IPs or fewer.
Related Providers
- Wireshark: capturing packets to compare MAC-to-gateway mappings when replies look forged.
- Wazuh: long-running host audits of logins and file changes correlated with ARP anomalies.
- BitNinja: internet-facing servers that need IP reputation and malware scanning on the host itself.
Community & Resources
Official docs and communities
- ip-neighbour(8) — full option set for neighbour entries
- arp(8) — the classic net-tools command and its state flags
- arp(7) — how the Linux kernel implements ARP
- ip-sysctl reference — precise meaning of
arp_ignoreandarp_announce
Learning resources and tools
- ip-route(8) — companion command when checking gateways
- nftables wiki — filtering ARP at the kernel level
- Stack Overflow: arp — conflicts, spoofing and entry states
- Stack Overflow: networking — general L2/L3 troubleshooting
Original source: cnblogs.com (CQZHUOMI) ↗