Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ip address of a server on Centos 7 in bash

Previously I used the following command in bash to find the main ip of my server

ipaddr=$(/sbin/ifconfig|grep inet|head -1|sed 's/\:/ /'|awk '{print $3}' | grep -v '127.0.0.1') 

But in centos7 it no longer works since ifconfig isn't available and the command no longer works even if I install ifconfig using yum install net-tools

What is the equivalent command for centos 7

Thanks a lot

like image 330
user2650277 Avatar asked Jan 14 '15 12:01

user2650277


People also ask

How do I find my IP address in CentOS 7?

In CentOS 7, we have three major utilities to display the device and address information: Newer IP command ( /sbin/ip ) Almost obsolete ifconfig command from net-tools package ( /sbin/ifconfig ) Versatile netstat command ( /usr/bin/netstat )

How do I find my IP address in bash?

To find out the IP address of Linux/UNIX/*BSD/macOS and Unixish system, you need to use the command called ifconfig on Unix and the ip command or hostname command on Linux. These commands used to configure the kernel-resident network interfaces and display IP address such as 10.8. 0.1 or 192.168. 2.254.

How do I find the IP address of my Linux server?

The best way to find IP address in Linux is using ip addr command. All we need is to open the terminal then type ip addr in the prompt. The number next to inet is our IP address. This command will list IP address, MAC address, MTU size and other information about a network interface.

What is my IP from command line?

From the desktop, navigate through; Start > Run> type "cmd.exe". A command prompt window will appear. At the prompt, type "ipconfig /all". All IP information for all network adapters in use by Windows will be displayed.


2 Answers

You can use hostname command :

ipaddr=$(hostname -I) 

-i, --ip-address: Display the IP address(es) of the host. Note that this works only if the host name can be resolved.

-I, --all-ip-addresses: Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback interface and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the order of the output.

like image 101
Alexander Avatar answered Sep 28 '22 21:09

Alexander


Enter the command ip addr at the console

enter image description here

like image 43
Rijul Sudhir Avatar answered Sep 28 '22 20:09

Rijul Sudhir