#头条创作挑战赛#
1.1 基本网络配置
将Linux主机接入到网络,需要配置网络相关设置
一般包括如下内容:
- 主机名
- IP/netmask
- 路由:默认网关
- DNS服务器(主DNS服务器、次DNS服务器、第三个DNS服务器)
1.2 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
1.3 网络配置命令
1.3.1 网络配置方式
-
静态指定:
ifconfig, route, netstat
ip: object {link, addr, route}, ss, tc
system-config-network-tui,setup
配置文件
- 动态分配:DHCP: Dynamic Host Configuration Protocol
1.3.2 ifconfig命令
- ifconfig命令来自于net-tools包,在一些新的Linux发行版中已被弃用,推荐使用ip命令来替代,在CentOS 6中,ifconfig仍然可用。
- ifconfig命令是一个用于配置和显示网络接口信息的命令,在CentOS 6中,ifconfig命令已被ifconfig工具替代。
案例:以下是ifconfig命令的一些常见用法和示例:
#显示所有网络接口信息:
ifconfig
#显示指定网络接口信息:
ifconfig eth0
#激活或停用网络接口:
ifconfig eth0 up # 激活eth0接口
ifconfig eth0 down # 停用eth0接口
#配置IP地址和子网掩码:
ifconfig eth0 192.168.1.100 netmask 255.255.255.0
#配置启用广播和禁用广播:
ifconfig eth0 broadcast 192.168.1.255 # 启用广播
ifconfig eth0 -broadcast # 禁用广播
#配置启用多播和禁用多播:
ifconfig eth0 multicast # 启用多播
ifconfig eth0 -multicast # 禁用多播
#查看net-tools包的安装详情
rpm -qi net-tools
格式:
ifconfig [interface]
ifconfig -a
ifconfig IFACE [up|down]
ifconfig interface [aftype] options | address ...
ifconfig IFACE IP/netmask [up]
ifconfig IFACE IP netmask NETMASK
- 注意:立即生效
- 启用混杂模式:[-]promisc
1.3.3 route命令
路由表管理命令
路由表主要构成:
- Destination: 目标网络ID,表示可以到达的目标网络ID,0.0.0.0/0 表示所有未知网络,又称为默认路由,优先级最低
- Genmask:目标网络对应的netmask
- Iface: 到达对应网络,应该从当前主机哪个网卡发送出来
- Gateway: 到达非直连的网络,将数据发送到临近(下一个)路由器的临近本主机的接口的IP地址,如果是直连网络,gateway是0.0.0.0
- Metric: 开销cost,值越小,路由记录的优先级最高
查看路由表:
route #显示当前路由表
route -n #查看详细的路由表信息(包括接口和Metric)
案例:
[root@centos7 ~]#route
[root@centos7 ~]#route -n
添加:route add
route add [-net|-host|default] target [netmask Nm] [gw GW] [[dev] If]
#添加一个默认路由:
route add default gw 192.168.1.1
删除:route del
route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]
#删除一个路由表项
route del default gw 192.168.1.1
案例:
#目标: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
案例:实现静态路由
环境:
四台主机:
A主机:eth0 NAT模式
R1主机:eth0 NAT模式,eth1 仅主机模式
R2主机:eth0 桥接模式,eth1 仅主机模式
B主机:eth0 桥接模式
#配置A主机
ifconfig eth0 10.0.0.123/8
route add -net 10.0.0.0/8 dev eth0
route add default gw 10.0.0.200 dev eth0
#配置R1
ifconfig eth0 10.0.0.200/8
ifconfig eth1 192.168.0.200/24
route add -net 10.0.0.0/8 dev eth0
route add -net 192.168.0.0/24 dev eth1
route add -net 172.16.0.0/16 gw 192.168.0.201 dev eth1
echo 1 > /proc/sys/net/ipv4/ip_forward
#配置R2
ifconfig eth0 172.16.0.200/16
ifconfig eth1 192.168.0.201/24
route add -net 192.168.0.0/24 dev eth1
route add -net 172.16.0.0/16 dev eth0
route add -net 10.0.0.0/8 gw 10.0.0.200 dev eth1
echo 1 > /proc/sys/net/ipv4/ip_forward
#配置B
ifconfig eth0 172.16.0.123/16
route add -net 172.16.0.0/16 dev eth0
route add default gw 172.16.0.200 dev eth0
案例:
[root@router ~]#echo 1 > /proc/sys/net/ipv4/ip_forward
1.3.4 配置动态路由
通过守护进程获取动态路由,安装quagga包,通过命令vtysh配置
支持多种路由协议:
- RIP:Routing Information Protocol,路由信息协议
- OSPF:Open Shortest Path First,开放式最短路径优先
- BGP:Border Gateway Protocol,边界网关协议
- RIP、OSPF和BGP
1.3.5 netstat命令
来自于net-tools包,建议使用 ss 代替
显示网络连接:
netstat [--tcp|-t] [--udp|-u] [--raw|-w] [--listening|-l] [--all|-a] [--
numeric|-n] [--extend|-e[--extend|-e]] [--program|-p]
常用选项
-t: tcp协议相关
-u: udp协议相关
-w: raw socket相关
-l: 处于监听状态
-a: 所有状态
-n: 以数字显示IP和端口
-e:扩展格式
-p: 显示相关进程及PID
常用组合:
-tan, -uan, -tnl, -unl
显示路由表:
netstat {--route|-r} [--numeric|-n]
-r: 显示内核路由表
-n: 数字格式
1.3.6 显示接口统计数据
netstat {--interfaces|-I|-i} [iface] [--all|-a] [--extend|-e] [--program|-p] [-
-numeric|-n]
netstat -i
netstat –I=IFACE
ifconfig -s IFACE
案例:
[root@centos7 ~]#netstat -Ieth0
[root@centos7 ~]#ifconfig -s eth0
[root@centos7 ~]#netstat -nt
1.3.7 ip命令
来自于iproute包,可用于代替ifconfig
1.3.7.1 配置Linux网络属性
ip [ OPTIONS ] OBJECT { COMMAND | help }
ip 命令说明:
OBJECT := { link | addr | route }
ip link - network device configuration
set dev IFACE,可设置属性:up and down:激活或禁用指定接口,相当于 ifup/ifdown
show [dev IFACE] [up]::指定接口 ,up 仅显示处于激活状态的接口
man帮助:ip(8), ip-address(8), ip-link(8), ip-route(8)
案例:查看帮助
[root@centos7 ~]#ip help
[root@centos7 ~]#ip link help
[root@centos7 ~]#ip addr help
[root@centos7 ~]#ip route help
ip 地址管理
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 newname
#启用网卡
ip link set newname 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
1.3.7.2 管理路由
ip route 用法
#添加路由:
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]
案例:
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
案例:查看路由过程
#查看到达10.0.0.7所使用的路由
[root@centos7 ~]#ip route get 10.0.0.7
10.0.0.7 dev eth0 src 10.0.0.8 uid 0
cache
#查看到达8.8.8.8所使用的路由
[root@centos7 ~]#ip route get 8.8.8.8
8.8.8.8 via 10.0.0.2 dev eth0 src 10.0.0.8 uid 0
cache
1.3.8 ss 命令
来自于iproute包,代替netstat,netstat 通过遍历 /proc来获取 socket信息,ss 使用 netlink与内核tcp_diag 模块通信获取 socket 信息
格式:
ss [OPTION]... [FILTER]
选项:
-t: tcp协议相关
-u: udp协议相关
-w: 裸套接字相关
-x:unix sock相关
-l: listen状态的连接
-a: 所有
-n: 数字格式
-p: 相关的程序及PID
-e: 扩展的信息
-m:内存用量
-o:计时器信息
格式说明
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 =
常用组合:
-tan, -tanl, -tanlp, -uan
案例:常见用法
#显示本地打开的所有端口
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@centos7 ~]#ss -no state established '( dport = :21 or sport = :21 )'
#列出当前socket详细信息
ss -s