Configure IPv4 addresses on centos 7, OL7, FEDORA 23, RHEL7


Source:  http://www.certdepot.net/rhel7-configure-ipv4-addresses/

Although it is still possible to define the network configuration through the files in the /etc/sysconfig/network-scripts directory, it’s not the preferred way any more (don’t forget to execute # nmcli con reload if you manually modify the files!).
With RHEL 7, all the network configuration is now mainly done through NetworkManager.
You can use:
  • the nmtui command and a Text User Interface,
  • the nmcli command at the Command Line Interface,
  • or the graphical interface.
For the rest of this tutorial, it is the nmcli option that has been chosen because it’s the quickest method and arguably the least prone to errors.
Changes made through the nmcli command are permanent.
Caution: To practice this tutorial in the best conditions, connect to the machine through its console (you could otherwise loose your connection!).

 

Network Configuration

To display the network configuration, type:

# nmcli con show
NAME           UUID                                  TYPE            DEVICE
ethernet-eth0  8d83684f-cd22-42cc-9fff-7704945a5c36  802-3-ethernet  eth0
 
Note: con is a shortcut for connection (you can even type only c).
Alternatively, you can type:

# nmcli dev status 
 
 DEVICE  TYPE      STATE      CONNECTION
eth0    ethernet  connected  ethernet-eth0
lo      loopback  unmanaged  --
 
To remove a connection (here ethernet-eth0), type:

# nmcli con del ethernet-eth0
Note1: If a space appears in the interface name (like System eth0), put everything between quotes: nmcli con del “System eth0”.
Note2: del is a shortcut for delete.
or

# nmcli con del 8d83684f-cd22-42cc-9fff-7704945a5c36

Connection Management

To create a connection with the name ethernet-eth0, the IPv4 address 192.168.1.10/24 and the default gateway 192.168.1.1, type:

# nmcli con add con-name net-eth0 ifname eth0 type ethernet ip4 192.168.1.10/24 gw4 192.168.1.1
Connection 'net-eth0' (441085a4-4155-417b-ad8f-78a888d89988) successfully added.
 
Note1: If you don’t specify con-name net-eth0, the connection is called ethernet-eth0.
Note2: If you don’t specify the ip4 192.168.1.10/24 gw4 192.168.1.1 part, you end up with a connection automatically configured through DHCP.

Note3: nmcli con up net-eth0 is not necessary when initially configuring a connection.
Note4: ip4 and gw4 are used for respectively the ip address and the default gateway. Below, you will see that the syntax when modifying a connection is different: it’s then using ipv4.addresses and a space between the ip address and the default gateway.
To check the configuration, type:

# ip a
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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::0000:00:0000:0000/64 scope link
       valid_lft forever preferred_lft forever
# ip r
default via 192.168.1.1 dev eth0  proto static  metric 1024
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.10
Note1: ip a is a shortcut for ip address show, ip r a shortcut for ip route show.

Note2: Don’t use the ifconfig command any more. This command is deprecated and no longer displays the correct network configuration (secondary ip addresses, etc).

To get all the information about a connection (here net-eth0), type:
# nmcli con show net-eth0
connection.id:                          net-eth0
connection.uuid:                        441085a4-4155-417b-ad8f-78a888d89988
connection.interface-name:              eth0
connection.type:                        802-3-ethernet
connection.autoconnect:                 yes
connection.timestamp:                   1427832564
connection.read-only:                   no
connection.permissions:                 
connection.zone:                        --
connection.master:                      --
connection.slave-type:                  --
connection.secondaries:                 
connection.gateway-ping-timeout:        0
802-3-ethernet.port:                    --
802-3-ethernet.speed:                   0
802-3-ethernet.duplex:                  --
802-3-ethernet.auto-negotiate:          yes
802-3-ethernet.mac-address:             --
802-3-ethernet.cloned-mac-address:      --
802-3-ethernet.mac-address-blacklist:   
802-3-ethernet.mtu:                     auto
802-3-ethernet.s390-subchannels:        
802-3-ethernet.s390-nettype:            --
802-3-ethernet.s390-options:            
ipv4.method:                            manual
ipv4.dns:                               
ipv4.dns-search:                        
ipv4.addresses:                         { ip = 192.168.1.10/24, gw = 192.168.1.1 }
ipv4.routes:                            
ipv4.ignore-auto-routes:                no
ipv4.ignore-auto-dns:                   no
ipv4.dhcp-client-id:                    --
ipv4.dhcp-send-hostname:                yes
ipv4.dhcp-hostname:                     --
ipv4.never-default:                     no
ipv4.may-fail:                          yes
ipv6.method:                            auto
ipv6.dns:                               
ipv6.dns-search:                        
ipv6.addresses:                         
ipv6.routes:                            
ipv6.ignore-auto-routes:                no
ipv6.ignore-auto-dns:                   no
ipv6.never-default:                     no
ipv6.may-fail:                          yes
ipv6.ip6-privacy:                       -1 (unknown)
ipv6.dhcp-hostname:                     --
GENERAL.NAME:                           net-eth0
GENERAL.UUID:                           441085a4-4155-417b-ad8f-78a888d89988
GENERAL.DEVICES:                        eth0
GENERAL.STATE:                          activated
GENERAL.DEFAULT:                        yes
GENERAL.DEFAULT6:                       no
GENERAL.VPN:                            no
GENERAL.ZONE:                           --
GENERAL.DBUS-PATH:                      /org/freedesktop/NetworkManager/ActiveConnection/0
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/Settings/0
GENERAL.SPEC-OBJECT:                    --
GENERAL.MASTER-PATH:                    --
IP4.ADDRESS[1]:                         ip = 192.168.1.10/24, gw = 192.168.1.1
IP6.ADDRESS[1]:                         ip = fe80::0000:00:0000:0000/64, gw = ::
Alternatively, you can type:
# nmcli dev show eth0
GENERAL.DEVICE:                         eth0
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         00:00:00:00:00:00
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     net-eth0
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/0
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         192.168.1.10/24
IP4.GATEWAY:                            192.168.4.10
IP4.DNS[1]:                             192.168.4.1
IP6.ADDRESS[1]:                         fe80::0000:00:0000:0000/64
IP6.GATEWAY:
To stop a network connection from working (here net-eth0), type:

# nmcli con down net-eth0
# nmcli con show
NAME      UUID                                  TYPE            DEVICE 
net-eth0  441085a4-4155-417b-ad8f-78a888d89988  802-3-ethernet  --     
Note1: The shows that the connection isn’t active any more (add the –active option to only display active connections).
Note2: You can specify the UUID (here 441085a4-4155-417b-ad8f-78a888d89988) instead of the network connection name.
Note3: After reboot, the connection still restarts automatically, the property connection.autoconnect being set to yes, equivalent to ONBOOT=yes.
To start a network connection (here net-eth0), type:

# nmcli con up net-eth0 
 
 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1)
