Gestion du réseau Linux avec NetworkManager
NetworkManager est le démon (par défaut sous Centos/RHEL 7) qui gère les connexions réseau. Il n’empêche pas l’usage des fichiers de configuration des interfaces ifcfg-*.
En Debian/Ubuntu, il sera peut-être nécessaire de l’installer. Aussi, il sera nécessaire de supprimer les entrées des interfaces à gérer par NetworkManager dans le fichier /etc/network/interfaces (man 5 interfaces).
sudo apt-get install network-manager
sudo systemctl stop networking
sudo systemctl disable networking
sudo systemctl enable NetworkManager
sudo systemctl start NetworkManager
On peut vérifier le fonctionnement du gestionnaire :
sudo systemctl status NetworkManager
● NetworkManager.service - Network Manager
Loaded: loaded (/lib/systemd/system/NetworkManager.service; enabled)
Active: active (running) since Wed 2017-02-01 18:09:16 CET; 32s ago
Main PID: 13994 (NetworkManager)
CGroup: /system.slice/NetworkManager.service
└─13994 /usr/sbin/NetworkManager --no-daemon
Feb 01 18:09:16 debian8 NetworkManager[13994]: <info> (lo): carrier is ON
Feb 01 18:09:16 debian8 NetworkManager[13994]: <info> (lo): new Generic devi...)
Feb 01 18:09:16 debian8 NetworkManager[13994]: <info> (lo): exported as /org...0
Feb 01 18:09:16 debian8 NetworkManager[13994]: <info> (eth0): link connected
Feb 01 18:09:16 debian8 NetworkManager[13994]: <info> (eth0): carrier is ON
Feb 01 18:09:16 debian8 NetworkManager[13994]: <info> (eth0): new Ethernet d...)
Feb 01 18:09:16 debian8 NetworkManager[13994]: <info> (eth0): exported as /o...1
Feb 01 18:09:16 debian8 NetworkManager[13994]: <info> startup complete
Feb 01 18:09:16 debian8 NetworkManager[13994]: <info> NetworkManager state i...L
Feb 01 18:09:16 debian8 NetworkManager[13994]: <info> ModemManager available...s
Hint: Some lines were ellipsized, use -l to show in full.
Outre le démon NetworkManager qui se gère directement avec systemctl, il est accompagné de plusieurs outils de diagnostic et de configuration :
-
nm-connection-editoretgnome-control-center networksont les outils graphiques de configuration du réseau. -
nmtuiest l’outil graphique dans un terminal texte. -
nmcliest l’outil en ligne de commande.
Par exemple :
sudo nm-connection-editor
sudo gnome-control-center network
sudo nmtui
sudo nmcli connection show -a
sudo nmcli device status
1. Définition du hostname
Le nom d’hôte est défini dans le fichier /etc/hostname :
[root@c7li ~]# cat /etc/hostname
c7li
On parle alors de “static hostname” mais le noyau maintient un “transient hostname” qui est un nom dynamique (copie du “static”). L’utilitaire hostnamectlvise à maintenir ces deux “hostnames”.
1.1. Changement du nom d’hôte
On peut changer le hostname avec les utilitaires nmtui ou nmcli. Il sera alors nécessaire de redémarrer le service qui gère le hostname :
sudo systemctl restart systemd-hostnamed
On peut aussi utiliser l’utilitaire hostnamectl …
1.2. Utilitaire hostnamectl
Vérification :
sudo hostnamectl status
Static hostname: c7li
Icon name: computer-vm
Chassis: vm
Machine ID: 39b49416825c4df8a8b08b98b088a173
Boot ID: 7137a2fc7d084efcab3d82f37a6f5156
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-327.4.5.el7.x86_64
Architecture: x86-64
Changement des deux hostnames identiques :
sudo hostnamectl set-hostname c7cli-1
hostnamectl status
Static hostname: c7cli-1
Icon name: computer-vm
Chassis: vm
Machine ID: 39b49416825c4df8a8b08b98b088a173
Boot ID: 7137a2fc7d084efcab3d82f37a6f5156
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-327.4.5.el7.x86_64
Architecture: x86-64
Changement des deux hostnames différents :
sudo hostnamectl set-hostname c7cli --static
hostnamectl status
Static hostname: c7cli
Transient hostname: c7cli-1
Icon name: computer-vm
Chassis: vm
Machine ID: 39b49416825c4df8a8b08b98b088a173
Boot ID: 7137a2fc7d084efcab3d82f37a6f5156
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-327.4.5.el7.x86_64
Architecture: x86-64
# hostnamectl set-hostname c7cli --transient
# hostnamectl status
Static hostname: c7cli
Icon name: computer-vm
Chassis: vm
Machine ID: 39b49416825c4df8a8b08b98b088a173
Boot ID: 7137a2fc7d084efcab3d82f37a6f5156
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-327.4.5.el7.x86_64
Architecture: x86-64
Effacer un hostname :
sudo hostnamectl set-hostname ""
hostnamectl status
Static hostname: n/a
Transient hostname: localhost
Icon name: computer-vm
Chassis: vm
Machine ID: 39b49416825c4df8a8b08b98b088a173
Boot ID: 7137a2fc7d084efcab3d82f37a6f5156
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-327.4.5.el7.x86_64
Architecture: x86-64
Changer un hostname à distance via ssh. Par exemple :
hostnamectl set-hostname -H root@192.168.23.1
1.3. Hostname avec l’utilitaire nmcli
Pour définir le hostname avec nmcli :
sudo nmcli general hostname server01
Vérification :
nmcli general hostname
2. Ligne de commande nmcli
nmcli est l’outil en ligne de commande de NetworkManager (https://developer.gnome.org/NetworkManager/unstable/nmcli.html).
2.1. Objets nmcli
nmcli peut interroger 8 objets avec options, commandes et arguments :
nmcli [OPTIONS...] { help | general | networking | radio | connection | device | agent | monitor } [COMMAND] [ARGUMENTS...]
-
nmcli help: Affiche l’aide -
nmcli general: Affiche le statut de NetworkManager, permet de vérifier et configurer le nom d’hôte (hostname), définir des niveaux de journaux (logs). -
nmcli networking: permet d’activer, désactiver, vérifier le statut du réseau. -
nmcli radio: permet d’activer, désactiver, vérifier le statut des interfaces radio. -
nmcli connection: … -
nmcli device: permet de vérifier et gérer les interfaces elle-mêmes. -
nmcli agent: permet faire fonctionner un agent de mots de passe ou polkit (EAP). -
nmcli monitor: permet d’observer l’activité de NetworkManager notamment pour les changement de connectivité des interfaces réseau.
2.2. Objet “general”
Par exemple, l’objet general sans option et abrégé :
nmcli general
ÉTAT CONNECTIVITÉ WIFI-HW WIFI WWAN-HW WWAN
connecté plein activé activé activé activé
nmcli -p general
============================================================
État de NetworkManager
============================================================
ÉTAT CONNECTIVITÉ WIFI-HW WIFI WWAN-HW WWAN
------------------------------------------------------------
connecté plein activé activé activé activé
nmcli -p g
============================================================
État de NetworkManager
============================================================
ÉTAT CONNECTIVITÉ WIFI-HW WIFI WWAN-HW WWAN
------------------------------------------------------------
connecté plein activé activé activé activé
3. Configurer une connexion existante
Principalement, on s’intéressera aux objets “device” et “connection”.
3.1. Vérification d’une interface existante
Vérification d’une interface existante :
nmcli device
DEVICE TYPE STATE CONNECTION
virbr0 bridge connected virbr0
eno16777736 ethernet connected eno16777736
virbr0-nic tap connected virbr0-nic
lo loopback unmanaged --
nmcli connection
NAME UUID TYPE DEVICE
virbr0-nic e9c5d546-e63f-4b79-968f-ccf6b61bce87 generic virbr0-nic
virbr0 d7369201-526e-4f29-b0c5-624e11a6d7d9 bridge virbr0
eno16777736 d289b1e3-cbdb-4be3-9feb-a8bc82ee9db3 802-3-ethernet eno16777736
nmcli device show eno16777736
GENERAL.DEVICE: eno16777736
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:0C:29:D6:02:86
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: eno16777736
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/0
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 172.16.98.164/24
IP4.GATEWAY: 172.16.98.2
IP4.DNS[1]: 172.16.98.2
IP4.DOMAIN[1]: localdomain
IP6.ADDRESS[1]: fe80::20c:29ff:fed6:286/64
IP6.GATEWAY:
nmcli d show eno16777736
connection.id: eno16777736
connection.uuid: d289b1e3-cbdb-4be3-9feb-a8bc82ee9db3
connection.interface-name: eno16777736
connection.type: 802-3-ethernet
connection.autoconnect: yes
connection.autoconnect-priority: 0
connection.timestamp: 1460231515
connection.read-only: no
connection.permissions:
connection.zone: --
connection.master: --
connection.slave-type: --
connection.autoconnect-slaves: -1 (default)
connection.secondaries:
connection.gateway-ping-timeout: 0
connection.metered: unknown
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:
802-3-ethernet.wake-on-lan: 1 (default)
802-3-ethernet.wake-on-lan-password: --
ipv4.method: auto
ipv4.dns:
ipv4.dns-search:
ipv4.addresses:
ipv4.gateway: --
ipv4.routes:
ipv4.route-metric: -1
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.gateway: --
ipv6.routes:
ipv6.route-metric: -1
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-send-hostname: yes
ipv6.dhcp-hostname: --
GENERAL.NAME: eno16777736
GENERAL.UUID: d289b1e3-cbdb-4be3-9feb-a8bc82ee9db3
GENERAL.DEVICES: eno16777736
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]: 172.16.98.164/24
IP4.GATEWAY: 172.16.98.2
IP4.DNS[1]: 172.16.98.2
IP4.DOMAIN[1]: localdomain
DHCP4.OPTION[1]: requested_domain_search = 1
DHCP4.OPTION[2]: requested_nis_domain = 1
DHCP4.OPTION[3]: requested_time_offset = 1
DHCP4.OPTION[4]: requested_Broadcast_address = 1
DHCP4.OPTION[5]: requested_rfc3442_classless_static_routes = 1
DHCP4.OPTION[6]: requested_classless_static_routes = 1
DHCP4.OPTION[7]: requested_domain_name = 1
DHCP4.OPTION[8]: expiry = 1460233018
DHCP4.OPTION[9]: domain_name = localdomain
DHCP4.OPTION[10]: next_server = 172.16.98.254
DHCP4.OPTION[11]: Broadcast_address = 172.16.98.255
DHCP4.OPTION[12]: dhcp_message_type = 5
DHCP4.OPTION[13]: requested_subnet_mask = 1
DHCP4.OPTION[14]: dhcp_lease_time = 1800
DHCP4.OPTION[15]: routers = 172.16.98.2
DHCP4.OPTION[16]: ip_address = 172.16.98.164
DHCP4.OPTION[17]: requested_static_routes = 1
DHCP4.OPTION[18]: requested_interface_mtu = 1
DHCP4.OPTION[19]: requested_nis_servers = 1
DHCP4.OPTION[20]: requested_wpad = 1
DHCP4.OPTION[21]: requested_ntp_servers = 1
DHCP4.OPTION[22]: requested_domain_name_servers = 1
DHCP4.OPTION[23]: domain_name_servers = 172.16.98.2
DHCP4.OPTION[24]: requested_ms_classless_static_routes = 1
DHCP4.OPTION[25]: requested_routers = 1
DHCP4.OPTION[26]: subnet_mask = 255.255.255.0
DHCP4.OPTION[27]: network_number = 172.16.98.0
DHCP4.OPTION[28]: requested_host_name = 1
DHCP4.OPTION[29]: dhcp_server_identifier = 172.16.98.254
IP6.ADDRESS[1]: fe80::20c:29ff:fed6:286/64
IP6.GATEWAY:
3.2. Configuration d’une interface avec nmcli
Pour passer d’une configuration dhcp vers une configuration statique :
- Nom : ipa.example.com
- Adresse IPv4 : 172.16.98.200/24
- Passerelle IPv4 : 172.16.98.2
- DNS IPv4 : 8.8.8.8
sudo hostnamectl set-hostname ipa.example.com
sudo -i su -c 'echo 172.16.98.200 ipa.example.com >> /etc/hosts'
sudo nmcli c mod eno16777736 ipv4.method manual ipv4.addresses 172.16.98.200/24
sudo nmcli c mod eno16777736 ipv4.gateway 172.16.98.2
sudo nmcli c mod eno16777736 ipv4.dns 8.8.8.8
sudo nmcli c up eno16777736
ip a show eno16777736
Dans cet exemple, eno16777736 est le nom du profil de connexion qui correspond à cette interface. Si on veut modifier le nom de profil de connexion, on utilisera la commande nmcli c mod <nom du profil> connection.id <nouveau nom du profil>
4. Désactiver NetworkManager
Se passer de NetworkManager ?! Oui, pour revenir aux configurations par fichier …
4.1. Désactivation par interface
Sous RHEL, en ajoutant le paramètre NM_CONTROLLED="no" dans le fichier de configuration d’une interface /etc/sysconfig/network-scripts/ifcfg-<INT> ou <INT> est le nom de l’interface ens3, eth0, etc.
4.2. Désactivation gobale
Sous RHEL/Centos 8, on peut réinstaller les “network scripts” et désactiver les avertissements :
sudo yum -y install network-scripts
sudo touch /etc/sysconfig/disable-deprecation-warnings
Vérification du service NetworkManager :
sudo systemctl status NetworkManager
● NetworkManager.service - Network Manager
Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
Active: active (running) since mer. 2016-02-17 22:14:57 CET; 1 weeks 0 days ago
Main PID: 801 (NetworkManager)
CGroup: /system.slice/NetworkManager.service
├─ 801 /usr/sbin/NetworkManager --no-daemon
└─2227 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /v...
févr. 24 22:29:51 c7cli dhclient[2227]: DHCPACK from 192.168.122.1 (xid=0x5...)
févr. 24 22:29:51 c7cli NetworkManager[801]: <info> address 192.168.122.226
févr. 24 22:29:51 c7cli NetworkManager[801]: <info> plen 24 (255.255.255.0)
févr. 24 22:29:51 c7cli NetworkManager[801]: <info> gateway 192.168.122.1
févr. 24 22:29:51 c7cli NetworkManager[801]: <info> server identifier 19...1
févr. 24 22:29:51 c7cli NetworkManager[801]: <info> lease time 3600
févr. 24 22:29:51 c7cli NetworkManager[801]: <info> hostname 'c7li'
févr. 24 22:29:51 c7cli NetworkManager[801]: <info> nameserver '192.168....'
févr. 24 22:29:51 c7cli NetworkManager[801]: <info> (eth0): DHCPv4 state c...d
févr. 24 22:29:51 c7cli dhclient[2227]: bound to 192.168.122.226 -- renewal....
Hint: Some lines were ellipsized, use -l to show in full.
sudo systemctl stop NetworkManager
sudo systemctl disable NetworkManager
Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
Redémarrer et activer le service réseau et vérifier :
En Centos/RHEL 7/8 :
sudo systemctl enable network
sudo systemctl restart network
sudo systemctl status network
● network.service - LSB: Bring up/down networking
Loaded: loaded (/etc/rc.d/init.d/network)
Active: active (exited) since mer. 2016-02-24 22:51:59 CET; 6s ago
Docs: man:systemd-sysv-generator(8)
Process: 2249 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=0/SUCCESS)
févr. 24 22:51:59 c7cli systemd[1]: Starting LSB: Bring up/down networking...
Hint: Some lines were ellipsized, use -l to show in full.
En Debian/Ubuntu :
sudo systemctl start networking
sudo systemctl status networking
● networking.service - LSB: Raise network interfaces.
Loaded: loaded (/etc/init.d/networking)
Drop-In: /run/systemd/generator/networking.service.d
└─50-insserv.conf-$network.conf
/lib/systemd/system/networking.service.d
└─network-pre.conf
Active: active (running) since Tue 2017-01-31 22:45:08 CET; 19h ago
CGroup: /system.slice/networking.service
└─390 dhclient -v -pf /run/dhclient.eth0.pid -lf /var/lib/dhcp/dhc...
Feb 01 17:23:32 debian8 dhclient[390]: DHCPREQUEST on eth0 to 192.168.122.1...67
Feb 01 17:23:32 debian8 dhclient[390]: DHCPACK from 192.168.122.1
Feb 01 17:23:32 debian8 dhclient[390]: bound to 192.168.122.54 -- renewal i...s.
Feb 01 17:46:46 debian8 dhclient[390]: DHCPREQUEST on eth0 to 192.168.122.1...67
Feb 01 17:46:46 debian8 dhclient[390]: DHCPACK from 192.168.122.1
Feb 01 17:46:46 debian8 dhclient[390]: bound to 192.168.122.54 -- renewal i...s.
Feb 01 18:11:24 debian8 dhclient[390]: DHCPREQUEST on eth0 to 192.168.122.1...67
Feb 01 18:11:24 debian8 dhclient[390]: DHCPACK from 192.168.122.1
Feb 01 18:11:24 debian8 dhclient[390]: bound to 192.168.122.54 -- renewal i...s.
Feb 01 18:20:00 zozo systemd[1]: Started LSB: Raise network interfaces..
Hint: Some lines were ellipsized, use -l to show in full.