How to find the IP address of the local Linux workstation

Posted on

How to find the IP address of the local Linux workstation – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about linux, centos, ip, shell-scripting, .

I am creating a shell script that should do some local settings and echo the IP of the machine in a text file. The reason I need to do this is to get the IP in a PHP script and use it from there as root for a site.

I can get the IP using ifconfig but I need to strip the useless information. I’d like to do this in the shell script not in the PHP file.

The OS is CentOS 5.5, I need the IP of eth0.

Solution :

Well ip -4 -o addr show dev eth0 will get you a reasonably nice one line output showing the address but it will need some parsing to extract the address. Something like this should work:

ip -4 -o addr show dev eth0 | awk '{ gsub(//[0-9]+$/, "", $4); print $4 }'

I personally will break the tie between the shell script and the php script to avoid both tightly coupled. If it is the servers IP address you want then, fire your script separately and obtain the servers IP programmatically. You can do this as instructed here. This way you only have logical dependence on the shell script.

Leave a Reply

Your email address will not be published.