Configurer une adresse ip en ligne de commande sous Linux

Il est facile de changer votre adresse IP en utilisant une interface graphique, mais saviez-vous que Linux vous permet également de changer l’adresse IP de votre carte réseau en utilisant une simple commande à partir de la ligne de commande?
 
 
Voici comment accéder au réseau en quelques secondes grâce à de quelques commandes seulement.
15 Commandes avec ifconfig pour configurer l'interface réseau sous Linux

Utiliser ifconfig
1. Définissez votre adresse IP
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[root@waytolearnx ~]# ifconfig eth0 192.168.1.2 netmask 255.255.255.0 up
[root@waytolearnx ~]# ifconfig eth0 192.168.1.2 netmask 255.255.255.0 up
[root@waytolearnx ~]# ifconfig eth0 192.168.1.2 netmask 255.255.255.0 up
2. Définissez votre passerelle par défaut
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[root@waytolearnx ~]# route add default gw 192.168.1.1
[root@waytolearnx ~]# route add default gw 192.168.1.1
[root@waytolearnx ~]# route add default gw 192.168.1.1
3. Définissez votre serveur DNS
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[root@waytolearnx ~]# echo "nameserver 1.1.1.1" > /etc/resolv.conf
[root@waytolearnx ~]# echo "nameserver 1.1.1.1" > /etc/resolv.conf
[root@waytolearnx ~]# echo "nameserver 1.1.1.1" > /etc/resolv.conf

En supposant que vous ayez des adresses valides, vous êtes tous prêts. Testez en envoyant une requête ping à google.com.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[root@waytolearnx ~]# ping google.com
[root@waytolearnx ~]# ping google.com
[root@waytolearnx ~]# ping google.com

Si vous utilisez un système Linux plus ancien, c’est tout ce que vous avez à faire. Si vous utilisez un système plus récent, lisez la suite.
9 Commandes avec ip pour configurer l’interface réseau sous Linux

Utiliser la commande ip et netplan

Ifconfig étant en cours de suppression, il est temps de s’habituer au nouveau système. Par défaut, Ubuntu 18.04 n’utilise plus « ifconfig », mais utilise les nouvelles commandes « ip » et « netplan ».

1. Afficher votre IP en utilisant la commande ip
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[root@waytolearnx ~]# ip addr show
[root@waytolearnx ~]# ip addr show
[root@waytolearnx ~]# ip addr show
2. Activer ou désactiver une interface réseau
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[root@waytolearnx ~]# ip link set eth0 up
[root@waytolearnx ~]# ip link set eth0 up
[root@waytolearnx ~]# ip link set eth0 up
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[root@waytolearnx ~]# ip link set eth0 down
[root@waytolearnx ~]# ip link set eth0 down
[root@waytolearnx ~]# ip link set eth0 down
3. Afficher la table de routage
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[root@waytolearnx ~]# ip route show
[root@waytolearnx ~]# ip route show
[root@waytolearnx ~]# ip route show

 
 

Modifier les informations de réseau

Pour Ubuntu, voici l’alternative de /etc/networking/* dans l’ancien système. L’ensemble du système utilise désormais les fichiers de configuration YAML sous /etc/netplan, puis la commande netplan applique ces configurations au système.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[root@waytolearnx ~]# vi /etc/netplan/*.yaml
[root@waytolearnx ~]# vi /etc/netplan/*.yaml
[root@waytolearnx ~]# vi /etc/netplan/*.yaml
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
network:
version: 2
ethernets:
eth0:
addresses: [192.168.1.2/24]
gateway4: 192.168.1.1
dhcp4: true
optional: true
nameservers:
addresses: [8.8.8.8,8.8.4.4]
network: version: 2 ethernets: eth0: addresses: [192.168.1.2/24] gateway4: 192.168.1.1 dhcp4: true optional: true nameservers: addresses: [8.8.8.8,8.8.4.4]
network:
  version: 2
  ethernets:
      eth0:
          addresses: [192.168.1.2/24]
          gateway4: 192.168.1.1
          dhcp4: true
          optional: true
          nameservers:
              addresses: [8.8.8.8,8.8.4.4]

Et ensuite pour appliquer la configuration, vous exécutez:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[root@waytolearnx ~]# netplan apply
[root@waytolearnx ~]# netplan apply
[root@waytolearnx ~]# netplan apply

Alors maintenant, que vous utilisiez un ancien ou un nouveau système, vous devriez maintenant avoir une adresse IP statique!

🚀 Boostez votre productivité avec les meilleurs outils IAEssayez-les

 

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *