Home Linux Networking How To Configure Static IP Address In Linux And Unix

How To Configure Static IP Address In Linux And Unix

3 Different Ways To Set Static IP Address In Linux And FreeBSD

By sk
Published: Last Updated on 35.6K views

Setting IP address after a fresh Linux installation is one of the mandatory skill that every Linux and Unix administrator should learn. We can easily assign IP address in Linux that has GUI mode. However, configuring IP networking from command line mode is entirely different! This step by step tutorial describes how to configure static IP address in Linux and Unix operating systems from command line mode.

The steps provided below are tested on AlmaLinux 8, CentOS 8 server editions, Ubuntu 22.04, 18.04, and 16.04 server and desktop editions and FreeBSD 13, FreeBSD 12 server editions. However, it should work on most RPM-based and DEB-based Linux systems and BSD flavors.

1. Configure Static IP Address In Linux

Setting up IP address in RPM-based and DEB-based systems is little bit different. First, we will see how to configure IP address on RPM-based systems.

1.1. Assign Static IP address In Fedora, RHEL, CentOS, AlmaLinux, Rocky Linux

Assigning IP address in Fedora and RHEL-based systems can be done with different ways. We can set IP address,

  • by manually editing the network configuration file,
  • using Nmcli command line tool,
  • and using Nmtui text-based user interface tool.

1.1.1. Set Static IP Address By Editing Network Configuration File

In Fedora, RHEL and its clones like CentOS, AlmaLinux and Rocky Linux, the network interface card (shortly NIC) configuration are stored under /etc/sysconfig/network-scripts/ directory.

Note: Here, I run all commands as root user. If you logged-in as normal user, just prepend each command with 'sudo'.

First, let us find the name of the network card.

To do so, run:

# ip link show

Sample output:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:80:63:19 brd ff:ff:ff:ff:ff:ff

Or, use this command to display detailed output:

# ip addr

Sample output:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 inet 127.0.0.1/8 scope host lo
 valid_lft forever preferred_lft forever
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 08:00:27:80:63:19 brd ff:ff:ff:ff:ff:ff
 inet 192.168.225.150/24 brd 192.168.1.255 scope global enp0s3
 valid_lft forever preferred_lft forever
 inet6 fe80::a00:27ff:fe80:6319/64 scope link 
 valid_lft forever preferred_lft forever

Usually, the wired network card name will start with letter "e", and wireless card name will start letter with "w".

As you see in the above output, my wired network card name is enp0s3. It might be different in your distribution, but it usually start with letter "e".

Let us now configure a static IP address to this NIC.

Open the network card config file in any editor:

# vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

Here, I use vi editor to edit the network config file. You can use any text/graphical editor of your choice, for example nano or gedit.

Set bootproto (boot protocol) to none and set the IP address, subnet mask, gateway, and DNS server as shown below.

TYPE="Ethernet"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
NAME="enp0s3"
UUID="e9f9caef-cb9e-4a19-aace-767c6ee6f849"
ONBOOT="yes"
HWADDR="08:00:27:80:63:19"
IPADDR0="192.168.225.150"
PREFIX0="24"
GATEWAY0="192.168.225.1"
DNS1="8.8.8.8"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
Configure static IP address on CentOS Linux
Configure static IP address on CentOS Linux

This is how a typical network card configuration file looks like in any RPM based systems. Did you notice the lines that I have marked in bold (and arrows in the image)? Those are the important lines.

Let me explain about those lines:

  • BOOTPROTO="none"- This line shows that the network card's IP address should be configured manually. If you set the value as "dhcp", then the network card will accept the IP address from any DHCP server in the network.
  • IPADDR0="192.168.225.150" - This line indicates the IP address of the network card. Here, the zero in the IPADDR0 parameter indicates that this card as configured with only one IP address. If you want to set more than one IP address (i.e virtual IP address), then add new lines - for example IPADDR1, IPADDR2 and set different IP addresses of your choice.
  • PREFIX0="24" - This line indicates the subnet mask, i.e 255.255.255.0. Here you can specify more than one subnet with lines PREFIX1, PREFIX 2 etc.
  • GATEWAY0="192.168.225.1" - This is the gateway address of the NIC.
  • DNS1="8.8.8.8" - The Name server address. You can also specify more than one DNS with lines DNS2, DNS3 etc.

