Catalog / Networking Utilities Cheatsheet (ifconfig, ip)

Networking Utilities Cheatsheet (ifconfig, ip)

A quick reference guide to using `ifconfig` and `ip` commands for network configuration and troubleshooting on Linux systems.

Basic Interface Configuration (ifconfig)

Displaying Interface Information

ifconfig - Displays information for all active network interfaces.

ifconfig <interface> - Displays detailed information for a specific interface.

ifconfig -a - Displays information for all interfaces, including inactive ones.

Configuring IP Address and Netmask

ifconfig <interface> <ip_address> netmask <netmask>

Assigns an IP address and netmask to the specified interface.
Example: ifconfig eth0 192.168.1.100 netmask 255.255.255.0

ifconfig <interface> <ip_address>/<cidr>

Assigns an IP address using CIDR notation.
Example: ifconfig eth0 192.168.1.100/24

Enabling/Disabling Interfaces

ifconfig <interface> up

Brings the specified interface up.
Example: ifconfig eth0 up

ifconfig <interface> down

Brings the specified interface down.
Example: ifconfig eth0 down

Setting the MTU

ifconfig <interface> mtu <value>

Sets the Maximum Transmission Unit (MTU) for the specified interface.
Example: ifconfig eth0 mtu 1400

Modern Interface Configuration (ip)

Displaying Interface Information

ip addr show or ip a - Displays IP addresses and interface information.

ip link show or ip l - Displays link layer information (MAC address, interface state).

ip route show or ip r - Displays the routing table.

ip neigh show - Displays the neighbor table (ARP cache).

Managing IP Addresses

ip addr add <ip_address>/<cidr> dev <interface>

Adds an IP address to the specified interface.
Example: ip addr add 192.168.1.100/24 dev eth0

ip addr del <ip_address>/<cidr> dev <interface>

Removes an IP address from the specified interface.
Example: ip addr del 192.168.1.100/24 dev eth0

Managing Link State

ip link set <interface> up

Brings the specified interface up.
Example: ip link set eth0 up

ip link set <interface> down

Brings the specified interface down.
Example: ip link set eth0 down

ip link set <interface> mtu <value>

Sets the MTU for the specified interface.
Example: ip link set eth0 mtu 1400

Managing Routes

ip route add default via <gateway_ip>

Adds a default gateway.
Example: ip route add default via 192.168.1.1

ip route del default via <gateway_ip>

Deletes a default gateway.
Example: ip route del default via 192.168.1.1

Advanced `ip` Commands

Working with VLANs

ip link add link <parent_interface> name <vlan_interface> type vlan id <vlan_id>

Creates a VLAN interface.
Example: ip link add link eth0 name eth0.10 type vlan id 10

ip link set <vlan_interface> up

Brings the VLAN interface up.
Example: ip link set eth0.10 up

ip addr add <ip_address>/<cidr> dev <vlan_interface>

Assigns an IP address to the VLAN interface.
Example: ip addr add 192.168.10.100/24 dev eth0.10

Managing Network Namespaces

ip netns add <namespace_name>

Creates a new network namespace.
Example: ip netns add ns1

ip netns exec <namespace_name> <command>

Executes a command within a network namespace.
Example: ip netns exec ns1 ip addr show

ip link set <interface> netns <namespace_name>

Moves an interface to a network namespace.
Example: ip link set eth1 netns ns1

Using `ip monitor`

ip monitor - Monitors changes to network devices, addresses, routes and link state in real time.

ip monitor link - Monitors link state changes.

ip monitor addr - Monitors address changes.

ip monitor route - Monitors route changes.

Troubleshooting and Examples

Common Problems and Solutions

Problem: Cannot connect to the internet.
Solution: Check the default gateway with ip route show. Ensure the gateway IP is correct and reachable. Verify DNS settings in /etc/resolv.conf.

Problem: Interface is down.
Solution: Bring the interface up with ip link set <interface> up or ifconfig <interface> up. Check for any hardware issues or driver problems.

Problem: IP address conflict.
Solution: Ensure that no other device on the network is using the same IP address. Use ping to check if the IP address is already in use.

Problem: Incorrect MTU size.
Solution: Adjust the MTU size using ip link set <interface> mtu <value>. Too large MTU will result in fragmentation, too small will reduce throughput.

Example Scenarios

Scenario: Assign a static IP address to eth0.
Commands:

sudo ip addr add 192.168.1.150/24 dev eth0
sudo ip link set eth0 up
sudo ip route add default via 192.168.1.1

Scenario: Create a VLAN interface on eth0 with VLAN ID 20 and IP address 192.168.20.1/24.
Commands:

sudo ip link add link eth0 name eth0.20 type vlan id 20
sudo ip addr add 192.168.20.1/24 dev eth0.20
sudo ip link set eth0.20 up

Scenario: Move interface eth1 to network namespace ns1.
Commands:

sudo ip netns add ns1
sudo ip link set eth1 netns ns1
sudo ip netns exec ns1 ip link set eth1 name veth1
sudo ip netns exec ns1 ip link set veth1 up