Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Copyrighted material

Linux ip Command Examples


Posted By nixCraft <webmaster@cyberciti.biz> On January 13, 2012 @ 5:29 pm [ 3 Comments ]

am a new Linux system admin user. How do I use ip command line utility to display or
configure networking, routing, and tunnels on Linux operating systems? How do I
configures or displays network interface parameters for a network using TCP/IP on Linux
operating systems?
The ip command is used to assign an address to a network interface and/or configure
network interface parameters on Linux operating systems. This command replaces old good
and now deprecated ifconfig command on modern Linux distributions. It is used for the
following purposes:
1.
2.
3.
4.
5.
6.
7.
8.

Find out which interfaces are configured on the system.


Query the status of a IP interface.
Configure the local loop-back, Ethernet and other IP interfaces.
Mark the interface as up or down.
Configure and modify default and static routing.
Configure tunnel over IP.
Configure ARP or NDISC cache entry.
Assign IP address, routes, subnet and other IP information to IP interfaces.

Purpose
Use this command to display and configure the network parameters for host interfaces.

Syntax
ip OBJECT COMMAND
ip [options] OBJECT COMMAND
ip OBJECT help

Understanding ip command OBJECTS syntax


OBJECTS can be any one of the following and may be written in full or abbreviated form:
Object

Abbreviated form Purpose

link

Network device.

address

a
addr

Protocol (IP or IPv6) address on a device.

addrlabel addrl

Label configuration for protocol address selection.

n
neighbour neigh

ARP or NDISC cache entry.

route

Routing table entry.

rule

ru

Rule in routing policy database.

maddress

m
maddr

Multicast address.

mroute

mr

Multicast routing cache entry.

tunnel

Tunnel over IP.

xfrm

Framework for IPsec protocol.

nixCraft is GIT UL++++ W+++ C++++ M+ e+++ d-

Page 1 of 7

To get information about each object use help command as follows:


ip
ip
ip
ip

OBJECT help
OBJECT h
a help
r help

Warning: The commands described below must be executed with care. If you make a mistake, you will loos connectivity
to the server. You must take special care while working over the ssh based remote session.

ip command examples
Don't be intimidated by ip command syntax. Let us get started quickly with examples.

Displays info about all network interfaces


Type the following command to list and show all ip address associated on on all network interfaces:
ip a
OR
ip addr
Sample outputs:

[1]

Fig.01 Showing IP address assigned to eth0, eth1, lo using ip command


You can select between IPv4 and IPv6 using the following syntax:
### Only show TCP/IP IPv4
ip -4 a

##

### Only show TCP/IP IPv6


ip -6 a

###

It is also possible to specify and list particular interface TCP/IP details:


### Only show eth0 interface ###
ip a show eth0
ip a list eth0
ip a show dev eth0
### Only show running interfaces ###
ip link ls up

Assigns the IP address to the interface


The syntax is as follows to add an IPv4/IPv6 address:
ip a add {ip_addr/mask} dev {interface}
To assign 192.168.1.200/255.255.255.0 to eth0, enter:
nixCraft is GIT UL++++ W+++ C++++ M+ e+++ d-

Page 2 of 7

ip a add 192.168.1.200/255.255.255.0 dev eth0


OR
ip a add 192.168.1.200/24 dev eth0
Adding the broadcast address on the interface
By default, the ip command does not set any broadcast address unless explicitly requested. So syntax is as follows to set
broadcast ADDRESS:
ip addr add brd {ADDDRESS-HERE} dev {interface}
ip addr add broadcast {ADDDRESS-HERE} dev {interface}
ip addr add broadcast 172.20.10.255 dev dummy0
It is possible to use the special symbols such as + and - instead of the broadcast address by setting/resetting the host bits
of the interface prex. In this example, add the address 192.168.1.50 with netmask 255.255.255.0 (/24) with standard
broadcast and label "eth0Home" to the interface eth0:
ip addr add 192.168.1.50/24 brd + dev eth0 label eth0Home
You can set loopback address to the loopback device lo as follows:
ip addr add 127.0.0.1/8 dev lo brd + scope host

