How to Set a Static IP on a Linux Machine

Step-by-step guide

Here’s a little primer on static Internet Protocol (IP) Addresses. Computers usually have the ability to grab a Dynamic Host Configuration Protocol (DHCP) address by default. Most networks are set to give out these dynamic IP addresses to any device that gets on the network and says, “I’m here! Give me an IP address so that I can use Google!”

The trouble with DHCP addresses is that they are dynamic. When a DHCP “lease” runs out, the computer or device may grab another available DHCP IP address. Normally this is ok and allows seamless Internet access to any computer; however, sometimes the need arises to statically assign an IP address for remote access, file transfers, or any other number of reasons.

To get a static IP address for your computer, submit a help ticket at http://cns.utexas.edu/help. Once you receive an IP assignment, follow the instructions below to get the IP Address and additional needed information onto your computer’s network connection.

NOTE: To make these changes, you must have an account with administrative rights on the machine.  You will either need to login as root, or be able to use the "sudo" command on your machine.  In the following, we assume you are logged in as root.

NOTE: Since the graphical interfaces change so much between Linux distributions, we're only going to show command line configuration here.  While the command line interface does change between Linux distributions, it changes less than the graphical interfaces.

NOTE: We provide directions below for the most popular Linux distributions in CNS.  If you use another distribution (like Gentoo, OpenSUSE, etc), you can contact the CNS Help Desk at http://cns.utexas.edu/help/ for additional instructions or help.  There is also a tremendous amount of helpful information on the Internet on this topic. If the steps here do not work for you, Google for it. You'll almost certainly be able to find the procedure for the particular distribution of Linux you are running.

NOTE: Since part of the process described below is restarting the networking, you should always execute these commands from a local login to the machine, and not a remote sessions such as via ssh, vnc, etc.

How to add a static IP Address to a Linux computer

1) Setting your system's hostname


You should first set your system's hostname to the Fully Qualified Domain Name assigned to it. Assuming the assigned hostname for your machine is "pluto.cns.utexas.edu", you would use the following commands to set the hostname:

Operating system Commands

Red Hat/CentOS/SL 7+, Ubuntu 14.04+,
Fedora 18+, Arch

hostnamectl set-hostname "pluto.cns.utexas.edu"
Older Ubuntu, Debian, Arch
echo "pluto.cns.utexas.edu" > /etc/hostname
hostname -F /etc/hostname
Older Red Hat/CentOS/SL, Fedora
    echo "HOSTNAME=pluto.cns.utexas.edu" >> /etc/sysconfig/network
hostname "pluto.cns.utexas.edu"

2) Edit your /etc/hosts file

Next, you should edit your /etc/hosts file to add a line containing your assigned IP address and FQDN.  Assuming again your hostname is "pluto.cns.utexas.edu" and your IP address is 128.83.155.1, you can do this using the following command (all versions):

echo "128.83.155.1 pluto.cns.utexas.edu" >> /etc/hosts

 

3) Setting the actual IP address

The first step is to figure out the name of your connected network interface. Run the command:

    ip link

from the command prompt. It will output something like:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default   
   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: p4p1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN mode DEFAULT group default qlen 1000   
   link/ether 00:0a:f7:69:7d:5d brd ff:ff:ff:ff:ff:ff
3: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 
   link/ether 98:90:96:bd:1c:fc brd ff:ff:ff:ff:ff:ff
 
In this example, we have two wired network interfaces, p4p1 and em1.  em1 is connect to the network, as indicated by the state field in the output. Note also the MAC address of the interfaces.

Now you need to set the IP address and additional information needed.  The file and contents vary somewhat by Linux distribution.  We assume the connected network interface name is em1 here – use whatever you determined to be your network name in the step above if it is different.

 

Debian and Ubuntu

Edit the file /etc/network/interfaces and make the changes needed to specify your assigned IP address, Gateway, and Subnet Mask.  The file should have comments in it to help you identify what needs to be done.  Assuming again we are assigned 128.83.155.1 and pluto.cns.utexas.edu, and the network is a /24 network,  then the file would read:

# lo is the loopback interface, em1 is the active ethernet interface
auto lo em1 
iface lo inet loopback 
# Configuration for em1 
# This line ensures that the interface will be brought up during boot
auto em1 
allow-hotplug em1 
# The address, gateway and netmask are necessary. 
iface em1 inet static 
    address 128.83.155.1
    gateway 128.83.155.250 
    netmask 255.255.255.0
    dns-search utexas.edu
    dns-namesevers 128.83.185.40, 128.83.185.41


You would then restart the network interface using the commands:

    ifdown -a;  ifup -a

 

Red Hat 7, CentOS 7,  Scientific Linux 7, Fedora 22+:

These instructions are only for the newest versions of these operating system.  See below for older versions.

On Red Hat based systems, each interface has its own configuration file /etc/sysconfig/networking-scripts/ifcfg-INTERFACE , where INTERFACE is the name of the interface. To configure em1, for example, you would edit the file /etc/sysconfig/networking-scripts/ifcfg-em1, and set it to something like the following:

DEVICE=em1 
BOOTPROTO=static
HWADDR=98:90:96:bd:1c:fc
IPADDR=128.83.155.1
NETMASK=255.255.255.0
GATEWAY=128.83.155.250
DEFROUTE=yes
NM_CONTROLLED=no 
ONBOOT=yes 
IPV6INIT=no
DOMAIN="ph.utexas.edu"
DNS1=128.83.185.40
DNS2=128.83.185.41

Then you would restart the network interface using the commands:

    nmcli con reload
service network restart

 

Arch Linux

On Arch Linux, the configuration file for network interface is /etc/netctl/INTERFACE , where INTERFACE is the name of the interface. To configure em1, for example, edit /etc/netctl/em1, and make it look like this:

Description="A basic static ethernet connection"
Interface=em1
Connection=ethernet
IP=static
Address=('128.83.155.1/24')
Gateway=('128.83.155.250')
DNS=('128.83.185.40', '128.83.185.41')

Then start the network interface and enable the interface to start automatically at boot with the commands:

    netctl restart em1 
    netctl enable em1

 

Red Hat, CentOS, Scientific Linux, Fedora

If you are not running one of the newer versions shown above, then you would edit the file /etc/sysconfig/networking-scripts/ifcfg-INTERFACE as follows:

# Configuration for em1 
DEVICE=em1
BOOTPROTO=none 
HWADDR=98:90:96:bd:1c:fc 
ONBOOT=yes 
DEFROUTE=yes
NM_CONTROLLED=no 
IPADDR=128.83.155.1 
NETMASK=255.255.0.0 
GATEWAY=128.83.155.250
IPV6INIT=no
DOMAIN="ph.utexas.edu"
DNS1=128.83.185.40
DNS2=128.83.185.41

Then restart the network interface using the command:

    service network restart

 

4) Configure your DNS servers if necessary

 

On recent distributions, your DNS resolver are automatically configured. On older systems, you may need to configure your DNS servers manually. You can verify if your resolver is working by looking up an external host, for example
    host -a google.com 
If this command locates the IPs for google, then your DNS is working. Otherwise, you probably need to configure your DNS server manually.  On Debian-based systems, the DNS configuration file is /etc/resolvconf/resolv.conf.d/base, on other systems, it is /etc/resolv.conf. This configuration file should contain the following two lines.

 

nameserver 128.83.185.40
nameserver 128.83.185.41

Related articles

How to Set a Static IP on a Windows Machine
How to Set a Static IP on a Mac OS X Machine

Written by CNS OIT staff
Questions or comments? The best and easiest way to contact us is via the CNS Help Desk form.

See also: Networking