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
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 )
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.
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.
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.
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.
Enter the command ip addr
at the console
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With