Remove / Delete the IP address from the interface


The syntax is as follows to remove an IPv4/IPv6 address:
ip a del {ipv6_addr_OR_ipv4_addr} dev {interface}
To delete 192.168.1.200/24 from eth0, enter:
ip a del 192.168.1.200/24 dev eth0

Flush the IP address from the interface


You can delete or remote an IPv4/IPv6 address one-by-one as described above [2]. However, the flush command can
remove as flush the IP address as per given condition. For example, you can delete all the IP addresses from the private
network 192.168.2.0/24 using the following command:
ip -s -s a f to 192.168.2.0/24
Sample outputs:
2: eth0
inet 192.168.2.201/24 scope global secondary eth0
2: eth0
inet 192.168.2.200/24 scope global eth0
*** Round 1, deleting 2 addresses ***
*** Flush is complete after 1 round ***
You can disable IP address on all the ppp (Point-to-Point) interfaces:
ip -4 addr flush label "ppp*"
Here is another example for all the Ethernet interfaces:
ip -4 addr flush label "eth*"

How do I change the state of the device to UP or DOWN?


The syntax is as follows:
ip link set dev {DEVICE} {up|down}
To make the state of the device eth1 down, enter:
ip link set dev eth1 down
To make the state of the device eth1 up, enter:
ip link set dev eth1 up
nixCraft is GIT UL++++ W+++ C++++ M+ e+++ d-

Page 3 of 7

How do I change the txqueuelen of the device?


You can set the length of the transmit queue [3] of the device using ifconfig command or ip command as follows:
ip link set txqueuelen {NUMBER} dev {DEVICE}
In this example, change the default txqueuelen from 1000 to 10000 for the eth0:
ip link set txqueuelen 10000 dev eth0
ip a list eth0

How do I change the MTU of the device?


For gigabit networks you can set maximum transmission units (MTU) [4] sizes (JumboFrames) for better network
performance. The syntax is:
ip link set mtu {NUMBER} dev {DEVICE}
To change the MTU of the device eth0 to 9000, enter:
ip link set mtu 9000 dev eth0
ip a list eth0
Sample outputs:
2: eth0: mtu 9000 qdisc pfifo_fast state UP qlen 1000
link/ether 00:08:9b:c4:30:30 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global eth1
inet6 fe80::208:9bff:fec4:3030/64 scope link
valid_lft forever preferred_lft forever

Display neighbour/arp cache


The syntax is:
ip n show
ip neigh show
Sample outputs (note: I masked out some data with alphabets):
74.xx.yy.zz dev eth1 lladdr 00:30:48:yy:zz:ww REACHABLE
10.10.29.66 dev eth0 lladdr 00:30:48:c6:0a:d8 REACHABLE
74.ww.yyy.xxx dev eth1 lladdr 00:1a:30:yy:zz:ww REACHABLE
10.10.29.68 dev eth0 lladdr 00:30:48:33:bc:32 REACHABLE
74.fff.uu.cc dev eth1 lladdr 00:30:48:yy:zz:ww STALE
74.rr.ww.fff dev eth1 lladdr 00:30:48:yy:zz:ww DELAY
10.10.29.65 dev eth0 lladdr 00:1a:30:38:a8:00 REACHABLE
10.10.29.74 dev eth0 lladdr 00:30:48:8e:31:ac REACHABLE
The last field show the the state of the "neighbour unreachability detection" machine for this entry:
1. STALE - The neighbour is valid, but is probably already unreachable, so the kernel will try to check it at the first
transmission.
2. DELAY - A packet has been sent to the stale neighbour and the kernel is waiting for confirmation.
3. REACHABLE - The neighbour is valid and apparently reachable.

Add a new ARP entry


