Ubuntu18.04 dual network card (supports simultaneous access to internal and external networks) configuration

Temporary modification of the routing system fails to restart

View system network card information

ifconfig

View system routing information

ruote -n

Add a route

route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.0.254 dev em1

ps: In the above net, the ip address starting with 192.168 in the 192.168.0.0 wildcard intranet, netmask corresponds to net, and gw is the intranet gateway address corresponding to the network card em1, which realizes intranet access; example: route add -net 192.168. 64.0 netmask 255.255.255.0 gw 192.168.0.254 dev em1

Delete a route

route del -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.0.254 dev em1

ps: The parameters following route del are required to be filled in except-net, others are not required, as long as the only record in the route table can be locked

One-time modification takes effect permanently

Modify the network configuration file, the location of the configuration file: /etc/netplan/*.yaml Different server file name prefixes are different
Modify the configuration file

sudo vim /etc/netplan/01-network-manager-all.yaml

The content of the configuration file is:

# Let NetworkManager manage all devices on this system

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    eno1:         //内网口
      dhcp4: false
      addresses: [192.168.77.189/24]  //内网ip
      #gateway4:
      routes:
        - to: 192.168.0.0/16   //目的ip
          via: 192.168.77.254  //内网网关
          metric: 100    
      nameservers:
              addresses: [192.168.1.223,192.168.1.222]  //dns服务器           
      optional: true
    eno2:     //外网口
      dhcp4: false
      addresses: [192.168.100.12/24]  //外网ip
      gateway4: 192.168.100.1   //外网网关
      optional: true

    eno3:
      addresses: []
      dhcp4: true

      optional: true
    eno4:
      addresses: []
      dhcp4: true
      optional: true

Save the file and apply

sudo netplan apply

ps: The yaml file format is very special. After writing the file, you need to verify the format. Refer to the website: https://www.bejson.com/validators/yaml_editor/

Restart network service command

Turn off the network service command:

sudo service network-manager stop

Restart the network service command:

sudo service network-manager restart

Guess you like

Origin blog.csdn.net/qq_43314560/article/details/112536010