SoFunction
Updated on 2025-03-03

Three ways to configure IP in Linux

Three configurations of Linux IP methods

Method 1: Use the nmcli command

illustrate:Network configuration configured using the nmcli command can take effect immediately and the configuration will not be lost after the system restarts.

Introduction to nmcli

nmcliis a command line tool for NetworkManager, which provides a way to use the command line to configure the network connections managed by NetworkManager.

The basic format of the nmcli command is:

nmcli [OPTIONS] OBJECT { COMMAND | help }

Among them, the OBJECT option can be general, network, radio, connection or device, etc. In daily use, the most commonly used are the -t, --terse (for scripts), -p, --pretty options (for users) and -h, --help options. Users can use "nmcli help" to obtain more parameters and usage information.

$ nmcli help

Commonly used commands are as follows:

  • Display NetworkManager status:
$ nmcli general status
  • Show all connections:
$ nmcli connection show
  • Only the currently active connection is displayed, as follows:
$ nmcli connection show --active
  • Displays the device and its status recognized by NetworkManager:
$ nmcli device status
  • Use the nmcli tool to start and stop the network interface, and execute the following command under root permissions:
# nmcli connection up id enp3s0 # nmcli device disconnect enp3s0

Equipment Management

Connect to the device

Using the following command, NetworkManager will connect to the corresponding network device, try to find the appropriate connection configuration, and activate the configuration.

# nmcli device connect "$IFNAME"

illustrate:If no corresponding configuration connection exists, NetworkManager will create and activate a new configuration file with default settings.

Disconnect the device

Using the following command, NetworkManager will disconnect the device and prevent the device from automatically activate.

# nmcli device disconnect "$IFNAME"

Set up a network connection

List the currently available network connections:nmcli con show

$ nmcli con show
NAME        UUID                                  TYPE      DEVICE
Wired connection 1  ffce71f8-6121-3024-84b5-192f50f90d25  ethernet  ens160

illustrate:The NAME field in the output result represents the connection ID (name).

Adding a network connection will generate the corresponding configuration file and associate it with the corresponding device. Check available equipment as follows:

nmcli dev status
$ nmcli dev status
DEVICE  TYPE      STATE   CONNECTION
ens160  ethernet  Connected  Wired connection 1
lo      loopback  Unmanaged  --

Configure static IP connections

Configure IP

To add a static IPv4 configuration network connection, use the following command:

nmcli connection add type ethernet con-name connection-name ifname interface-name ip4 address gw4 address

Example:

$ nmcli connection add type ethernet con-name eth1_con ifname eth1 ip4 192.168.10.25/24 gw4 192.168.10.1

illustrate:If you want to add IPv6 address and gateway information, use the ip6 and gw6 options.

Method 2: Use the ip command

illustrate:Network configuration configured using the ip command can take effect immediately, but the configuration will be lost after the system restarts.

Configure IP address

Use the ip command to configure the address for the interface. The command format is as follows, where interface-name is the network interface name.

ip addr [ add | del ] address dev interface-name

Configure static addresses

Under root permissions, configure a static IP address, and the usage example is as follows:

$ ip address add 192.168.0.10/24 dev enp3s0

Under root permissions, check the configured network port:

$ ip addr show dev enp3s0

Configure multiple addresses

The ip command supports assigning multiple addresses to the same interface. You can use the ip command to assign multiple addresses repeatedly under root permissions.

Examples of use are as follows:

$ ip address add 192.168.2.223/24 dev enp4s0
$ ip address add 192.168.4.223/24 dev enp4s0
$ ip addr

3: enp4s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:aa:da:e2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.203.12/16 brd 192.168.255.255 scope global dynamic noprefixroute enp4s0
       valid_lft 8389sec preferred_lft 8389sec
    inet 192.168.2.223/24 scope global enp4s0
       valid_lft forever preferred_lft forever
    inet 192.168.4.223/24 scope global enp4s0
       valid_lft forever preferred_lft forever
    inet6 fe80::1eef:5e24:4b67:f07f/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

Configure static routing

If you need static routing, you can use the ip route add command to add it in the routing table and use the ip route del command to delete it.

The most commonly used ip route command format is as follows:

$ ip route [ add | del | change | append | replace ] destination-address

View the routing table:

$ ip route

Add a static route to the host address, under root permissions, use the following command format:

$ ip route add 192.168.2.1 via 10.0.0.1 [dev interface-name]
# 192.168.2.1It's the destination hostip,10.0.0.1It's the default gateway,dev ethXXIt's the internet port that goes out

Static routes added to a network segment:

$ ip route add 192.168.2.0/24 via 10.0.0.1 [dev interface-name]
#Indicates arrival192.168.2.0-192.168.2.255Routing of network segments

Add the default gateway route:

$ ip route add default via  ethX

Method 3: Configure the network through ifcfg file

illustrate:Network configuration configured through ifcfg file will not take effect immediately and needs to be executed under root permissionsifup interface-nameThe command will only take effect after activating the network configuration (fill in the specific network interface name at the interface-name).

Configure a static network

Taking the enp4s0 network interface for static network settings as an example, by modifying the ifcfg file under root permissions,/etc/sysconfig/network-scripts/Generate a file named ifcfg-enp4s0 in the directory and modify the parameter configuration

Examples are as follows:

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=192.168.0.10
GATEWAY=192.168.0.1
PREFIX=24
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp4s0static
UUID=08c3a30e-c5e2-4d7b-831f-26c3cdc29293
DEVICE=enp4s0
ONBOOT=yes

Pay attention to setting up static IP when configuringBOOTPROTO=none

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.