Detailed basic network configuration

Detailed basic network configuration

1 basic network configuration

The linux host access to a network, the network settings configure

Generally include the following:

  • CPU name

  • IP/netmask

  • Route: Default Gateway

  • DNS server
    primary DNS server

    Secondary DNS servers

2 network configuration commands

2.1 Network configuration

  • Static specify: command or configuration file
  • Dynamic allocation: DHCP

2.2 ifconfig

Command Format

ifconfig [interface]
ifconfig -a
ifconfig IFACE [up|down]
ifconfig interface [aftype] options | address ...
ifconfig IFACE IP/netmask [up]
ifconfig IFACE IP netmask NETMASK

Note: effective immediately

Enable promiscuous mode: [-] promisc

example:

[root@centos8 ~]#ifconfig eth0 10.0.0.68 netmask 255.255.0.0

#清除eth0上面的IP地址
[root@centos8 ~]#ifconfig eth0 0.0.0.0/0

#启用和禁用网卡
[root@centos8 ~]#ifconfig eth0 down
[root@centos8 ~]#ifconfig eth0 up

[root@centos7 ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.173  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::20c:29ff:fe06:9835  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:06:98:35  txqueuelen 1000  (Ethernet)
        RX packets 408  bytes 39533 (38.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 365  bytes 42607 (41.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.174  netmask 255.255.255.0  broadcast 10.0.0.255
        ether 00:0c:29:06:98:35  txqueuelen 1000  (Ethernet)

...省略...

[root@centos7 ~]# 
[root@centos7 ~]# ifconfig eth0:1 down
[root@centos7 ~]# ifconfig eth0:1
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:06:98:35  txqueuelen 1000  (Ethernet)

[root@centos7 ~]# 

example:

[root@centos7 ~]# ifconfig -s
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0             1500      556      0      0 0           458      0      0      0 BMRU
lo              65536       97      0      0 0            97      0      0      0 LRU
[root@centos7 ~]# ifconfig -s eth0
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0             1500      586      0      0 0           476      0      0      0 BMRU
[root@centos7 ~]# 

2.3 route command

Routing Table Management Command

Routing Table main components:

[root@centos7 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.2        0.0.0.0         UG    100    0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 eth0
  • Destination: the destination network ID, network ID indicates the target can be reached, 0.0.0.0 / 0 means that all unknown network, also known as the default path
    of the lowest priority
  • Genmask: target network corresponding netmask
  • Iface: to reach the corresponding network, should be sent out from the current host to which the card
  • Gateway: indirectly connected to the network, send data to the IP address of the interface adjacent, towards the host (next) router, if
    a directly connected network, gateway is 0.0.0.0
  • Metric: Cost of cost, the smaller the value, the highest priority routing records

View the routing table:

[root@centos7 ~]# route
[root@centos7 ~]# route -n 

example:

[root@centos7 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 eth0
[root@centos7 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.2        0.0.0.0         UG    100    0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 eth0
[root@centos7 ~]# 

Add routing information: route add

route add [-net|-host|default] target [netmask Nm] [gw GW] [[dev] If]

Delete route: route del

route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]

example:

#目标:192.168.1.3 网关:172.16.0.1
route add -host 192.168.1.3 gw 172.16.0.1 dev eth0

#目标:192.168.0.0 网关:172.16.0.1
route add -net 192.168.0.0 netmask 255.255.255.0 gw 172.16.0.1 dev eth0
route add -net 192.168.0.0/24 gw 172.16.0.1 dev eth0
route add -net 192.168.8.0/24 dev eth1 metric 200


#默认路由,网关:172.16.0.1
route add -net 0.0.0.0 netmask 0.0.0.0 gw 172.16.0.1
route add default gw 172.16.0.1

#目标:192.168.1.3 网关:172.16.0.1
route del -host 192.168.1.3


#目标:192.168.0.0 网关:172.16.0.1
route del -net 192.168.0.0 netmask 255.255.255.0

Dynamic routing 2.4

Get through dynamic routing daemon

Quagga installation package, vtysh configuration command

Supports multiple routing protocols: RIP, OSPF and BGP

2.5 netstat command

Display the network connection:

netstat [--tcp|-t] [--udp|-u] [--raw|-w] [--listening|-l] [--all|-a] [--numeric|-n] [--extend|-e[--extend|-e]] [--program|-p]

Common options

-t: tcp协议相关
-u: udp协议相关
-w: raw socket相关
-l: 处于监听状态
-a: 所有状态
-n: 以数字显示IP和端口
-e:扩展格式
-p: 显示相关进程及PID

Common combinations

-tan, -uan, -tnl, -unl

Displays the routing table

netstat {--route|-r} [--numeric|-n]
-r: 显示内核路由表
-n: 数字格式

Display interface statistics

netstat -i
netstat –I=IFACE
ifconfig -s IFACE

example:

[root@centos7 ~]# netstat -I=eth0
Kernel Interface table
Iface             MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0             1500      887      0      0 0           702      0      0      0 BMRU
[root@centos7 ~]# ifconfig -s eth0
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0             1500      953      0      0 0           742      0      0      0 BMRU
[root@centos7 ~]# netstat -nt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0     52 10.0.0.173:22           10.0.0.1:53127          ESTABLISHED
[root@centos7 ~]# 

2.6 ip command (Key)

Iproute from the package, instead of the future ifconfig

Linux 2.6.1 Configuring Network Properties
ip [ OPTIONS ] OBJECT { COMMAND | help }

Command Description:

OBJECT := { link | addr | route }
ip link - network device configuration
set dev IFACE,可设置属性:	up and down:激活或禁用指定接口,相当于 ifup/ifdown
show [dev IFACE] [up]::指定接口 ,up 仅显示处于激活状态的接口

ip address management

ip addr { add | del } IFADDR dev STRING [label LABEL] [scope {global|link|host}]
[broadcast ADDRESS]

[label LABEL]:添加地址时指明网卡别名
[scope {global|link|host}]:指明作用域,global: 全局可用.link: 仅链接可用,host: 本机可用

[broadcast ADDRESS]:指明广播地址

ip address show
ip addr flush
#禁用网卡
ip link set eth1 down

#网卡改名
ip link set eth1 name songnet

#启用网卡
ip link set songnet up

#网卡别名
ip addr add 172.16.100.100/16 dev eth0 label eth0:0
ip addr del 172.16.100.100/16 dev eth0 label eth0:0

#清除网络地址
ip addr flush dev eth0

example:

[root@centos7 ~]# ip a a 10.0.0.174/24 dev eth0
[root@centos7 ~]# ip a 
......
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:06:98:35 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.173/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.174/24 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe06:9835/64 scope link 
       valid_lft forever preferred_lft forever
[root@centos7 ~]# 

2.6.1 Routing Management

ip route Usage

#添加路由:
ip route add TARGET via GW dev IFACE src SOURCE_IP
		TARGET:
			主机路由:IP
   		    网络路由:NETWORK/MASK
   		    
#添加网关:
ip route add default via GW dev IFACE

#删除路由:
ip route del TARGET

#显示路由:
ip route show|list

#清空路由表:
ip route flush [dev IFACE] [via PREFIX]

example:

ip route add 192.168.0.0/24 via 172.16.0.1
ip route add 192.168.1.100 via 172.16.0.1
ip route add default via 172.16.0.1
ip route flush dev eth0

2.7 ss command

netstat socket information acquired by traversing / proc, ss using the communication with the kernel netlink socket information obtaining module tcp_diag

format:

ss [OPTION]... [FILTER]

Options:

-t: tcp协议相关
-u: udp协议相关
-w: 裸套接字相关
-x:unix sock相关
-l: listen状态的连接
-a: 所有
-n: 数字格式
-p: 相关的程序及PID
-e: 扩展的信息
-m:内存用量
-o:计时器信息

Format Description:

FILTER : [ state TCP-STATE ] [ EXPRESSION ]
TCP的常见状态:
	tcp finite state machine:
		LISTEN: 监听
		ESTABLISHED:已建立的连接
        FIN_WAIT_1
		FIN_WAIT_2
		SYN_SENT
		SYN_RECV
		CLOSED
EXPRESSION:
	dport =
	sport =

Common combination:

-tan, -tanl, -tanlp, -uan

Example: common usage

#显示本地打开的所有端口
ss -l
#显示每个进程具体打开的socket
ss -pl
#显示所有tcp socket
ss -t -a
#显示所有的UDP Socekt
ss -u -a
#显示所有已建立的ssh连接
ss -o state established '( dport = :ssh or sport = :ssh )'
#显示所有已建立的HTTP连接
ss -o state established '( dport = :http or sport = :http )'
[root@centos8 ~]#ss -no state established '( dport = :21 or sport = :21 )'
Netid 					Recv-Q					 Send-Q
Local Address:Port 									Peer Address:Port
tcp 					0 							0
[::ffff:10.0.0.8]:21 								[::ffff:10.0.0.7]:46638
		timer:(keepalive,119min,0)
#列出当前socket详细信息
ss -s

3 Network Configuration File

Network Basic Profile 3.1

IP, MASK, GW, DNS related configuration files:

/etc/sysconfig/network-scripts/ifcfg-IFACE

Description Reference:

/usr/share/doc/initcripts-*/sysconfig.txt

Common Configuration

Set up Explanation
TYPE Interface type, some common Ethernet, Bridge
NAME This configuration file to device
DEVICE Equipment name
HWADDR MAC address of the device
UUID Unique identification devices
BOOTPROTO: Address configuration protocol used to activate the device, commonly dhcp, static, none, bootp
IPADDR Specify the IP address
NETMASK Subnet mask, such as: 255.255.255.0
PREFIX Network ID bits, such as: 24
GATEWAY Default Gateway
DNS1 The first DNS server address
DNS2 The second DNS server address
DOMAIN When the host is not complete, automatically search for the domain name suffix
ONBOOT This device is activated when the system boots
USERCTL Whether the average user can control the device
PEERDNS If BOOTPROTO value "dhcp", YES will allow the allocation dhcp server dns service
information directly to the cover /etc/resolv.conf file, NO not modify resolv.conf
NM_CONTROLLED NM NetworkManager is shorthand for this card whether to accept NM control

example:

[root@centos7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.0.0.173
PREFIX=24
GATEWAY=10.0.0.2
DNS1=223.5.5.5
DNS2=180.76.76.76
[root@centos7 ~]# 

3.2 Configuring the host name of the current host

#centos6 之前版本
/etc/sysconfig/network
HOSTNAME=

#centos7 以后版
/etc/hostname
HOSTNAME

#更改完主机名后,添加与IP地址的映射
#优先于使用DNS前检查  getent hosts 查看/etc/hosts 内容
[root@centos7 ~]# vim /etc/hosts

3.3 DNS name resolution

/etc/resolv.conf
nameserver DNS_SERVER_IP1
nameserver DNS_SERVER_IP2
nameserver DNS_SERVER_IP3
search DOMAIN

3.4 modify / etc / hosts and DNS priority

/etc/nsswitch.conf
hosts: files dns

3.5 routing related configuration files

/etc/sysconfig/network-scripts/route-IFACE

两种风格:
(1) TARGET via GW
如:10.0.0.0/8 via 172.16.0.1

(2) 每三行定义一条路由
ADDRESS#=TARGET
NETMASK#=mask
GATEWAY#=GW

4 CentOS 7 above network configuration

4.1 Change the name of the NIC as eth0

  1. Edit / etc / default / grub configuration file

    GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"
    
  2. Generate its configuration file to grub2

    grub2-mkconfig -o /etc/grub2.cfg
    
  3. Reboot the system

4.2 hostname

Profiles

/etc/hostname

Default Without this file, obtain the host name through reverse resolution DNS, host name defaults to: localhost.localdomain

Set the host name

hostnamectl set-hostname centos7.swyer.club

Delete the file / etc / hostname, hostname localhost.localdomain recovery

Show host name information

hostname
hostnamectl status

example:

[root@centos7 ~]# hostnamectl status
   Static hostname: centos7.swyer.club
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 0018bccee2aa4a94bf6ede9328aeb9ff
           Boot ID: b1655cb208c54cee8ddf1f5696a73931
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1062.el7.x86_64
      Architecture: x86-64
[root@centos7 ~]# 

4.3 network configuration tool nmcli

nmcli command related terms

  • That is, the network interface device
  • Connection configuration of the network interface, a network interface may have a plurality of connection configuration, but only one connection configuration takes effect

format:

nmcli [ OPTIONS ] OBJECT { COMMAND | help }

OPTIONS:

device 	- show and manage network interfaces
nmcli 	device help
connection 	- start, stop, and manage network connections
nmcli connection help

Modify the IP address and other attributes:

nmcli connection modify IFACE [+|-]setting.property value
setting.property: ipv4.addresses ipv4.gateway ipv4.dns1 ipv4.method manual | auto

** modify the configuration files to perform to take effect: ** CentOS reboot to load more than 7 card information to change this command

nmcli conn reload
nmcli conn up con-name

example:

#查看帮助
[root@centos7 ~]# nmcli con add help

#使用nmcli配置网络
[root@centos7 ~]# nmcli con show

#显示所有活动连接
[root@centos7 ~]# nmcli con show --active

#显示网络连接配置
[root@centos7 ~]# nmcli con show "eth0"

#显示设备状态
[root@centos7 ~]# nmcli dev status
DEVICE  TYPE      STATE      CONNECTION 
eth0    ethernet  connected  eth0       
lo      loopback  unmanaged  -- 

#显示网络接口属性
[root@centos7 ~]# nmcli dev show eth0
#创建新连接default,IP自动通过dhcp获取
[root@centos7 ~]# nmcli con add con-name default type Ethernet ifname eth0
#删除连接
[root@centos7 ~]# nmcli con del default
#创建新连接static ,指定静态IP,不自动连接
[root@centos7 ~]# nmcti con add con-name static ifname eth0 autoconnect no type Ethernet
ipv4.addresses 172.25.X.10/24 ipv4.gateway 172.25.X.254
#启用static连接配置
[root@centos7 ~]# nmcli con up static
#启用default连接配置
[root@centos7 ~]# nmcli con up default
#修改连接设置
[root@centos7 ~]# nmcli con mod “static” connection.autoconnect no
[root@centos7 ~]# nmcli con mod “static” ipv4.dns 172.25.X.254
[root@centos7 ~]# nmcli con mod “static” +ipv4.dns 8.8.8.8
[root@centos7 ~]# nmcli con mod “static” -ipv4.dns 8.8.8.8
[root@centos7 ~]# nmcli con mod “static” ipv4.addresses “172.16.X.10/24 172.16.X.254”
[root@centos7 ~]# nmcli con mod “static” +ipv4.addresses 10.10.10.10/16
#DNS设置存放在/etc/resolv.conf,PEERDNS=no 表示当IP通过dhcp自动获取时,dns仍是手动设置,不自动获取等价于下面命令
[root@centos7 ~]# nmcli con mod “system eth0” ipv4.ignore-auto-dns yes

5 Ubuntu Network Configuration

Document official website:
https://help.ubuntu.com/
https://help.ubuntu.com/lts/serverguide/network-configuration.html.zh-CN

5.1 hostname

Ubuntu modify the hostname

hostnamectl set-hostname ubuntu1804.swyer.club

Permanently change the method is identical with CentOS

5.2 NIC configuration

Dynamic IP configuration

root@ubuntu1804:~# cat /etc/netplan/01-netcfg.yaml 
network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:
      dhcp4: yes
      
root@ubuntu1804:~# netplan apply 

Configure a static IP

root@ubuntu1804:~# cat /etc/network/interfaces 
# ifupdown has been replaced by netplan(5) on this system.  See
# /etc/netplan for current configuration.    #这里说明了网络配置的新路径
# To re-enable ifupdown on this system, you can run:
#    sudo apt install ifupdown
root@ubuntu1804:~# ls /etc/netplan/
01-netcfg.yaml
root@ubuntu1804:~# 
root@ubuntu1804:~# cat /etc/netplan/01-netcfg.yaml 
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:
      addresses: [10.0.0.100/24]      
      gateway4: 10.0.0.2
      dhcp4: no
      nameservers:
              addresses: [180.76.76.76,223.6.6.6]
root@ubuntu1804:~# 
#一定要注意缩进
root@ubuntu1804:~# netplan apply     #设置静态IP和DNS后,更新网络设置

5.3 NIC renamed eth0

#修改配置文件为下面形式
vi /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0"

#生效新的grub.cfg文件
grub-mkconfig -o /boot/grub/grub.cfg
#或者
update-grub

#重启生效
reboot

example:

root@ubuntu1804:~# ip a 
...省略...
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:51:27:7c brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.100/24 brd 10.0.0.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe51:277c/64 scope link 
       valid_lft forever preferred_lft forever
root@ubuntu1804:~# vim /etc/default/grub 
#找到GRUB_CMDLINE_LINUX=""
#修改为GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"

#重新生成grub引导配置文件
root@ubuntu1804:~# grub-mkconfig -o /boot/grub/grub.cfg

#切记一定要更改网络配置文件
root@ubuntu1804:~# vim /etc/netplan/01-netcfg.yaml
root@ubuntu1804:~# cat /etc/netplan/01-netcfg.yaml 
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:    #对应的eth0
      addresses: [10.0.0.100/24]      
      gateway4: 10.0.0.2
      dhcp4: no
      nameservers:
              addresses: [180.76.76.76,223.6.6.6]
root@ubuntu1804:~# reboot
Published 12 original articles · won praise 0 · Views 398

Guess you like

Origin blog.csdn.net/swyer_66/article/details/104897815