The other lines are less important. If you're curious to know what are those, here you go.

  • DEFROUTE - Whether to use this connection as the default route.
  • HWADDR - Indicates the hardware address of the network device.
  • IPV4_FAILURE_FATAL - Whether to disable the device if IPv4 configuration fails. The default value is no.
  • IPV6INIT - Whether to enable IPv6 support for this connection.
  • ONBOOT - Whether to start this connection at system boot.
  • UUID - The UUID associated with this connection.
  • TYPE - Indicates the type os this connection i.e. Ethernet, WiFi etc.

For more details about each parameter, check the manual page of ip addr command.

Once you setup all details, save and close the file. Restart the network service for the changes to take effect.

# systemctl restart network

Or, simply reboot your system.

Now, verify the new static IP address using command:

# ip addr

Or, you can check a specific network card's address as shown below.

# ip a s enp0s3

Sample output:

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:80:63:19 brd ff:ff:ff:ff:ff:ff
inet 192.168.225.150/24 brd 192.168.225.255 scope global noprefixroute enp0s3
valid_lft forever preferred_lft forever
inet6 2405:204:714e:faa:a00:27ff:fe80:6319/64 scope global noprefixroute 
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe80:6319/64 scope link noprefixroute 
valid_lft forever preferred_lft forever

1.1.2. Set IP Address Using Nmtui

Alternatively, you can use the NetworkManager TUI (nmtui) utility to configure IP address.

If it is not installed already, you can install it using command:

# dnf install NetworkManager-tui

Now, start nmtui utility by entering the following command:

# nmtui

Choose "Edit a connection" option:

Edit Network Connection
Edit Network Connection

Choose the network interface card to configure from the left pane and select "Edit" option on the right and hit ENTER key:

Choose Edit Connection
Choose Edit Connection

Enter the IP address, netmask, gateway, and DNS details etc. Finally, Click OK to save the changes.

Set IP Address Using Nmtui
Set IP Address Using Nmtui

Restart Networkmanager service:

# systemctl restart NetworkManager

Or reboot your system to take effect the changes.

1.1.3. Set IP Address Using Nmcli

nmcli is a command line NetworkManager interface to create, view, modify, activate, deactivate and remove network connections.

To show all active and inactive network connections, run:

$ nmcli connection show 

Sample output:

NAME         UUID                                  TYPE      DEVICE  
System eth0  466779c6-2f49-11ed-a11e-a0e70bdf3dfb  ethernet  eth0    
docker0      97580a1a-1e98-4534-ae8f-52fbbccf6a82  bridge    docker0 

As you see in the above output, it shows two connection profiles namely "System eth0" and "docker0" and the devices they are attache to.

Let us see how to configure IP address for the eth0 connection.

To assign a static IP address to eth0 interface using nmcli, run:

$ sudo nmcli connection modify "System eth0" ip4 192.168.1.20/24 gw4 192.168.1.101 ipv4.dns 8.8.8.8

Here, we are setting IP address 192.168.1.20/24 to the connection profile "System eth0" with gateway 192.168.1.101 and DNS 8.8.8.8. Replace the the name of the connection profile with your own along with the IP address, gateway and DNS.

Update the changes using command:

$ sudo nmcli connection up "System eth0"

That's it. We have assigned a static IP address to eth0 card.

Let us verify it using command:

$ ip a

Sample output:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0@if21: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether d2:85:0c:c7:c1:c3 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.1.20/24 brd 192.168.1.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::d085:cff:fec7:c1c3/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:a9:0a:0a:9b brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever

You can also verify the IP address by displaying the contents of the ifcfg-eth0 config file.

$ cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
UUID=a5da8caa-2f52-11ed-a720-a0e70bdf3dfb
BOOTPROTO=none
IPADDR=192.168.1.20
NETMASK=255.255.255.0
GATEWAY=192.168.1.101
DNS1=8.8.8.8

1.2. Configure Static IP Address In Debian, Ubuntu

Configuring IP address in Debian and Ubuntu using nmcli and nmtui tools is exactly same as described above. If you're using desktop environment in Debian or Ubuntu, nmcli and nmtui comes pre-installed.

