An IP address is a unique identifier assigned to a computer or device connected to the internet.
In this blog post, we will discuss three methods to find the IP address of a Linux system: using theifconfigcommand with theIPcommand and with theIP-RouteCommand.
Let's begin!
Procedure to find IP address in Linux
- Open the terminal application.
- Enter the command ifconfig -a or ip addr or ip route
- Press Enter to run the command.
- The output shows the IP address for all network interfaces.
Related:Step-by-step guide on how to fix a network problem on Linux
Find the IP address using the ip addr command in Linux
The most convenient way to find IP addresses in Linux is to useIP-AdrCommand. All we need is to open the terminal, then type ip addr in the command prompt and hit enter.
The number next to inet is the IP address. This command lists the IP addressMAC-Address,person sizeand other information about a network interface.
I find this command very useful as it gives me more detailed information about my system's IP address and network interface status.
We can also enter ip address or ip a for short.
- ip addr - Display IP address for all interfaces
- ip addr show dev em1 - Display information for device em1 only
- ip addr show up - Show IP address for the running interfaces
By default,Dieip addr befehldisplays IP information of all network interfaces on the Linux system.
This is the command output on my system.
# IP-Adr
1:please refer: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
Link/Loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet127.0.0.1/8 range host lo
valid_lft forever preferred_lft forever
2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc mq state UP Gruppenvorgabe qlen 1000
link/ether 02:3f:4d:eb:52:e7 brd ff:ff:ff:ff:ff:ff
inet10.1.0.143/24 brd 10.1.0.255 scope global dynamic noprefixroute eth0
valid_lft 3198 sec. Preferred_lft 3198 sec
3:eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP-Gruppenstandardwert qlen 1000
link/ether fa:16:3e:18:3a:76 brd ff:ff:ff:ff:ff:ff
inet10.254.222.37/23 brd 10.254.223.255 scope global dynamic eth0
valid_lft 70000 sec preferred_lft 70000 sec
From the example above we can see that there are three network interfaces l0, eth0 and eth1.
The IP address for eth0 is 10.1.0.143 and the IP address for eth1 is 10.254.222.37.
This output is easy to understand.
- The internet entry shows the IP address for the interface.
- UP means the interface is operational;
- Brd indicates that the interface is connected to a network that supports broadcast messaging (Ethernet).
- LOOPBACK indicates which device (lo) is the loopback address.
- Themaximum transmission unit(PERSON) oneth0 is 1500 bytes. This determines the size of the largest packet that can be transmitted over that interface (and is sometimes "tuned" to other values to improve performance).
If you have more than one network interface on your system, you can useShow IP address devcommand toView the IP address of the specific interface.
The syntax for this command is:
ip addr show dev Schnittstellenname
Let's say if I want to find the IP address of interface eth1 I use the following command:
$ ip addr show dev eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP-Gruppenstandardwert qlen 1000
link/ether fa:16:3e:18:3a:76 brd ff:ff:ff:ff:ff:ff
inet10.254.222.37/23 brd 10.254.223.255 scope global dynamic eth0
valid_lft 69786 sec. Preferred_lft 69786 sec
In addition, I would like to share another commandIP address is displayed.
The "ip addr show up" command is used to display the network interfaces that are currently in an "up" state, meaning they are enabled and active.
There are a number of other options you can use with the ip addr command. You can type command for more informationIP addr help.
To learn more about ip command I would recommend the following article.
3 Useful Linux Commands You Must Know About Networking
(Video) Kali Linux - How to discover the IP & MAC addresses of all devices on a network
Get the IP address using the ifconfig command in Linux
TheifconfigThe command is a general command used to display IP related information in Linux. The command output includes the interface name, assigned IP address, netmask, broadcast address, and other network-related information.
If no argument is given, this command displays the configuration details of all active network interfaces on the system.
Also, if you are only interested in a specific interface eth0, use: ifconfig eth0
To use this command, simply type ifconfig in the shell prompt and press Enter.
# ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 9001
inet10.1.0.143Netmask 255.255.255.0 Broadcast 10.1.0.255
ether 02:3f:4d:eb:52:e7 txqueuelen 1000 (Ethernet)
RX-Pakete 1142453 Bytes 77488923 (73,8 MiB)
RX-Fehler 0 Dropped 0 Overruns 0 Frame 0
TX-Pakete 5835535 Byte 8505814381 (7,9 GiB)
TX errors 0 dropped 0 overruns 0 carriers 0 collisions 0
The ifconfig command is a powerful network configuration utility that can be used to view or configure a network interface.
In addition, it should be noted that the ifconfig command is considered a deprecated command and is replaced by the iproute2 package in many distributions.
This means that it may not be supported in newer versions of the operating system and may not be compatible with some distributions.
If you need to install it, you can follow this guide.
3 ways to fix ifconfig command not found in Linux
Related:10 Linux Networking Interview Questions You Must Know
Get the IP address using the ip route command in Linux
TheIP-RouteThe command can be used to find the IP address of a Linux system.
This command will print our system's IP address along with other network details. Once we run the command we will see an output similar to the following text.
$ ip-Route
Default above 10.1.0.1 dev eth0 proto dhcp metric 100
10.1.0.0/24 dev eth0 proto kernel space link source10.1.0.143metric 100
The information shows us the IP routing table of the server andnetwork gateway.
In these lines, the IP addresses following "src" are the IP addresses configured on the server.
In the example above, we can see that the server has an IP address of `10.1.0.143`.
Check IP address with bash script in Linux
We can get a list of network interfaces and IPv4 addresses on our server by running the following command:
ip -4 -o on | cut -d '' -f 2.7 | cut -d '/' -f 1
The output lists the interface names on the left and the corresponding IP addresses on the right.
lo 127.0.0.1
eth0 10.1.0.79
Let's break down the command:
ip -4 -o a : print each record on a single line, replacing newlines with the‘\’Character. This is handy when you want to get the IP address in the bash script.
The Cut command is used to extract a subset of rows from a command output or to extract a subset of columns.
The -d parameter specifies the delimiter used to separate the fields.
The -f parameter specifies the field number(s) you want to extract.
Here's an example: cut -d',' -f1,3 filename.txt This command extracts the first and third columns from the lines in the filename.txt file, using commas as separators.
Related:Bash shell script to lookup IP address
Find public IP address in Linux
The way to get your public IP address is Google it. Type in "What is my IP address" or "How do I find my IP address" and Google will tell you.
Your IP address is public on the internet. We can get it from many online tools. It can be easily followed by others.
Alternatively, you can try using curl to access an external service that recognizes your public IP address. There are quite a few out there - I just went with the first search result I found:
curl ifconfig.me/all
IP Address: 64.104.44.110
remote_host: not available
user_agent: curl/7.84.0
Port: 35116
Language:
refer:
Connection:
stay alive:
Method: GET
Encoding:
Pantomime: */*
character set:
via: 1.1 google
Forwarded: 64.104.44.110, 34.160.111.145, 35.191.8.3%
There is another way to find out your public IP address. Perform a DNS lookup for myip.opendns.com. It's a service provided by OpenDNS, and it's lightning fast!
dig +short myip.opendns.com @resolver1.opendns.com
64.104.44.110
What is an IP address?
From Wikipedia, the free encyclopediaIP Addressused in a system for communication. It's a numeric designation like 192.0.2.1. An IP address serves two main functions: network interface identification and location addressing.
An IP address is the identifier that allows our device to send or receive packets of data over the internet. It contains information about our location and therefore makes devices availableTwo way communication.
Most internet service providers still use IPv4.It is based on 32 binary bits, consists of four numbers from 0 to 255 and is separated by dots. Example: 103.86.98.1.
A Linux server can have more than one network interfacee. This is often the case with web servers and other devices that need to connect to different networks. For example, a web server may need to be able to connect to both the internet and a local area network.
A network interface can have more than one IP address. The first address is called the primary address while the others are called secondary addresses or aliases. The use of secondary IP addresses is common for web servers.
For example, a web server may have a primary IP address that is used to connect to the internet and a secondary IP address that is used to connect to a local area network. This means that the server can be reached from both networks.
In the example below, there are two IP addresses10.252.10.51 and 10.252.10.52on the physical interface eth0.
eth0 Link encap:Ethernet HWaddr 00:50:56:8F:44:C7
inet address:10.252.10.51Bcast:10.252.10.255 Mask:255.255.255.0
eth0:0 Link encap:Ethernet HWaddr 00:50:56:8F:44:C7
inet address:10.252.10.52Bcast:10.252.10.255 Mask:255.255.255.0
3 ways to find MAC addresses in Linux