1.基本网络配置

将Linux主机接入到网络,需要配置网络相关设置

一般包括如下内容:

 主机名

IP/netmask

 路由:默认网关    DNS服务器     主DNS服务器    次DNS服务器    第三个DNS服务器

 CentOS 6 之前版本网卡名称

接口命名方式:CentOS 6

以太网:eth[0,1,2,...]

ppp:ppp[0,1,2,...]

网络接口识别并命名相关的udev配置文件:

/etc/udev/rules.d/70-persistent-net.rules

查看网卡:

dmesg |grep –i eth

ethtool -i eth0

卸载网卡驱动:

modprobe -r e1000

rmmod e1000

装载网卡驱动:

modprobe e1000

临时修改网卡名称

[root@centos6 ~]#ip link set eth0 down
[root@centos6 ~]#ip link set eth0 name abc
[root@centos6 ~]#ip link set abc up

2.网络配置命令 

静态指定:

ifconfig, route, netstat

ip: object {link, addr, route}, ss, tc

system-config-network-tui,setup

配置文件

动态分配:DHCP: Dynamic Host Configuration Protocol

[root@c7-137-mariadb-bak ~]# cat /etc/resolv.conf 
# Generated by NetworkManager

# No nameservers found; try putting DNS servers into your
# ifcfg files in /etc/sysconfig/network-scripts like so:
#
search wangxiaochun.com #自动补全后缀
# DNS2=xxx.xxx.xxx.xxx
# DOMAIN=lab.foo.com bar.foo.com
nameserver 10.0.0.2

#自动补全后缀
[root@c7-137-mariadb-bak ~]# ping www
PING www.wangxiaochun.com (123.56.174.200) 56(84) bytes of data.
64 bytes from 123.56.174.200: icmp_seq=1 ttl=128 time=45.2 ms
64 bytes from 123.56.174.200: icmp_seq=2 ttl=128 time=45.1 ms
64 bytes from 123.56.174.200: icmp_seq=3 ttl=128 time=45.2 ms
^C

判断是否是自动获取的IP

[root@c7-147 ~]# ss -ntlu
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 *:670 *:*
udp UNCONN 0 0 127.0.0.1:914 *:*
udp UNCONN 0 0 *:111 *:*
udp UNCONN 0 0 *:34187 *:*
udp UNCONN 0 0 :::670 :::*
udp UNCONN 0 0 :::58121 :::*
udp UNCONN 0 0 :::111 :::*
tcp LISTEN 0 128 *:111 *:*
tcp LISTEN 0 128 *:22 *:*
tcp LISTEN 0 100 127.0.0.1:25 *:*
tcp LISTEN 0 128 *:54429 *:*
tcp LISTEN 0 128 :::111 :::*
tcp LISTEN 0 128 :::22 :::*
tcp LISTEN 0 100 ::1:25 :::*
tcp LISTEN 0 128 :::42277 :::*
[root@c7-147 ~]# ss -ntlu |grep 68 #是否有68号开启

3.ifconfig命令

来自于net-tools包,建议使用 ip 代替

[root@c7-147 ~]# rpm -qi net-tools
Name : net-tools
Version : 2.0
Release : 0.25.20131004git.el7
Architecture: x86_64
Install Date: Wed 08 Sep 2021 08:43:13 PM CST
Group : System Environment/Base
Size : 938978
License : GPLv2+
Signature : RSA/SHA256, Fri 23 Aug 2019 05:36:04 AM CST, Key ID 24c6a8a7f4a80eb5
Source RPM : net-tools-2.0-0.25.20131004git.el7.src.rpm
Build Date : Fri 09 Aug 2019 09:10:26 AM CST
Build Host : x86-02.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://sourceforge.net/projects/net-tools/
Summary : Basic networking tools
Description :
The net-tools package contains basic networking tools,
including ifconfig, netstat, route, and others.
Most of them are obsolete. For replacement check iproute package.

ifconfig [interface]

ifconfig -a

ifconfig IFACE [up|down]

ifconfig interface [aftype] options | address ...

ifconfig IFACE IP/netmask [up]

ifconfig IFACE IP netmask NETMASK

# 添加IP地址
[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

#对一个网卡设置多个IP地址
[root@centos8 ~]#ifconfig eth0:1 172.16.0.8/24

#将刚才的IP地址关闭
[root@centos8 ~]#ifconfig eth0:1 down

 ifconfig -s 统计当前网卡的流量

[root@c7-147 ~]# ifconfig -s
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 92 0 0 0 70 0 0 0 BMRU
eth1 1500 0 0 0 0 0 0 0 0 BMU
lo 65536 2 0 0 0 2 0

#只看eth0
[root@c7-137-mariadb-bak ~]# ifconfig -s eth0
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 97595 0 0 0 98686 0 0 0 BMRU

# 1秒就监测一次
[root@c7-147 ~]# watch -n1 ifconfig -s
#GJ一次
[root@c7-137-mariadb-bak ~]# ping -f 10.0.0.147 -s 65507
PING 10.0.0.147 (10.0.0.147) 65507(65535) bytes of data.
..^C
--- 10.0.0.147 ping statistics ---
2142 packets transmitted, 2140 received, 0% packet loss, time 8713ms

4.route

路由表管理命令

路由表主要构成:

 Destination: 目标网络ID,表示可以到达的目标网络ID,0.0.0.0/0 表示所有未知网络,又称为默认路由,优先级最低

Genmask:目标网络对应的netmask

  Iface: 到达对应网络,应该从当前主机哪个网卡发送出来

  Gateway: 到达非直连的网络,将数据发送到临近(下一个)路由器的临近本主机的接口的IP地址,如果

是直连网络,gateway是0.0.0.0

  Metric: 开销cost,值越小,路由记录的优先级最高

#任何网络设备都路由表
[root@c7-147 ~]# 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

添加:route add

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

删除:route del

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

#目标: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 -net 0.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