Get ipv4 address linux

3 Easy Ways to Find IP address in Linux (private and public)

From Wikipedia, IP address is a numerical label assigned to each device in a computer network that uses the Internet Protocol for communication.

It serves as a unique identifier for devices within a network, enabling them to send and receive data across the network. An IP address is represented in a dotted decimal format, such as 192.0.2.1,

There are two types of IP addresses: private IP addresses and public IP addresses.

Private IP addresses are used for internal communication within a private network, while public IP addresses are used for communication over the Internet and allow devices to be accessible from anywhere on the Internet.

You can check the private IP address in Linux using commands such as “ifconfig” (deprecated), “ip addr show” or “ip route” command. To check the public IP address in Linux, you can use the command “curl ifconfig.me”.

  • ifconfig -a
  • ifconfig eth0
  • ip addr show
  • ip addr show eth0
  • ip a
  • ip route
  • curl ifconfig.me

In this article, we will discuss these commands in details. Let’s get started!

If you’re new to the world of Linux, you can refer to “Mastering the Linux Command: A Beginner’s Guide.” This in-depth article will be your guiding light, providing you with the foundational knowledge and skills required to navigate and wield the Linux command line interface (CLI) effectively and with assurance.

Procedures to find ip address in Linux

  • Open the terminal application.
  • Type ifconfig -a or ip addr or ip route command
  • Press Enter to run the command.
  • The output will display the private IP address for all the network interfaces.
  • Run curl ifconfig.me to get the public IP address.

You can also check out the following video to get the details.

Find IP address with ip addr command in Linux

From Redhat website, the ip addr command is a powerful tool in Linux used to display and manage network interface information, including IP addresses.

To find IP addresses using the ip addr command in Linux, you can follow these steps:

  1. Open a terminal: Launch a terminal or command prompt on your Linux system. This will provide you with a command-line interface.
  2. Run the ip addr command: Type the following command and press Enter: ip addr
  3. View the IP addresses: The ip addr command will display the network interfaces along with their associated IP addresses. Look for the lines starting with “inet” or “inet6” to identify the IP addresses.
    • IPv4 addresses are listed under “inet” followed by the IP address and subnet mask.
    • IPv6 addresses are listed under “inet6” followed by the IP address and prefix length.

The following is the command output in my system.

# ip addr
1: lo: 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
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever

Читайте также:  Ftp command with port in linux

2: eth0: mtu 9001 qdisc mq state UP group default qlen 1000
link/ether 02:3f:4d:eb:52:e7 brd ff:ff:ff:ff:ff:ff
inet 10.1.0.143/24 brd 10.1.0.255 scope global dynamic noprefixroute eth0
valid_lft 3198sec preferred_lft 3198sec

3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether fa:16:3e:18:3a:76 brd ff:ff:ff:ff:ff:ff
inet 10.254.222.37/23 brd 10.254.223.255 scope global dynamic eth0
valid_lft 70000sec preferred_lft 70000sec

From the above example, 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.

The ip addr command is installed by default in most distributions of Linux, including Ubuntu. It is part of the iproute2 suite.

The ip addr command provides the following information for each network interface:

  • Interface name: The name of the network interface, such as eth0 or wlan0.
  • Link state: Indicates whether the interface is UP or DOWN.
  • MAC address: The Media Access Control (MAC) address of the interface.
  • IP addresses: Displays both IPv4 and IPv6 addresses associated with the interface.
  • Network prefixes: Shows the subnet mask or prefix length for each IP address.
  • Additional details: Provides additional information, such as the interface’s MTU (Maximum Transmission Unit), flags, and network namespaces.

I find this command very useful, as it gives me more detailed information about my system’s IP address and the status of the network interface.

In addition to the ip addr command, you can also use the shorthand version ip a to achieve the same result.

Using the shorthand version ip a can save a bit of typing while still allowing you to quickly retrieve network interface and IP address information in Linux systems.

If you have multiple network interfaces in Linux, you can use ip addr show dev + interface name to view the IP address of the specific interface. Each interface will have its own set of IP addresses.

The syntax for this command is:

ip addr show dev interface_name

Let’s say if I want to find the ip address of interface eth1, I will use the following command:

$ ip addr show dev eth1
3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether fa:16:3e:18:3a:76 brd ff:ff:ff:ff:ff:ff
inet 10.254.222.37/23 brd 10.254.223.255 scope global dynamic eth0
valid_lft 69786sec preferred_lft 69786sec

We can get a list of the network interfaces and IPv4 addresses on our server by running the following command:

ip -4 -o a | cut -d ‘ ‘ -f 2,7 | cut -d ‘/’ -f 1

The output lists the interface names on the left and the associated IP addresses on the right.

Let’s break down the command:

  • ip -4: Display only IPv4 addresses.
  • ip -o: Output each record on a single line, replacing line feeds with the ‘\’ character.
  • ip a: Display addresses for all network interfaces. it is short for ip addr.
  • cut -d: Specifies the delimiter used to separate the fields.
  • cut -f: Specifies the field number(s) to extract.
  • cut: The cut command is used to extract a subset of the lines or columns from a command output

In addition, I would like to mention another useful command, “ip addr show up” which displays the network interfaces and their corresponding IP addresses that are currently up or active.