Note: As before, you can specify the UUID (here 441085a4-4155-417b-ad8f-78a888d89988) instead of the network connection name.
To prevent the connection (here net-eth0) to restart after reboot, type:

# nmcli con mod net-eth0 connection.autoconnect no
 
Note: mod is a shortcut for modify.
To change the ip address and default gateway of the net-eth0 connection to respectively 192.168.2.10/24 and 192.168.2.1, type:

# nmcli con mod net-eth0 ipv4.addresses 192.168.2.10/24
# nmcli con mod net-eth0 ipv4.gateway 192.168.2.1
# nmcli con mod net-eth0 ipv4.method manual
# nmcli con up net-eth0 
 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
Caution: The command nmcli con mod net-eth0 ipv4.addresses “192.168.2.10/24 192.168.2.1” with a space between the ip address and the default gateway, all between quotes, was working in RHEL 7.0/CentOS 7.0 but doesn’t in RHEL 7.1/CentOS 7.1 and later due to NetworkManager changes (v0.9.9.1 -> v1.0.0).
Note1: You can use the syntax +ipv4.addresses or -ipv4.addresses to respectively add other ip addresses or remove some previously set (the initial one included).
Note2: The syntax is different from the one you used to initially set up the connection with ip4 and gw4.
To assign the net-eth0 connection to the work zone, type:

