logoCndocs
Network Interface and Configuration

ifconfig Command

The ifconfig (interface configuration) command is a powerful networking utility used to configure, manage, and display information about network interfaces on Linux and Unix-like operating systems. Think of it as a tool that helps you see and control how your computer connects to networks.

What is ifconfig and Why Should I Learn It?

If you're new to Linux or networking, you might wonder why this command matters. Here's why learning ifconfig is important:

  • See Your Network Connections: It shows you all the ways your computer connects to networks
  • Find Your IP Address: Quickly discover your computer's address on the network
  • Fix Network Problems: When your internet isn't working, this tool helps figure out why
  • Set Up Networks: Configure how your computer connects to networks
  • Network Security: Change network settings for better security

In simple terms, ifconfig is like a control panel for your computer's network connections!

How to Use ifconfig (Basic Syntax)

The basic way to use the ifconfig command is:

ifconfig [interface] [options]

Let's break this down:

  • ifconfig - The command itself
  • [interface] - The name of the network connection you want to work with (like eth0, wlan0)
  • [options] - Additional settings you want to apply

Don't worry if this seems confusing at first! We'll see plenty of examples that make it clear.

What if ifconfig Isn't Working?

If you try to use ifconfig and get an error message saying "command not found", don't panic! This just means the tool isn't installed on your system yet.

How to Install ifconfig

Modern Linux systems might not have ifconfig pre-installed. Here's how to install it:

For Ubuntu, Debian, Mint, and similar systems:

sudo apt-get install net-tools

For CentOS, Fedora, RHEL, and similar systems:

sudo yum install net-tools
# or on newer versions
sudo dnf install net-tools

After running one of these commands (depending on your Linux version), you'll be able to use ifconfig!

Common Ways to Use ifconfig (With Examples)

1. See All Your Network Connections

The most basic way to use ifconfig is without any options. This shows all your active network connections:

ifconfig
ifconfig command output showing network interfaces

When you run this command, you'll see information about each network connection, including:

  • Interface names (like eth0, wlan0, lo) - These are the names of your network connections
  • inet - This is your IPv4 address (like 192.168.1.10)
  • netmask - This defines your network range
  • ether - This is your MAC address (a unique hardware identifier)
  • RX/TX packets - Information about data received and sent

Beginner Tip: The "lo" interface (loopback) with IP 127.0.0.1 is always present - it's how your computer talks to itself!

2. Look at Just One Network Connection

If you only want to see information about a specific network connection:

ifconfig eth0

This shows details for just the "eth0" connection (which is often your main wired connection).

3. See ALL Connections (Even Inactive Ones)

To see all network interfaces, including ones that are turned off:

ifconfig -a

This is helpful when troubleshooting, as it shows connections that might be disabled.

4. Turn a Network Connection On

If a network connection is turned off, you can enable it with:

ifconfig eth0 up

This activates the eth0 interface, making it available for use.

5. Turn a Network Connection Off

Similarly, you can disable a network connection with:

ifconfig eth0 down

This can be useful when you want to temporarily disconnect from a network.

6. Change Your IP Address

You can manually set an IP address for a network connection:

ifconfig eth0 192.168.1.10

This assigns the IP address 192.168.1.10 to the eth0 interface.

7. Set Both IP Address and Netmask

For more complete configuration, you can set both the IP address and netmask:

ifconfig eth0 192.168.1.10 netmask 255.255.255.0

The netmask (255.255.255.0) defines which part of the IP address refers to the network and which part refers to the host.

8. Set a Broadcast Address

You can configure the broadcast address (used to send messages to all devices on the network):

ifconfig eth0 broadcast 192.168.1.255

9. Change Your MAC Address

For security or testing purposes, you might want to change your MAC address:

ifconfig eth0 hw ether 00:1a:2b:3c:4d:5e

Beginner Tip: A MAC address is a unique identifier assigned to your network hardware. Changing it can sometimes help bypass network restrictions, but should be used responsibly.

10. Create a Virtual Network Interface (Alias)

You can create a virtual interface with a different IP address:

ifconfig eth0:0 192.168.1.20

This creates a virtual interface called eth0:0 with IP 192.168.1.20, while keeping eth0 with its original IP.

11. Remove a Virtual Interface

To remove a virtual interface you've created:

ifconfig eth0:0 down

Common Options Explained

Here's a simple guide to the most useful options you can use with ifconfig:

OptionWhat It DoesExampleWhen To Use It
-aShows all network interfaces, even if they're turned offifconfig -aWhen troubleshooting missing connections
-sShows a shorter, simplified listifconfig -sWhen you just need a quick overview
upTurns on a network interfaceifconfig eth0 upWhen you need to activate a connection
downTurns off a network interfaceifconfig eth0 downWhen you want to disable a connection
netmaskSets the network maskifconfig eth0 netmask 255.255.255.0When configuring a network manually
broadcastSets the broadcast addressifconfig eth0 broadcast 192.168.1.255When setting up network broadcasting
mtuChanges the maximum packet sizeifconfig eth0 mtu 1500When optimizing network performance
hw etherChanges the MAC addressifconfig eth0 hw ether 00:1a:2b:3c:4d:5eFor network security or testing

Understanding What ifconfig Shows You

