Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to have a bash script for Centos machine. I want the script to show me the current IP Address /subnet mask / Default GW. and give me option to enter a new IP Address /subnet Mask / Default GW. It should do it for ETH0.

Can someone have an idea how to do it?


share|improve this question
system-config-network-tui could probably do this. Any particular reason why it has to be a bash script? – nrolans Nov 20 '11 at 21:30

I'm not going to write it all for you, but here's something that works in Ubuntu:

ifconfig eth0 | grep "inet addr:" | awk -F: '{ print $2 }' | awk '{ print $1 }'

That will echo the current ip address in eth0.

share|improve this answer

ifconfig(1) is the standard command for manipulating IP parameters of interfaces. See man ifconfig for how to use it, but e.g.

ifconfig eth0 netmask

will set the IP address and netmask for eth0.

To read the interface parameters, you can use grep/sed/perl to parse the output of ifconfig eth0, but if you have the ifdata command, a less painful way to get e.g. just the IP address is

ifdata -pa eth0

In Ubuntu, ifdata is part of the moreutils package.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.