The syntax is:
ip neigh add {IP-HERE} lladdr {MAC/LLADDRESS} dev {DEVICE} nud {STATE}
In this example, add a permanent ARP entry for the neighbour 192.168.1.5 on the device eth0:
ip neigh add 192.168.1.5 lladdr 00:1a:30:38:a8:00 dev eth0 nud perm
Where,
neighbour
state (nud)

meaning

nixCraft is GIT UL++++ W+++ C++++ M+ e+++ d-

Page 4 of 7

permanent

The neighbour entry is valid forever and can be only be removed administratively

noarp

The neighbour entry is valid. No attempts to validate this entry will be made but it can be removed when
its lifetime expires.

stale

The neighbour entry is valid but suspicious. This option to ip neigh does not change the neighbour state if
it was valid and the address is not changed by this command.

reachable

The neighbour entry is valid until the reachability timeout expires.

Delete a ARP entry


The syntax to invalidate or delete an ARP entry for the neighbour 192.168.1.5 on the device eth1 is as follows.
ip neigh del {IPAddress} dev {DEVICE}
ip neigh del 192.168.1.5 dev eth1
Change are state to reachable for the neighbour 192.168.1.100 on the device eth1
ip neigh chg 192.168.1.100 dev eth1 nud reachable

Flush ARP entry


This flush or f command flushes neighbour/arp tables, by specifying some condition. The syntax is:
ip -s -s n f {IPAddress}
In this example, flush neighbour/arp table
ip -s -s n f 192.168.1.5
OR
ip -s -s n flush 192.168.1.5

ip route: Routing table management commands


Use the following command to manage or manipulate the kernel routing table.

Show routing table


To display the contents of the routing tables:
ip
ip
ip
ip

r
r list
r list [options]
route

Sample outputs:
default via 192.168.1.254 dev eth1
192.168.1.0/24 dev eth1 proto kernel

scope link

src 192.168.1.10

scope link

src 192.168.1.10

Display routing for 192.168.1.0/24:


ip r list 192.168.1.0/24
Sample outputs:
192.168.1.0/24 dev eth1

proto kernel

Add a new route


The syntax is:
ip
ip
ip
ip

route
route
route
route

add
add
add
add

{NETWORK/MASK} via {GATEWAYIP}


{NETWORK/MASK} dev {DEVICE}
default {NETWORK/MASK} dev {DEVICE}
default {NETWORK/MASK} via {GATEWAYIP}

Add a plain route to network 192.168.1.0/24 via gateway 192.168.1.254 [5]:


nixCraft is GIT UL++++ W+++ C++++ M+ e+++ d-

Page 5 of 7

ip route add 192.168.1.0/24 via 192.168.1.254


To route all traffic via 192.168.1.254 gateway connected via eth0 network [6] interface:
ip route add 192.168.1.0/24 dev eth0

Delete a route
The syntax is as follows to delete default gateway:
ip route del default
In this example, delete the route created in previous subsection [7]:
ip route del 192.168.1.0/24 dev eth0
Category

List of Unix and Linux commands

File Management cat [8]


Network Utilities

dig [9] host [10] ip [11]

Processes bg [12] disown [13] fg [14] jobs [15] kill [16] killall [17] pwdx [18] time [19] pidof [20] pstree
Management [21]
Searching
User Information

whereis [22] which [23]


groups [24] id [25] last [26] lastcomm [27] logname [28] users [29] w [30] who [31] whoami
[32] lid [33] members [34]

Article printed from www.cyberciti.biz


