[Share Experiences] Summary of Common Linux Network Commands
Tofloor
poster avatar
SuperDavid
Moderator
2024-08-28 17:00
Author

[Experience Sharing] Summary of Common Linux Network Commands

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

  • /etc/network/interfaces: This configuration file is mainly used for server versions of Ubuntu systems.
  • /etc/NetworkManager/NetworkManager.conf: This can also be used to configure the network, which is to adapt to changes in network environments caused by mobile office settings, with the corresponding IP also changing.

The strategy is to choose one of the two:

  • When managed=false in /etc/NetworkManager/NetworkManager.conf, the configuration in the interfaces file is used.
  • When managed=true in /etc/NetworkManager/NetworkManager.conf, the configuration in the NetworkManager directory is used.
  1. ifconfig

ifconfig is one of the commonly used network configuration tools in Linux, used to configure and display details of network interfaces.

Notes:

  • The network card information configured with the ifconfig command is temporarily effective. The configuration will be lost after restarting the network card or the machine.
  • In some newer Linux distributions, the ifconfig command has been replaced by the ip command.
  • It can temporarily configure IP addresses and subnet masks but cannot configure gateways and DNS (use other commands for these).

Usage Help (Common Parameters)

ifconfig [-a] [-v] [-s] [[]

]

[netmask

]

[up | down]

Common Scenarios:

  • Start and stop network cards:

ifconfig eth0 up # Start network card eth0

ifconfig eth0 down # Stop network card eth0

  • Display information of the eth0 network card:

ifconfig eth0

  • Set the IP address of the network card:

ifconfig eth0 192.168.3.127 netmask 255.255.255.0

  • Add an IP address to the network card:

ifconfig eth0 add 192.168.200.200 netmask 255.255.255.0 # This will generate a virtual subnetwork card eth0:0

  • Delete an IP address from the network card (temporary):

ifconfig eth0 del 192.168.200.200 netmask 255.255.255.0

  • Change the MAC address (temporary):

ifconfig eth0 hw ether 10:BA:CB:54:86:B3

2. ip

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

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 dev ens33 # Add a temporary IP address to the ens33 network card

ip address del 192.168.10.10/24 dev ens33 # Delete a temporary IP address from the ens33 network card

Note:

  • The IP added through ip a add will be invalid after the host restarts.
  • There is no command to modify the IP address. To modify it, you can delete the original IP first and then add a new IP.

ip route

ip route # View routes

ip route add default via 172.17.0.1 # Default route (gateway)

ip link

ip link# View all network devices

ip link add [link DEVICE] [name] NAME type TYPE # Create virtual network devices

3. nmcli

nmcli is a command provided by the NetworkManager software. Below are four commonly used nmcli command categories: networking, general, connection, and device.

nmcli networking

nmcli networking # networking can be abbreviated as n, display whether NetworkManager is managing network settings

nmcli n on # Turn on network connection

nmcli n off # Turn off network connection

nmcli general

nmcli general status or nmcli g # Display network status

nmcli g hostname or nmcli g h # Display hostname

nmcli g hostname newHostName or nmcli g h newHostName # Change hostname, stored in the /etc/hostname file, requires restarting NetworkManager to take effect

nmcli connection

nmcli connection show or nmcli c # Display information of all network connections

nmcli c s --active or nmcli c s -a # Display only currently active connections

nmcli c s ens33 # Display detailed information of a specific connection

nmcli c up ens33 # Start a specified connection

nmcli c down ens33 # Stop a specified connection

Using nmcli connection to modify connections:

nmcli c modify ens33 [+ | -] options options_value or nmcli c m [+ | -] options options_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 # Change to static configuration, default is auto

nmcli c m ens33 ipv4.gateway 192.168.80.2 # Modify the 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 Ali's DNS

nmcli c m ens33 connection.autoconnect yes# Enable network card at startup

Notes:

  • Modify ipv4.address first before modifying ipv4.method!
  • Use empty quotes "" to reset the option to default! For example, nmcli c m ens33 ipv4.method ""

*Add new connection configuration: *(This is a network session function. For example, when a laptop uses a manually specified IP address in the company network, and uses DHCP to automatically assign addresses at home. At this time, you can create two sessions and switch to different usage environments, activate the corresponding network session to achieve automatic switching of network information.)

nmcli c add type connection_type options options_value

nmcli c add type ethernet con-name ens36 ifname ens36 # con-name is the session name, also known as the network configuration file namebash复制代码 nmcli c delete ens33 # Delete a specified connection

nmcli c reload # Reload network configuration

nmcli device

  • Display the status of all network interface devices:

nmcli device status or nmcli d

  • Display detailed information of all devices:

nmcli d show or nmcli d sh

  • Display detailed information of a specific device:

nmcli d sh ens33

  • Connect the device, if ens33 is in connection state, it will restart the ens33 network card:

nmcli d connect ens33 or nmcli d c ens33

  • Disconnect the device:

nmcli d disconnect ens33 or nmcli d d ens33

Other related commands:

# Check status:

systemctl status NetworkManager

# Start:

systemctl start NetworkManager

# Restart:

systemctl restart NetworkManager

# Stop:

systemctl stop NetworkManager

# Check if it starts at boot:

systemctl is-enabled NetworkManager

# Enable at boot:

systemctl enable NetworkManager

# Disable at boot:

systemctl disable NetworkManager

4. Wireless Network

iw

iw list # Get all devices

ifconfig wlan0 up # Activate network card

iw dev wlan0 scan # Scan

iw wlan0 connect foo # Connect to an unencrypted hotspot foo

wpa_passphrase test 12345678 >> /etc/wpa_supplicant.conf # Configure to connect to 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# Check connection status

Get IP address for wlan0:

sudo dhclient wlan0

Reply Favorite View the author
All Replies

No replies yet