[ Content contribution] Summary of commonly used Linux network commands
Tofloor
poster avatar
yanjuner
Super Moderator
2023-11-30 23:42
Author

Summary of Commonly Used Linux Network Commands

Explanation:

Ubuntu uses two methods for wired network configuration, so there are two configuration files (Deepin should be the same):

First, /etc/network/interfaces is mainly used for the server version of Ubuntu for ease of use;

Second, /etc/NetworkManager/NetworkManager.conf can also configure the network. This is to adapt to the changes in the network environment caused by mobile office, and the corresponding IP will also change;

The strategy is to choose one of the following:

When managed=false in /etc/NetworkManager/NetworkManager.conf, the configuration in the interfaces file takes effect;

When managed=true in /etc/NetworkManager/NetworkManager.conf, the configuration in the NetworkManager directory takes effect;

1. ifconfig

  1. ifconfig is one of the commonly used network configuration tools in Linux, used to configure and display the specific conditions of network interfaces.
  2. Precautions
    • The network card information configured with the ifconfig command is temporarily effective. After the network card is restarted or the machine is restarted, the configuration will no longer exist.
    • In some newer Linux distributions, the ifconfig command has been replaced by the ip command.
    • The IP address and subnet mask can be temporarily configured, but the gateway and DNS cannot be configured (configured with other commands).
  3. Usage help (common parameters)
    ifconfig [-a] [-v] [-s]  [[] 
    ] [netmask
    ] [up | down]
  4. Common situations
  • Start and stop the network card
    ifconfig eth0 up #Start the network card eth0
    ifconfig eth0 down #Close the network card eth0
  • Display eth0 network card information separately
    ifconfig eth0
  • Set the IP address of the network card
    ifconfig eth0 192.168.3.127 netmask 255.255.255.0
  • Add the IP address of the network card
    ifconfig eth0 add 192.168.200.200 netmask 255.255.255.0 #This will generate a virtual subnet card eth0:0
  • Delete the IP address of the network card, which will take effect temporarily.
    ifconfig eth0 del 192.168.200.200 netmask 255.255.255.0
  • Modify the MAC address and take temporary effect
    ifconfig eth0 hw ether 10:BA:CB:54:86:B3

2. IP

The ip command comes from the iproute2 software package. The iproute2 software package provides many commands (rpm -ql iproute |grep bin). This article only introduces a few of the commonly used ones:

  • ip address
  • ip route
  • ip link
  1. IP address
    address can be abbreviated as a or ad or add, etc.
    • ip address #View all IP addresses
    • ip address show ens33 #View the IP address on the ens33 network card
    • ip address add 192.168.100.10/24 device ens33 #Add a temporary IP address to the ens33 network card
    • ip address del 192.168.10.10/24 device ens33 #Delete a temporary IP address from the ens33 network card
    • Note:
      The IP added through ip a add will become invalid after restarting the host.
      There is no command to modify the IP address. If you want to modify it, you can delete the original IP first and then add a new IP.
  2. ip route
    • ip route #View routing
    • ip route add default via 172.17.0.1 #Default route (gateway)
  3. ip link
    • ip link #View all network devices
    • ip link add [link DEVICE] [name] NAME type TYPE #Create virtual network device

3. nmcli

nmcli is a command provided by the software NetworkManager. The following introduces the four commonly used commands of nmcli: network, general, connection, and device.

  1. nmcli networking

    • nmcli networking #networing can be abbreviated as n, showing whether Networmanager takes over the network settings
    • nmcli n on #Open network connection
    • nmcli n off #Close network connection
  2. nmcli general

    • nmcli general status or abbreviated as nmcli g #display network status
    • nmcli g hostname or nmcli gh #display hostname
    • nmcli g hostname newHostName or nmcli gh newHostName #Change the host name, which is stored in the /etc/hostname file. NetworkManager needs to be restarted to take effect.
  3. nmcli connection

    • nmcli connection show or nmcli c #Display all network connection information
    • nmcli cs --active or nmcli cs -a #Only display currently started connections
    • nmcli cs ens33 #Display detailed information about a specific connection
    • nmcli c up ens33 #Start the specified connection
    • nmcli c down ens33 #Close the specified connection
  4. Use nmcli connection to modify the connection:

    • nmcli c modify ens33 [+ | -] option option value or nmcli cm [+ | -] option option value
      Common modification examples:
    nmcli c m ens33 ipv4.address 192.168.80.10/24  # Modify IP address and subnet mask
    nmcli c m ens33 +ipv4.addresses 192.168.80.100/24
    nmcli c m ens33 ipv4.method manual             # Modify to static configuration, default is auto
    nmcli c m ens33 ipv4.gateway 192.168.80.2      # Modify default gateway
    nmcli c m ens33 ipv4.dns 192.168.80.2          # Modify DNS
    nmcli c m ens33 +ipv4.dns 223.5.5.5      # Add an Alibaba DNS
    nmcli c m ens33 connection.autoconnect yes     # Start the network card at boot
    Notice:
    * ipv4.address must be modified first, and then ipv4.method can be modified!
    * Use empty quotes "" to replace the value of the option to set the option back to its default value! Such as nmcli c m ens33 ipv4.method ""
    
    

    New connection configuration: (This is a network session function. For example, when a laptop is used on the company network, the IP address is manually assigned, and when used at home, the address is automatically assigned by DHCP. At this time, two sessions can be created and switched to different When using the environment, activate the corresponding network session to realize automatic switching of network information.)

    nmcli c add type Connection type option option value
    nmcli c add type ethernet con-name ens36 ifname ens36 #con-name Is the session name, also known as the network profile name

    • nmcli c delete ens33 #Delete the specified connection
    • nmcli c reload #Reload network configuration
  5. nmcli device

    • Display the status of all network interface devices
      nmcli device status or nmcli d
    • Show details of all devices
      nmcli d show or nmcli d sh
    • Show detailed information about a specific device
      nmcli d sh ens33
    • Connect the device. If ens33 is connected, the ens33 network card will be restarted.
      nmcli d connect ens33 or nmcli d c ens33
    • Disconnect device
      nmcli d disconnect ens33 or nmcli d d ens33
  6. Other related commands:

    View status:systemctl status NetworkManager 
    start up:systemctl start NetworkManager 
    Restart:systemctl restart NetworkManager 
    closure:systemctl stop NetworkManager 
    Check whether it starts at startup:systemctl is-enabled NetworkManager 
    boot:systemctl enable NetworkManager 
    Disable startup:systemctl disable NetworkManager
    
    

4. Wireless network

  1. iw
    • iw list #Get all devices
    • ifconfig wlan0 up #Activate network card
    • iw dev wlan0 scan #Scan
    • iw wlan0 connect foo #Connect to the unencrypted hotspot foo
    • wpa_passphrase test 12345678 >> /etc/wpa_supplicant.conf #Configure connection wifi, test is the wireless SSID, 12345678 is the password
    • wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf #Connect to wifi device
    • iw wlan0 link #View connection status
    • Get ip address for wlan0
      sudo dhclient wlan0
Reply Favorite View the author
All Replies

No replies yet