How to set a static IP Address from the Command Line in GNU/Linux using ifconfig and route

Assuming you want to make the following changes to the network device eth0

  1. Change the IP to the static value
  2. Set the Subnet Mask to
  3. Set the Default Gateway for the device to be

you can perform these changes using the following two commands

sudo ifconfig eth0 netmask;
sudo route add default gw eth0;


ifconfig is an application that allows you to configure a network interface.
It is used to configure the kernel-resident network interfaces. and it is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed.
If no arguments are given, ifconfig displays the status of the currently active interfaces. If a single interface argument is given, it displays the status of the given interface only; if a single -a argument is given, it displays the status of all interfaces, even those that are down. Otherwise, it configures an interface.


route is an application that allows you to show and manipulate the IP routing table. The primary use of route is to set up static routes to specific hosts or networks via an interface after it has been configured with the ifconfig program.
When the add or del options are used, route modifies the routing tables. Without these options, route displays the current contents of the routing tables.


Ubuntu Linux: How to register machine IPs and domain name to web-server using php

First of all you will need a page the will keep record of the registrations.

To do the paste the following code in a *.php file on a php-enabled webserver:


if (isset($_GET["ip"]) && isset($_GET["name"]))
  echo "Current Time " . gmdate("d/m/Y H:i:s e", $_SERVER['REQUEST_TIME']) . " IP " . $_GET["ip"] . " Hostname " . $_GET["name"] . "!<br/>"; 
  $file=fopen("cookies.txt","a") or $file=fopen("log.txt","x");
  fwrite($file,"Time " . gmdate("d/m/Y H:i:s e", $_SERVER['REQUEST_TIME']) . " IP " . $_GET["ip"] . " Hostname " . $_GET["name"] . "\n" );
  echo "Welcome Murloc!<br />";
  echo "Following the SSH Login Log File:<br /><br />";
  $file=fopen("log.txt","r") or $file=fopen("cookies.txt","x");
    echo fgets($file). "<br />";
  echo "Your IP is ".$_SERVER['REMOTE_ADDR']. "<br />Current Time " . gmdate("d/m/Y H:i:s e", $_SERVER['REQUEST_TIME']) . "<br /><br />";
  echo "Bye Bye Bob<br />";

The above php script will store your IP and Hostname if you provide them on the URL along with the time that the event happened and if you do not then it will list the registered IPs.

NOTE:Not sure how safe this code is

In order to invoke it / send you IP and hostname, issue form bash the following:

IPs=`ifconfig  | grep 'inet addr:'| grep -v '' | cut -d: -f2 | awk '{ print $1}'`
wget "http://www.example.com/ip.php?ip=$IPs&name=$HOSTNAME" -o /dev/null


Resolve IPs for Servers listed in a file using /etc/hosts

cat $NODEFILE | xargs -L 1 -I xx grep xx /etc/hosts | awk '{print $1}'

*NOTES:$NODEFILE contains a list of Hostnames that you want their IPs resolved.
xargs is used to get each Hostname and use on its own as a filter for the grep command that will parse the /etc/hosts file. In other words for each hostname the commands xx grep xx /etc/hosts | awk ‘{print $1}’ are issued. Also it is important to explain what xx is: xx is a variable name that we use, in order to show to the grep command where and how we want it to use the hostname that we got from the /etc/hosts file.
awk is removing all columns but the first where the IPs should be listed there.