Setting up static IP address by editing network configuration files is bit different in DEB-based systems. The following steps shows you how to configure static IP address in Debian 11 bullseye.

1.2.1. Set Static IP Address In Debian

Let us first list the available network interfaces using command:

$ ip link show

Sample output:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether ca:da:d1:bc:ba:65 brd ff:ff:ff:ff:ff:ff
    altname enp0s18

As you see in the above output, my network interface card name is ens18.

We can set static IP address by editing the network interface config file. All network configuration files are stored under /etc/network/ directory in Debian-based systems.

Edit /etc/network/interfaces/ file using any text editor:

$ sudo vim /etc/network/interfaces

Or,

$ sudo nano /etc/network/interfaces

Add or modify the following lines to configure static IP address.

auto ens18
iface ens18 inet static
        address 192.168.1.150
        netmask 255.255.255.0
        gateway 192.168.1.101
        dns-nameservers 8.8.8.8    
Set Static IP Address In Debian
Set Static IP Address In Debian

Replace ens18 with your network interface name along with the IP address, gateway and dns. save the file and close it.

Restart NetworkManager service to update the changes.

$ sudo systemctl restart networkijng.service

That's it. You can now check the IP address using command:

$ ip a

Sample output:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether ca:da:d1:bc:ba:65 brd ff:ff:ff:ff:ff:ff
    altname enp0s18
    inet 192.168.1.150/24 brd 192.168.1.255 scope global ens18
       valid_lft forever preferred_lft forever
    inet6 fe80::c8da:d1ff:febc:ba65/64 scope link 
       valid_lft forever preferred_lft forever

1.2.2. Assign Static IP Address In Ubuntu

Like I already said, setting up IP address with nmcli or nmtui tools is exactly same as we explained in the RHEL-based systems section above.

If you want to set IP address by manually editing network configuration file, follow the steps provided below.

Edit /etc/network/interfaces/ file in any text editor:

$ sudo nano /etc/network/interfaces

Add or modify the following lines to configure static IP address.

auto enp0s3
iface enp0s3 inet static
 address 192.168.1.105
 netmask 255.255.255.0
 gateway 192.168.1.1
 dns-nameservers 192.168.1.1
Configure IP address in Ubuntu
Configure IP address in Ubuntu

Save and close the file.

Restart network service using command:

$ sudo systemctl restart ifup@enp0s3

Or,

$ sudo systemctl restart networking.service

Or, simply reboot the system.

Now, check the new static IP address using any one of the following commands:

$ ifconfig
$ ip addr
$ ip a s enp0s3

Sample output:

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
 link/ether 08:00:27:12:f8:c1 brd ff:ff:ff:ff:ff:ff
 inet 192.168.1.105/24 brd 192.168.1.255 scope global enp0s3
 valid_lft forever preferred_lft forever
 inet6 fe80::a00:27ff:fe12:f8c1/64 scope link 
 valid_lft forever preferred_lft forever
View IP Address
View IP Address

Heads Up: Starting from Ubuntu 17.10, we no longer use /etc/network/interfaces file to configure IP address. In recent Ubuntu versions, we use Netplan utility to configure IP address. To configure IP address on recent Ubuntu distributions, refer the following link.

We just learned how to configure static IP address in Linux from Command line. Let us now configure static IP address in Unix. For the purpose of this tutorial, I will be using FreeBSD 13.

2. Configure Static IP Address In FreeBSD

We use "ifconfig" command to find out the network card name in FreeBSD. Here, I logged-in as root user to perform the following commands.

# ifconfig

Sample output:

em0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
    options=81009b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,VLAN_HWFILTER>
    ether 08:00:27:7e:6d:d4
    inet6 fe80::a00:27ff:fe7e:6dd4%em0 prefixlen 64 scopeid 0x1 
    inet6 2405:204:714e:faa:a00:27ff:fe7e:6dd4 prefixlen 64 autoconf 
    inet 192.168.225.27 netmask 0xffffff00 broadcast 192.168.225.255 
    media: Ethernet autoselect (1000baseT <full-duplex>)
    status: active
    nd6 options=23<PERFORMNUD,ACCEPT_RTADV,AUTO_LINKLOCAL>
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
    options=680003<RXCSUM,TXCSUM,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6>
    inet6 ::1 prefixlen 128 
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2 
    inet 127.0.0.1 netmask 0xff000000 
    groups: lo 
    nd6 options=23<PERFORMNUD,ACCEPT_RTADV,AUTO_LINKLOCAL>
