Using the ip command

The ip command is a handy tool that can replace much of ifconfig and route. Unfortunately, the man page lacks examples, and so this page is an effort to rectify that. I'm only going to cover some of the basic uses of the command, doing what I used to do with ifconfig.

As far as I know, the ip command is Linux only. I know that it's not in FreeBSD.

To give an interface an address with netmask, ip uses what is sometimes called slash notation. Instead of ifconfig's 192.168.1.2 netmask 255.255.255.0 it will use 192.168.1.2/24. For networks with the common subnet of 255.255.255.0 we use /24. For networks with a netmask of 255.255.0.0 we use /16. For networks using the 255.0.0.0 mask we use /8. If your network uses something else, the chances are that you know how to figure out the slash notation. I'm not going to go into it here, though I have another page on subnetting. You count the number of 1's in the netmask, after putting it into binary. That's covered on the other page.

So if we have a typical home network address, it might have a router that has an address of 192.168.1.1. That's the gateway. We'll give the interface an address of 192.168.1.20. The syntax would be
ip addr add 192.168.1.20/24 dev eth0

Hopefully, this is clear. addr is address, add is add. dev is the device, in this case eth0.

The equivalent of route add -net 192.168.1.0 netmask 255.255.255.0 eth0 would be
ip route add 192.168.1.0/24 dev eth0

Then, add a gateway. (As if typing route add default gw 192.168.1.1)
ip route add default via 192.168.1.1

To see your IP address, rather than typing ifconfig you use
ip addr show dev eth0

If you leave out the dev eth0 it will show the IP information for all interfaces.

To bring an interface up
ip link set eth0 up

That's it for now. The ip command has many different abilities, detailed in the man page. I'll add other examples as I run into them. In RH based systems, at least, there are also some .ps (postscript) documents giving more details and examples. These are located in /usr/share/doc/iproute-<version_number> on RedHat based systems. As they are in postscript format, they have to be read with evince, gv, or another document viewer capable of viewing ps.

This is a pretty good cheatsheet.