How to clear the arp cache on linux

arp

Both:

sudo ip -s -s neigh flush all

And:

sudo arp -d 192.168.0.102

Instead of clearing the arp cache they seem to just invalidate entries (they will appear as incomplete). Even after some minutes, the ARP cache looks like:

$ arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.0.103                    (incomplete)                              eth0
192.168.0.1              ether   DE:AD:BE:EF:DE:AD   C                     eth0

(The MAC of the gateway has been refreshed – that is ok)

How can I really clear the ARP cache, like in "delete all entries from the table"? I do not want to keep incomplete entries, I want them removed. Is this possible?

EDIT

This is my system:

» arp --version
net-tools 1.60
arp 1.88 (2001-04-04)
+I18N
AF: (inet) +UNIX +INET +INET6 +IPX +AX25 +NETROM +X25 +ATALK +ECONET +ROSE 
HW: (ether) +ETHER +ARC +SLIP +PPP +TUNNEL -TR +AX25 +NETROM +X25 +FR +ROSE +ASH +SIT +FDDI +HIPPI +HDLC/LAPB +EUI64 

» lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.2 LTS
Release:        14.04
Codename:       trusty

» uname -a
Linux polyphemus.xxx-net 3.13.0-46-generic #77-Ubuntu SMP Mon Mar 2 18:23:39 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

Best Answer

Original oneliner

ip link set arp off dev eth0 ; ip link set arp on dev eth0

Be sure to do it all at once, so you don't break network connectivity before you're able to turn ARP back on.

Interface discovering copy-paste command

interfaces=$(
  arp -n | awk '
    NR == 1 {next}
    {interfaces[$5]+=1}
    END {for (interface in interfaces){print(interface)}}
  '
);
for interface in $interfaces; do
  echo "Clearing ARP cache for $interface";
  sudo ip link set arp off dev $interface;
  sudo ip link set arp on  dev $interface;
done

Note: The semicolons allow you to condense this command into a oneliner, but it looks terrible in a code block on SO.

Example output on Raspbian

pi@raspberrypi:~ $ arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
10.0.0.1                 ether   58:19:f8:0d:57:aa   C                     wlan0
10.0.0.159               ether   88:e9:fe:84:82:c8   C                     wlan0

pi@raspberrypi:~ $ interfaces=$( arp -n | awk ' NR == 1 {next} {interfaces[$5]+=1} END {for (interface in interfaces){print(interface)}} '); for interface in $interfaces; do echo "Clearing ARP cache for $interface"; sudo ip link set arp off dev $interface; sudo ip link set arp on  dev $interface; done
Clearing ARP cache for wlan0

pi@raspberrypi:~ $ arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
10.0.0.159               ether   88:e9:fe:84:82:c8   C                     wlan0
Related Question