Linux network service network settings related commands

Keywords: Linux network DNS CentOS

Linux network settings

  1. View network settings

  2. Test network connection

  3. Use network configuration command
  4. Modify network profile

This article mainly introduces some basic and common Linux network setting commands and some script file configuration steps in Linux.

I. view and test network configuration

1.ifconfig -- View network interface address

Format: ifconfig [network interface name] [ip address] (subnet mask)

[root@lokott ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.68.130  netmask 255.255.255.0  broadcast 192.168.68.255
        inet6 fe80::7eb1:2dde:8a54:6927  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)
        RX packets 3556  bytes 232528 (227.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 104  bytes 16400 (16.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 56  bytes 6144 (6.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 56  bytes 6144 (6.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:71:37:09  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@lokott ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.68.130  netmask 255.255.255.0  broadcast 192.168.68.255
        inet6 fe80::7eb1:2dde:8a54:6927  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)
        RX packets 3560  bytes 232768 (227.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 104  bytes 16400 (16.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2.hostname -- view the hostname

Format: hostname

[root@lokott ~]# hostname
lokott
[root@lokott ~]# cat /etc/hostname
lokott
[root@lokott ~]# echo "local" > /etc/hostname 
[root@lokott ~]# cat /etc/hostname
local
[root@lokott ~]#You need to restart to set successfully. Another way is to use hostnamectl set hostname ^ c
[root@lokott ~]#reboot
[root@local ~]# hostnamectl set-hostname lokott
[root@local ~]# su
[root@lokott ~]# For lokott ^ c

3.route - view route entries

-n: digital display (almost the same in CentOS 6 and 7)

[root@lokott ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
192.168.68.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@lokott ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.68.2    0.0.0.0         UG    100    0        0 ens33
192.168.68.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

4.netstat -- check the network connection

-n serial number

-p port number

-a all service ports

-r display routing table information

-l display network connection and port information in monitoring status

-t tcp

-u udp

[root@lokott ~]# netstat -natp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1490/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      941/sshd            
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      939/cupsd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1186/master         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      941/sshd            
tcp6       0      0 ::1:631                 :::*                    LISTEN      939/cupsd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1186/master         

[root@lokott ~]# netstat -nuap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
udp        0      0 0.0.0.0:18066           0.0.0.0:*                           3935/dhclient       
udp        0      0 192.168.122.1:53        0.0.0.0:*                           1490/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           1490/dnsmasq        
udp        0      0 0.0.0.0:68              0.0.0.0:*                           3935/dhclient       
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           553/avahi-daemon: r 
udp        0      0 0.0.0.0:50467           0.0.0.0:*                           553/avahi-daemon: r 
udp6       0      0 :::61206                :::*                                3935/dhclient    

[root@lokott ~]# netstat -r / / same as route
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         gateway         0.0.0.0         UG        0 0          0 ens33
192.168.68.0    0.0.0.0         255.255.255.0   U         0 0          0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0

5.traceroute -- test the network nodes passing from the current host to the target host

[root@lokott ~]# traceroute 127.0.0.1
traceroute to 127.0.0.1 (127.0.0.1), 30 hops max, 60 byte packets
 1  localhost (127.0.0.1)  0.030 ms  0.007 ms  0.035 ms
[root@lokott ~]# traceroute 192.168.10.11
traceroute to 192.168.10.11 (192.168.10.11), 30 hops max, 60 byte packets
 1  gateway (192.168.68.2)  0.154 ms  0.085 ms  0.087 ms
 2  * * *
 3  * * *
 4  * * *
 5  * * *
 6  * * *
 7  *^C
[root@lokott ~]# 

6.nslookup -- Test DNS domain name resolution

[root@lokott ~]# nslookup www.taobao.com
Server:     192.168.68.2
Address:    192.168.68.2#53

Non-authoritative answer:
www.taobao.com  canonical name = www.taobao.com.danuoyi.tbcache.com.
Name:   www.taobao.com.danuoyi.tbcache.com
Address: 111.3.79.235
Name:   www.taobao.com.danuoyi.tbcache.com
Address: 111.3.79.234
Name:   www.taobao.com.danuoyi.tbcache.com
Address: 211.138.124.238

Add: the dos interface of Windows system is also applicable

2. Set the network address parameters

1. Temporary configuration - used when debugging the network

2. Fixed configuration - to change the configuration file, you need to restart the network service or restart the host before it takes effect, which is equivalent to permanent configuration.

7.ifdown and ifup disable and activate network interface

[root@lokott ~]# ifconfig ens33:0 192.168.155.55 / / virtual interface configuration
[root@lokott ~]# ifconfig ens33:0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.155.55  netmask 255.255.255.0  broadcast 192.168.155.255
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)

[root@lokott ~]# ifconfig ens33:0 down  
[root@lokott ~]# ifconfig ens33:0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)

[root@lokott ~]# ifconfig ens33:0 192.168.155.55
[root@lokott ~]# ifdown ens33:0 
//Usage: ifdown < device name >
[root@lokott ~]# ifdown ens33 
//Device 'ens33' disconnected successfully.
[root@lokott ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)
        RX packets 5048  bytes 329861 (322.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 490  bytes 46907 (45.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@lokott ~]# ifconfig ens33:0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)
[root@lokott ~]# ifup ens33 
//Connection activated successfully (D-Bus active path / org/freedesktop/NetworkManager/ActiveConnection/6)
[root@lokott ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.68.130  netmask 255.255.255.0  broadcast 192.168.68.255
        inet6 fe80::7eb1:2dde:8a54:6927  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)
        RX packets 5082  bytes 332183 (324.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 514  bytes 50940 (49.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

8.route add and delete static route records

[root@lokott ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
192.168.68.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@lokott ~]# route add -net 192.168.10.0/24 gw 192.168.122.1
[root@lokott ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.68.2    0.0.0.0         UG    100    0        0 ens33
192.168.10.0    192.168.122.1   255.255.255.0   UG    0      0        0 virbr0
192.168.68.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@lokott ~]# Route del - net 192.168.10.0/24 / / delete route record
[root@lokott ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.68.2    0.0.0.0         UG    100    0        0 ens33
192.168.68.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

[root@lokott ~]# route del default gw 192.168.68.2 / / delete the default gateway
[root@lokott ~]# route add default gw 192.168.68.2 / / add default gateway

9. Network interface profile

[root@lokott ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE="Ethernet"                     //Type is Ethernet
PROXY_METHOD="none"                 
BROWSER_ONLY="no"
BOOTPROTO="dhcp"                      //Dynamic acquisition
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="ens33"                        //Network card name is ens33
UUID="5785d9d2-2603-4b14-bbfb-d95f6545e260"
DEVICE="ens33"                      //Set the network interface name to ens33
ONBOOT="yes"                        //Set the network interface to activate when the Linux system starts
[root@lokott ~]# 

Of course, we can set the static ip address as follows (be sure to restart the network service):

[root@lokott ~]# vim  /etc/sysconfig/network-scripts/ifcfg-ens33 
[root@lokott ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
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="ens33"
UUID="5785d9d2-2603-4b14-bbfb-d95f6545e260"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.68.130"
NETMASK="255.255.255.0"
GATEWAY="192.168.68.2"
[root@lokott ~]# systemctl restart network
[root@lokott ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.68.130  netmask 255.255.255.0  broadcast 192.168.68.255
        inet6 fe80::7eb1:2dde:8a54:6927  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)
        RX packets 5803  bytes 377581 (368.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 552  bytes 55914 (54.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 168  bytes 16496 (16.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 168  bytes 16496 (16.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:71:37:09  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@lokott ~]# 

10. Domain name resolution profile

1) specify the server address information to provide DNS resolution for this computer

[root@lokott ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 192.168.68.2
[root@lokott ~]# 

2) local host mapping file

[root@lokott ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

Generally, the host information that needs to be accessed frequently can be stored here. According to the mapping table here, you can find whether there is a corresponding record, without going to the DNS server for query, so as to improve the speed of Internet access. However, an exception will occur if the error is added.

Posted by Bobby_Jo on Tue, 19 Nov 2019 03:24:32 -0800