Another way to print your IP address in Linux. This is a very useful one liner.

Posted: February 1, 2014. At: 12:58 PM. This was 4 years ago. Post ID: 6928
Page permalink.
WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire two weeks after they are set.

This simple one liner will print out your IP address and then IPv6 equivalent if you have it set.

[[email protected] ~]$ ifconfig enp6s1 | awk '/inet/ { print $2 } ' | sed -e s/addr://
192.168.1.2
fe80::213:46ff:fe3a:283

Here is a way to only print the IPv4 address.

[[email protected] ~]$ ifconfig enp6s1 | awk '/inet / { print $2 }'
192.168.1.2

This would be a useful one liner to have in a script to get your IP address and do something with it.

Here is one way you could do this.

[[email protected] ~]$ ping $(ifconfig enp6s1 | awk '/inet / { print $2 }')
PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.062 ms
64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.060 ms
64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=0.056 ms
64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=0.049 ms
64 bytes from 192.168.1.2: icmp_seq=5 ttl=64 time=0.058 ms
64 bytes from 192.168.1.2: icmp_seq=6 ttl=64 time=0.058 ms
64 bytes from 192.168.1.2: icmp_seq=7 ttl=64 time=0.064 ms
^C
--- 192.168.1.2 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 5999ms
rtt min/avg/max/mdev = 0.049/0.058/0.064/0.006 ms

This is very useful indeed. This would be very good for writing a script.

No comments have been made. Use this form to start the conversation :)

Leave a Reply