Unix Shell – Set/Change IP

|

By default UNIX computers/servers are setup with a DHCP/Dynamic IP address, below is how to change the IP to static

Before you do either process understand what these settings should be:

  1. Network Interace you plan to modify (eth0, eth1, etc.)
  2. IP Address (address)
  3. Subnet Mask (netmask)
  4. Default Gateway (gateway)

Whether you choose the manual or the automatic process it is best if you do this on the machine itself whether through VMWare (or the like) or the physical machine. Once the IP is changed it’s also best if you restart the server/computer.

Manual Process

Assuming we are changing eth0 (make sure you change the correct one)

sudo nano /etc/network/interfaces

By default it should look something like this

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp

It is suggested that you insert a # in front of “iface eth0 inet dhcp” and then type under it the information you need.

The configuration will vary based on the network you use, for my test network I use 10.1.1.0/24 so my configuration would look like this

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
#iface eth0 inet dhcp
iface eth0 inet static
        address 10.1.1.5
        netmask 255.255.255.0
        gateway 10.1.1.1

Automated Process

sed -i 's|iface eth0 inet dhcp|#iface eth0 inet dhcp|' /etc/network/interfaces
echo 'iface eth0 inet static \n address 10.1.1.68 \n netmask 255.255.255.0 \n gateway 10.1.1.1' >> /etc/network/interfaces
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.