When you run the ifconfig command, you'll see output that looks something like this:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.10  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::215:5dff:fe01:7c8d  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:01:7c:8d  txqueuelen 1000  (Ethernet)
        RX packets 8470  bytes 1574533 (1.5 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4921  bytes 1356371 (1.3 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Let's break this down in simple terms:

First Line

  • eth0: This is the name of your network interface (like "eth0" for Ethernet)
  • flags: These are status flags that show the interface state
    • UP: The interface is active and working
    • BROADCAST: The interface can send messages to all devices on the network
    • RUNNING: The interface is ready to accept data
    • MULTICAST: The interface can send data to multiple recipients at once
  • mtu 1500: Maximum Transmission Unit - the largest packet size this interface can handle (1500 bytes)

Second Line

  • inet 192.168.1.10: This is your IPv4 address - your computer's address on the local network
  • netmask 255.255.255.0: This defines which part of the IP is the network portion and which is the host portion
  • broadcast 192.168.1.255: This is the address used to send messages to all devices on your network

Third Line

  • inet6: This is your IPv6 address (the newer version of IP addresses)

Fourth Line

  • ether 00:15:5d:01:7c:8d: This is your MAC address - a unique hardware identifier for your network card
  • txqueuelen 1000: The transmission queue length

Statistics Lines

  • RX packets/bytes: Information about data your computer has received
  • TX packets/bytes: Information about data your computer has sent
  • errors/dropped/overruns: Information about any problems with data transmission

Beginner Tip: The most important parts to look at are usually the interface name (eth0), your IP address (inet), and whether the interface is UP or not.

Finding Your IP Addresses

Your computer actually has two different types of IP addresses:

  1. Private IP Address - Used on your local network (like your home or office network)
  2. Public IP Address - Used on the internet (how websites see you)

Let's learn how to find both!

Finding Your Private IP Address

Your private IP address is what identifies your computer on your local network. It's usually something like 192.168.1.x or 10.0.0.x.

The easiest way to find it is with ifconfig - look for the "inet" value. But there are several other ways too:

1. Using the hostname command

hostname -I

This simple command shows just your IP address without all the extra information.

2. Using NetworkManager

If your system uses NetworkManager (most modern Linux distributions do):

nmcli dev show | grep IP4.ADDRESS

This shows your IP address along with the subnet mask in CIDR notation.

3. Using awk with ifconfig

This command filters the output of ifconfig to show just the IP addresses:

ifconfig | awk '/inet / {print $2}'

4. Using grep with the ip command

The modern alternative to ifconfig is the ip command. You can use it with grep:

ip addr show | grep -oP 'inet \K[\d.]+'

Finding Your Public IP Address

Your public IP address is how the internet sees you. It's assigned by your Internet Service Provider (ISP) and is usually shared by all devices on your network.

To find your public IP, you need to ask an external service on the internet:

1. Using curl with ifconfig.me

curl ifconfig.me

This contacts the ifconfig.me service, which tells you your public IP address.

2. Using wget with ifconfig.me

If you don't have curl installed, you can use wget:

wget -qO- ifconfig.me

3. Using dig with OpenDNS

This method uses DNS to find your public IP:

dig +short myip.opendns.com @resolver1.opendns.com

4. Using curl with icanhazip.com

Another web service that can tell you your public IP:

curl icanhazip.com

Beginner Tip: Your public IP address might change from time to time if your ISP uses dynamic IP assignment. If you're running a server from home, you might want to look into "Dynamic DNS" services.

The Modern Alternative: The ip Command

While ifconfig is a classic command that many Linux users know and love, newer Linux distributions are moving toward using the ip command instead. The ip command is part of the iproute2 package and offers more features.

If you're learning Linux networking today, it's good to know both commands. Here's how they compare:

What you want to doifconfig commandip command equivalent
See all network interfacesifconfigip addr show
See one specific interfaceifconfig eth0ip addr show dev eth0
Turn on a network interfaceifconfig eth0 upip link set eth0 up
Turn off a network interfaceifconfig eth0 downip link set eth0 down
Set an IP addressifconfig eth0 192.168.1.10ip addr add 192.168.1.10/24 dev eth0
Set a netmaskifconfig eth0 netmask 255.255.255.0ip addr add 192.168.1.10/24 dev eth0
Change a MAC addressifconfig eth0 hw ether 00:1a:2b:3c:4d:5eip link set dev eth0 address 00:1a:2b:3c:4d:5e

Beginner Tip: Notice how the ip command is more structured? It uses subcommands like addr and link to organize different types of network settings.

Setting Up a Gateway

To set up a default gateway (the router that connects your network to other networks or the internet), you'll need to use the route command along with ifconfig:

# First set your IP address and netmask
ifconfig eth0 192.168.1.10 netmask 255.255.255.0
 
# Then add the default gateway
route add default gw 192.168.1.1

In this example, 192.168.1.1 is the IP address of your router or gateway.

With the newer ip command, you would do:

# Set IP address and netmask
ip addr add 192.168.1.10/24 dev eth0
 
# Add default gateway
ip route add default via 192.168.1.1

Conclusion: Why ifconfig Matters

Even though ifconfig is considered "old" by some Linux distributions, it remains an essential tool to understand for several reasons:

  1. It's Simple: The command structure is straightforward and easy to remember
  2. It's Widespread: Many tutorials, guides, and older systems still use it
  3. It's Powerful: Despite its simplicity, it can handle most network configuration tasks
  4. It's Educational: Learning ifconfig helps you understand basic networking concepts

Whether you're a beginner just starting with Linux, a student learning networking, or an experienced system administrator, knowing how to use ifconfig will help you manage and troubleshoot network connections effectively.

Remember that networking is a fundamental skill in today's connected world. By mastering tools like ifconfig, you're building a solid foundation for more advanced networking concepts and technologies.

Happy networking!

Test Your Knowledge

Take a quiz to reinforce what you've learned

Exam Preparation

Access short and long answer questions for written exams

Share this page