View IP Address In FreeBSD
View IP Address In FreeBSD

Here em0 is the network interface card name.

To configure static IP address, edit /etc/rc.conf file:

# vi /etc/rc.conf

Add/modify the lines as shown below.

hostname="freebsd.ostechnix.local"
ifconfig_em0="inet 192.168.255.110 netmask 255.255.255.0"
defaultrouter="192.168.255.1"
local_unbound_enable="YES"
sshd_enable="YES"
moused_enable="YES"
ntpd_enable="YES"
powerd_enable="YES"
# Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
dumpdev="AUTO"

Save and close the file.

Restart network service using the following command:

# /etc/rc.d/netif restart && /etc/rc.d/routing restart

Now, check if the IP address has been changed or not using command:

# ifconfig
View IP Address In FreeBSD Using ifconfig Command
View IP Address In FreeBSD Using ifconfig Command

To configure network card to obtain IP address from a DHCP server, add or modify the following lines only:

hostname="freebsd.ostechnix.local"
ifconfig_em0="DHCP"

Or,

hostname="freebsd.ostechnix.local"
ifconfig_DEFAULT="DHCP inet6 accept_rtadv"

Save and close the file. Restart networking service or reboot your system to take effect the changes.

Conclusion

In this tutorial, we learned a few ways to configure static IP address in Linux and Unix operating systems. In the first method, we assigned the IP address by manually editing network configuration files. In the second and third methods method, we used nmcli and nmtui to set IP address from command line in Linux.

Featured image by mohamed Hassan from Pixabay.

You May Also Like

7 comments

Dan St.André September 1, 2016 - 9:44 pm

Thank you for this clearly written description of static IP configuration. I would be interested in a follow-up article that discusses static IP for a walk-about laptop. Specifically, I need STATIC_IP_A when I’m part of NETWORK_1, STATIC_IP_B when I’m part of NETWORK_2,…, STATIC_IP_Z when I’m part of NETWORK_N, and dynamic IP address the rest of the time.

Reply
SK September 2, 2016 - 11:21 am

I am afraid we couldn’t do that in Linux with single network interface card. However, It is possible to do it with manageable network switch. We could create different vlans and assign different set of IP addresses for each vlan.

Reply
Dan St.André September 2, 2016 - 5:22 pm

Is there some way to:
1. detect which network is available
2. run one or more scripts that are per-network specific
3. the scripts would used DHCP or static-IP as needed
4. might need to connect-inspect-bounce-configure

While I would like to detect wired & wireless and launch appropriate scripts, wireless is by far the most common and most useful.

I am trying, with poor success, to find the network connection hooks that I could use to launch my own scripts.

~~~0;-Dan

ASIDE — I use Linux Mint 17.3 from the Ubuntu family of distributions.

Reply
SK September 4, 2016 - 12:45 pm

As I understood..
Create a script..First detect which network available on system. And show available network status like up or down and also show Static ip or DHCP IP.
Something like-
Available—Status—DH/ ST——–IP
eth0. Up. dhcp. *******
wlan0. Down. —- ——-
Second option detect network configuration – Enter Available network name- when we enter name , then again show option set ip address dhcp or static-
-> Input dhcp (auto set dhcp ip)
-> Input static (Then auto set static ip) or
Enter -> Ipaddress-
-> netmask-
-> gateway-
-> dns1
-> dns2

I think when we are connected both network (wlan & lan) ..the first priority of wireless network if wireless network down then automatic up lan port…

Reply
Don Porter March 27, 2018 - 5:57 pm

Static IP setting for Linux Mint Sylvia worked perfectly…

… AFTER I substituted enp3s0 for enp0s3. Who knows why mine was different?

THANKS for accurate instructions which actually WORK! Rare in my world of Googling.

Reply
Ahmed September 5, 2019 - 2:59 am

What ip adress re we supposed to enter in when configuring the static ip ?

Reply
sk September 5, 2019 - 12:17 pm

Any IP address of your choice.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More