URL to article: http://www.cyberciti.biz/faq/linux-ip-command-examples-usage-syntax/
URLs in this post:
[1] Image: http://www.cyberciti.biz/faq/linux-ip-command-examples-usage-syntax/ip-command-show-address-ethernetinterface/
[2] described above: #5
[3] set the length of the transmit queue: http://www.cyberciti.biz/faq/gentoo-centos-rhel-debian-fedora-increasingtxqueuelen/
[4] gigabit networks you can set maximum transmission units (MTU): http://www.cyberciti.biz/faq/centos-rhel-redhat-fedoradebian-linux-mtu-size/
[5] a plain route to network 192.168.1.0/24 via gateway 192.168.1.254: http://www.cyberciti.biz/faq/linux-route-add/
[6] route all traffic via 192.168.1.254 gateway connected via eth0 network: http://www.cyberciti.biz/faq/linux-setup-defaultgateway-with-route-command/
[7] previous subsection: #15
[8] cat: http://www.cyberciti.biz/faq/linux-unix-appleosx-bsd-cat-command-examples/
[9] dig: http://www.cyberciti.biz/faq/linux-unix-dig-command-examples-usage-syntax/
[10] host: http://www.cyberciti.biz/faq/linux-unix-host-command-examples-usage-syntax/
[11] ip: http://www.cyberciti.biz/faq/linux-ip-command-examples-usage-syntax/
[12] bg: http://www.cyberciti.biz/faq/unix-linux-bg-command-examples-usage-syntax/
[13] disown: http://www.cyberciti.biz/faq/unix-linux-disown-command-examples-usage-syntax/
[14] fg: http://www.cyberciti.biz/faq/unix-linux-fg-command-examples-usage-syntax/
[15] jobs: http://www.cyberciti.biz/faq/unix-linux-jobs-command-examples-usage-syntax/
[16] kill: http://www.cyberciti.biz/faq/unix-kill-command-examples/
[17] killall: http://www.cyberciti.biz/faq/unix-linux-killall-command-examples-usage-syntax/
[18] pwdx: http://www.cyberciti.biz/faq/unix-linux-pwdx-command-examples-usage-syntax/
[19] time: http://www.cyberciti.biz/faq/unix-linux-time-command-examples-usage-syntax/
[20] pidof: http://www.cyberciti.biz/faq/linux-pidof-command-examples-find-pid-of-program/
[21] pstree: http://www.cyberciti.biz/faq/unix-linux-pstree-command-examples-shows-running-processestree/
[22] whereis: http://www.cyberciti.biz/faq/unix-linux-whereis-command-examples-to-locate-binary/
[23] which: http://www.cyberciti.biz/faq/unix-linux-which-command-examples-syntax-to-locate-programs/
[24] groups: http://www.cyberciti.biz/faq/unix-linux-groups-command-examples-syntax-usage/
[25] id: http://www.cyberciti.biz/faq/unix-linux-id-command-examples-usage-syntax/
nixCraft is GIT UL++++ W+++ C++++ M+ e+++ d-

Page 6 of 7

[26] last: http://www.cyberciti.biz/faq/linux-unix-last-command-examples/


[27] lastcomm: http://www.cyberciti.biz/faq/linux-unix-lastcomm-command-examples-usage-syntax/
[28] logname: http://www.cyberciti.biz/faq/unix-linux-logname-command-examples-syntax-usage/
[29] users: http://www.cyberciti.biz/faq/unix-linux-users-command-examples-syntax-usage/
[30] w: http://www.cyberciti.biz/faq/unix-linux-w-command-examples-syntax-usage-2/
[31] who: http://www.cyberciti.biz/faq/unix-linux-who-command-examples-syntax-usage/
[32] whoami: http://www.cyberciti.biz/faq/unix-linux-whoami-command-examples-syntax-usage/
[33] lid: http://www.cyberciti.biz/faq/linux-lid-command-examples-syntax-usage/
[34] members: http://www.cyberciti.biz/faq/linux-members-command-examples-usage-syntax/
Copyrighted material
Copyright 2006-2014 nixCraft. All rights reserved. This print / pdf version is for personal non-commercial use only. Unless otherwise indicated, the
documents and graphics stored on this Web server, www.cyberciti.biz, are copyrighted. Links to these documents are permitted and encouraged. No
copies may be made without permission. More details - http://www.cyberciti.biz/tips/copyright

nixCraft is GIT UL++++ W+++ C++++ M+ e+++ d-

Page 7 of 7

You might also like