# nmcli con mod net-eth0 connection.zone work
# nmcli con up net-eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)

Hostname Configuration

In RHEL 7, there are three kinds of hostnames: static, pretty, and transient.
“The static host name is the traditional hostname, which can be chosen by the user, and is stored in the /etc/hostname file. The transient hostname is a dynamic host name maintained by the kernel. It is initialized to the static host name by default, whose value defaults to localhost. It can be changed by DHCP or mDNS at runtime. The pretty hostname is a free-form UTF8 host name for presentation to the user.” Source: RHEL 7 Networking Guide.
To get the server hostnames, type:

# hostnamectl
   Static hostname: centos7.example.com
         Icon name: computer
           Chassis: n/a
        Machine ID: 8f56e45764474b668b0db97b4127a01b
           Boot ID: 2ae7e6c78331414b82aa89a0ffcfa9fa
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-123.el7.x86_64
      Architecture: x86_64
 
Alternatively, you can use the hostname command to only get the host name (this reads the /etc/hostname file):

# hostname
centos7.example.com
 
Note: You can even get the same result with the command nmcli gen host.
To permanently assign the rhel7 hostname  to the server, type:
 
# hostnamectl set-hostname rhel7
 
 
Note1: With this syntax all three hostnames (static, pretty, and transient) take the rhel7 value at the same time. However, it is possible to set the three hostnames separately by using the –pretty, –static, and –transient options.
Note2: The nmcli gen host rhel7 command will give you the same result.

Hostname Resolution

Hostname resolution relies on the /etc/nsswitch.conf file where you can find the following line by default:

hosts:      files dns
 
This means that hostname resolution is at first done through files (static resolution) then dns (dynamic resolution).
The static hostname resolution comes through the /etc/hosts file:

192.168.1.10 centos7.example.com centos7
 
Note: Always write the IP address, the Full Qualified Domain Name and optionally some aliases in this order, otherwise some services like Kerberos will not work!
The dynamic hostname resolution is based on the /etc/resolv.conf file:

# Generated by NetworkManager
search example.com
nameserver 192.168.1.1
 
Note: You can have up to 3 nameservers configured. As nameservers are called in the mentioned order (the second is called if the first doesn’t reply and so on), always put the main nameserver first in the list.
To add a DNS server (here 8.8.8.8) to the configuration of the connection (here net-eth0), type:

# nmcli con mod net-eth0 +ipv4.dns 8.8.8.8
# nmcli con up net-eth0 
 
 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4) 
 
# more /etc/resolv.conf
# Generated by NetworkManager
search example.com
nameserver 192.168.1.1
nameserver 8.8.8.8

Note1: Use +ipv4.dns to add a new DNS server, -ipv4.dns to remove a DNS server and ipv4.dns to replace the current DNS server.

Note2: The change only occurs after the connection is restarted.

Note3: Use the ipv4.dns-search option to change the domain name if necessary. Be careful to set the correct full qualified domain name before with the hostnamectl set-hostname command.

You can’t remove a DNS server provided through DHCP with the previous command (with the -ipv4.dns option for example), you will get this error message: “Error: failed to remove a value from ipv4.dns: the property doesn’t contain DNS server ‘192.168.1.1’.”.

If you want to set your own DNS configuration in this context, type:

# nmcli con mod net-eth0 ipv4.ignore-auto-dns yes
 
Note: You get the same result by specifying PEERDNS=no in the network configuration files

Comentarios