Find network interface names and their IPs in Linux

Linux School Tech
4 min readJun 29, 2024

--

How to find the names of network interfaces and their IP addresses in Linux? How to find the network interface name by IP in Linux? In this video, it is shown that using different commands such as networkctl, nmcli, hostname, ip and ifconfig, we can get the names of network interfaces and their IPs.

A) neworkctl Command

networkctl is a command-line utility used to manage and display network configuration and status on Linux systems. It is a part of the systemd suite of tools and is used to manage network interfaces, connections, and services.

networkctl provides a more modern and user-friendly alternative to the traditional ifconfig and ip commands, which are often used to manage network interfaces on Linux systems.

Here are some of the key features and uses of networkctl:

  1. Network interface management: networkctl allows you to create, modify, and delete network interfaces, as well as configure their properties such as IP addresses, netmasks, and gateway addresses.
  2. Network connection management: networkctl can be used to establish and manage network connections, including Wi-Fi, Ethernet, and VPN connections.
  3. Network service management: networkctl can be used to start, stop, and restart network services such as DHCP clients, DNS resolvers, and other network-related services.
  4. Network status monitoring: networkctl provides information about the current network status, including the list of active network interfaces, their states (e.g., up or down), and the IP addresses associated with each interface.
  5. Command-line interface: networkctl has a command-line interface that allows you to issue commands to manage networks and interfaces using a simple and intuitive syntax.

B) nmcli Command

nmcli is a command-line tool used to manage network connections and devices on Linux systems. It is part of the NetworkManager suite of tools and provides a command-line interface to manage and configure network connections, devices, and settings.

nmcli is often used in place of other network configuration tools such as ifupdown, dhcpcd, and wpa_supplicant, as it provides a more unified and intuitive way to manage network connections.

Here are some common uses of nmcli:

  1. Connection management: nmcli can be used to create, modify, and delete network connections, including wireless, wired, and VPN connections.
  2. Device management: nmcli can be used to enable, disable, and manage network devices, such as Ethernet interfaces, Wi-Fi adapters, and USB modems.
  3. Connection properties: nmcli can be used to set various properties for network connections, such as IP addresses, DNS servers, proxy settings, and more.
  4. Network service management: nmcli can be used to start, stop, and restart network services such as DHCP clients, DNS resolvers, and other network-related services.

C) hostname Command

The hostname command is a Unix-like command that allows you to display or modify the hostname of a computer. The hostname is the name given to a computer that identifies it on a network.

Displaying the hostname

When you run the hostname command without any options or arguments, it will display the current hostname of the computer:

$ hostname
linuxadminhacks

Setting the hostname

You can use the hostname command to set a new hostname for the computer. To do this, use the -s option followed by the new hostname:

sudo hostname -s mynewhostname

This will set the hostname to mynewhostname. Note that you may need to use sudo to run this command, depending on your system configuration.

D) ip and jq (json query) Command

The ip command is a powerful command-line utility in Linux and other Unix-like operating systems that allows you to manage and configure network interfaces, routes, and network protocols. It is a replacement for the older ifconfig command and is more flexible and feature-rich.

Here are some common ip commands:

  • ip addr: Displays or sets IP addresses on a network interface.
  • ip link: Displays or sets link properties, such as MTU, broadcast, and DNS servers.
  • ip route: Displays or sets IP routes.
  • ip neigh: Displays or sets neighbor discovery settings.
  • ip tunnel: Creates or deletes IP tunnels.
  • ip maddr: Displays or sets multicast addresses.

Displaying IP addresses on a network interface

ip addr show eth0

Setting a new IP address on a network interface

ip addr add 192.168.1.100/24 dev eth0

Displaying routes

$ ip route show

Setting a new route

$ ip route add 192.168.2.0/24 via 192.168.1.1

jq is a lightweight and flexible command-line JSON processor that allows you to parse, filter, and manipulate JSON data. It is a powerful tool for working with JSON data on the command line.

Basic syntax:

The basic syntax of the jq command is as follows:

jq [options] '[filter]' input.json

Where:

  • [options]: Optional flags that modify the behavior of jq.
  • [filter]: A JSON path or a filter expression to apply to the input data.
  • input.json: The JSON file or string to process.

Filtering and extracting data:

jq allows you to extract specific data from a JSON file or string using a filter expression. A filter expression is a JSON path that selects specific fields or values from the input data.

For example, suppose you have a JSON file called data.json containing the following data:

{
"name": "John",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA"
}
}

You can use the following jq command to extract the value of the name field:

jq '.name' data.json

This would output:

"John"

Now the questions

Q1) How to find the names of network interfaces and their IP addresses in Linux?

Q2) How to find the network interface name by IP in Linux?

Read More

My YouTube Channel

More shell script videos and linux tutorials on my YouTube Channel.

--

--