Читайте также:  Linux рисование блок схем

This can be particularly helpful when troubleshooting network connectivity issues or when you only need information about the active interfaces.

Here are the useful variations and options of the ip addr command in Linux:

  • ip addr: Show IP address for all interfaces
  • ip addr show dev em1: Display information only for device em1
  • ip addr show up: Show IP address for the running interfaces

For more information, you can type command ip addr help or refer to this article.

Get Ip address with ifconfig command in Linux

The ifconfig command is a common command that is used to view IP related information in Linux.

The command output includes the interface name, its assigned IP address, netmask, broadcast address, and other network-related information.

To get the IP address using the ifconfig command in Linux, follow these steps:

  1. Open a terminal: Launch a terminal or command prompt on your Linux system. This will provide you with a command-line interface.
  2. Run the ifconfig command: Type the following command and press Enter: ifconfig
  3. If no argument is given, this command displays the configuration details of all active network interfaces on the system.
  4. If you are only interested in certain interface like eth0, use: ifconfig eth0

# ifconfig -a
eth0: flags=4163 mtu 9001
inet 10.1.0.143 netmask 255.255.255.0 broadcast 10.1.0.255
ether 02:3f:4d:eb:52:e7 txqueuelen 1000 (Ethernet)
RX packets 1142453 bytes 77488923 (73.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5835535 bytes 8505814381 (7.9 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

The ifconfig command is a powerful network configuration utility that can be used to display or configure a network interface.

  • ifconfig: Command used to configure and display network interfaces.
  • ifconfig -a: Display information about all network interfaces, including those that are down.
  • ifconfig [interface]: Display information about a specific network interface, such as eth0 or wlan0.
  • ifconfig -s: Display a short list of network interfaces, showing only the basic information.
  • ifconfig -v: Display more detailed information about the network interfaces.

It should be noted that ifconfig command is considered as deprecated command and it is replaced by ip command in many distributions.

This means that it may not be supported in newer versions of the operating system and it may not be compatible with some distributions.

If you need to install it, you can refer to the this guide: 3 ways to fix ifconfig command not found in Linux

Get Ip address with ip route command in Linux

The “ip route” command is primarily used to manage routing tables and network routes rather than directly checking IP addresses. However, you can use the “ip route” command to indirectly help you determine the IP address.

To get the IP address using the ip route command in Linux, you can follow these steps:

  1. Open a terminal: Launch a terminal or command prompt on your Linux system. This will provide you with a command-line interface.
  2. Run the ip route command: Type the following command and press Enter: ip route
  3. View the IP address: The ip route command will display the routing table, which includes the IP addresses and network prefixes associated with various destinations.Here’s an example output:
    default via 192.168.1.1 dev eth0 proto static metric 100
    192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10 metric 100
    In the above example, the IP address is 192.168.1.10.
  4. Note down the desired IP address: Identify the IP address you are interested in and make a note of it for further reference.
Читайте также:  Get network name linux

Once we execute the command we will see an output similar to the text below.

$ ip route
default via 10.1.0.1 dev eth0 proto dhcp metric 100
10.1.0.0/24 dev eth0 proto kernel scope link src 10.1.0.143 metric 100

In the example output above, the first line represents the default route. It states that any traffic without a specific route should be sent via the gateway 10.1.0.1 through the network interface eth0.

The second line indicates a specific route for the network 10.1.0.0/24. It specifies that traffic destined for this network should be sent directly through the network interface eth0, with the source IP address of 10.1.0.143.

The information shows us the server’s IP routing table and network gateway.

On these lines, the IP address following `src` are the IP(s) configured on the server.

In the example above, we can see that the server has an IP address of `10.1.0.143`.

Find public IP address in Linux

To retrieve the public IP address in Linux, you can use various methods. Here are a few commonly used approaches:

  1. Google it. Type in “what is my IP address” or “how to find my IP address,” and Google will tell you.
  2. Using curl or wget with external services: Open a terminal and execute one of the following commands:Using curl:
dig +short myip.opendns.com @resolver1.opendns.com 
nslookup myip.opendns.com resolver1.opendns.com | awk '/^Address: / < print $2 >' 

What is Ip address?

An IP address is the identifier that enables our device to send or receive data packets across the internet. It holds information related to our location and therefore making devices available for two-way communication.

Most internet service providers still use IPv4. It’s based on 32 binary bits, consists of four numbers from 0 to 255, and is separated by dots. For example, 103.86.98.1.

A Linux server can have more than one network interface. This is often the case with web servers and other devices that need to be able to connect to different networks. For example, a web server may need to be able to connect to both the internet and a local 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. Using secondary IP addresses is common with 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 network. This allows the server to be reachable from both networks.

In the following example, there are two IP addresses 10.252.10.51 and 10.252.10.52 on the physical interface eth0.

eth0 Link encap:Ethernet HWaddr 00:50:56:8F:44:C7
inet addr:10.252.10.51 Bcast:10.252.10.255 Mask:255.255.255.0
eth0:0 Link encap:Ethernet HWaddr 00:50:56:8F:44:C7
inet addr:10.252.10.52 Bcast:10.252.10.255 Mask:255.255.255.0

Источник

Оцените статью
Adblock
detector