Setting up static IP address on CentOS/RHEL7

Configure Static IP On CentOS/RHEL7

In this short article we will see different methods to configure static ip address on CentOS/RHEL7 minimal Systems and how to set host name on CentOS/RHEL7 system.

Before to start we will check how do We list of network interfaces.

how do We list of network interfaces

#ip address or #ip a

[root@server1 ~]# ip address
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:d5:b2:a2 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::a00:27ff:fed5:b2a2/64 scope link 
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:20:02:39 brd ff:ff:ff:ff:ff:ff
    inet 194.168.1.5/24 brd 193.168.1.255 scope global dynamic enp0s8
       valid_lft 86354sec preferred_lft 86354sec
    inet6 fe80::a00:27ff:fe20:239/64 scope link 
       valid_lft forever preferred_lft forever

You can also check using below commands;

# nmcli -p dev or #nmcli con show

[root@server1 ~]# nmcli -p dev 
=================================================
                Status of devices
=================================================
DEVICE  TYPE      STATE      CONNECTION         
-------------------------------------------------
enp0s3  ethernet  connecting  enp0s3             
enp0s8  ethernet  connected  Wired connection 1 
lo      loopback  unmanaged  --                 
[root@server1 ~]#

#ifconfig

[root@server1 ~]# ifconfig

Note: If you got the error “ifconfig command not found”, install “net-tools” package.

Configure static ip address on CentOS/RHEL7

Method1: Modify ifcfg-enp0s3 config file using Editor.

In our demonstration, We modify the ifcfg-enp0s3 configuration file for the network adapter using vim editor.

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

TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
NAME=enp0s3
UUID=c1d25750-1959-4034-a6b1-637dec60a04d
DEVICE=enp0s3
ONBOOT=yes
IPADDR=192.168.1.10
PREFIX=24
GATEWAY=193.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

~
~
:wq

Restarting the networking service.

[root@server1 ~]# systemctl restart network

Method2: Modify ifcfg-enp0s3 config file using nmtui.

#nmtui

[root@server1 ~]#nmtui

It is based on TUI application for interacting with NetworkManager. It is used to show the connection editor that supports adding, modifying, viewing and deleting connections. In our case we change static IP using this tool for enp0s3, After execute the nmtui command it will shows below screen.

nmtui1_ittroubleshooter-in

Then select and Edit your Network Adapter.

nmtui2_ittroubleshooter-in

In our case we setup a static IP, as shown below;

nmtui3_ittroubleshooter-in

After setup a static IP press quit button.

Note: if you got “bash: nmtui: command not found” error, then install NetworkManager-tui package on system using yum command.

Method3: Modify ifcfg-enp0s3 config file using nmcli.

To add an Ethernet connection with static IPv4 configuration using nmcli, a command in the following format can be used:

[root@server1 ~]#nmcli con add type ethernet con-name itcare-lab ifname enp0s3 ip4 192.168.1.10/24 gw4 192.168.1.1

To set two IPv4 DNS server addresses:

[root@server1 ~]#nmcli con mod itcare-lab ipv4.dns "8.8.8.8 8.8.4.4"

How to Change or Set Hostname on CentOS/RHEL7

Method1: Using hostnamectl command

# hostnamectl set-hostname <Your_hostname>

[root@server1 ~]# hostname
server1.itcare.in
[root@server1 ~]# hostnamectl set-hostname Mail.itcare.in
[root@server1 ~]# hostname
mail.itcare.in
[root@server1 ~]#

To Verify new Settings, refer below command;

#hostnamectl status

[root@server1 ~]# hostnamectl status
   Static hostname: mail.itcare.in
   Pretty hostname: Mail.itcare.in
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 0764da3e816b4ea58f061f760383d27d
           Boot ID: 539b8b363fb84aadbf18561c40f4ac02
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.el7.x86_64
      Architecture: x86-64
[root@server1 ~]#

Note: When you relogin on system the entry of server1 from [root@server1 ~]# will be transform to “mail”.

To Delete a particular host name.
# hostnamectl set-hostname “”

[root@mail ~]# hostnamectl set-hostname ""

# hostnamectl set-hostname “” –static

[root@mail ~]# hostnamectl set-hostname "" --static

# hostnamectl set-hostname “” –pretty

[root@mail ~]# set-hostname "" --pretty

Method2: Using nmtui command

Using nmtui command you can also set host name of the system.

# nmtui

[root@server1 ~]# nmtui

After execution of nmtui command, It will prompt NetworkManager TUI. select “Set system hostname” option.

hostname_ittroubleshooter-in

Set Hostname and press on button.

hostname2_ittroubleshooter-in

host-name is successfully set.

hostname3_ittroubleshooter-in

Now restart the hostnamed service or re-login on the server.

#systemctl restart systemd-hostnamed

[root@mail ~]# systemctl restart systemd-hostnamed

Note: Now restart the hostnamed service or re-login on the server.

Method3: Using nmcli command

The syntax is; #nmcli general hostname <Your_hostname>

[root@server1 ~]# nmcli general hostname mail.itcare.in

restart the systemd-hostnamed service.

[root@server1~]# systemctl restart systemd-hostnamed

Note: Now restart the hostnamed service or re-login on the server.

Hope this post will help Linux/Unix beginners. Please share you feedback and Comments. Till then connected with us at ittroubleshooter.in .!!!

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz