ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

(三)网络基础之ifconfig,route,ss

2021-04-17 21:32:58  阅读:204  来源: 互联网

标签:ss 0.0 route ifconfig net root eth0


基本配置

将Linux主机接入到网络,需要配置网络相关设置
一般包括如下内容:
主机名
IP/netmask
路由:默认网关
DNS服务器
主DNS服务器
次DNS服务器
第三个DNS服务器

接口命名方式: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

网络配置方式

静态指定:
ifconfig, route, netstat
ip: object {link, addr, route}, ss, tc
system-config-network-tui,setup
配置文件
动态分配:DHCP: Dynamic Host Configuration Protocol

ifconfig命令

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

[root@centos8 ~]#rpm -qi net-tools
Name : net-tools
Version : 2.0

范例:

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

注意:立即生效

启用混杂模式:[-]promisc

范例:

[root@centos8 ~]#ifconfig eth0 10.0.0.68 netmask 255.255.0.0
#清除eth0上面的IP地址
[root@centos8 ~]#ifconfig eth0 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
[root@centos8 ~]#ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.8 netmask 255.0.0.0 broadcast 10.255.255.255

[root@centos8 ~]#ifconfig eth0:1 down

[root@centos8 ~]#ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.8 netmask 255.0.0.0 broadcast 10.255.255.255

范例:

[root@centos8 ~]#ifconfig -s
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 673005 0 0 0 672688 0 0
0 BMRU
lo 65536 70 0 0 0 70 0 0
0 LRU
[root@centos8 ~]#ifconfig -s eth0
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 673011 0 0 0 672694 0 0
0 BMRU

route 命令

路由表管理命令

路由表主要构成:

Destination: 目标网络ID,表示可以到达的目标网络ID,0.0.0.0/0 表示所有未知网络,又称为默认路
由,优先级最低
Genmask:目标网络对应的netmask
Iface: 到达对应网络,应该从当前主机哪个网卡发送出来
Gateway: 到达非直连的网络,将数据发送到临近(下一个)路由器的临近本主机的接口的IP地址,如果
是直连网络,gateway是0.0.0.0
Metric: 开销cost,值越小,路由记录的优先级最高

查看路由表:

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

范例:

[root@centos8 ~]#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@centos8 ~]#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]

范例:

#目标:10.0.0.128 网关:172.31.0.254
[09:26:56 root@sz-centos7 ~]# route add -host 10.0.0.128 gw 172.31.0.254
[09:40:53 root@sz-centos7 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.31.0.254    0.0.0.0         UG    100    0        0 eth0
10.0.0.128      172.31.0.254    255.255.255.255 UGH   0      0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.31.0.0      0.0.0.0         255.255.0.0     U     100    0        0 eth0

范例:添加路由

[09:42:34 root@sz-centos7 ~]# route add -net 10.0.0.0/24 gw 172.31.0.254 dev eth0

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

范例:添加默认路由

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

范例:删除某台主机路由

#目标:10.0.0.128 网关:172.31.0.254
route del -host 10.0.0.128

# 删除某个网段的路由
#目标:10.0.0.0 网关:172.31.0.254
route del -net 10.0.0.0 netmask 255.255.255.0

范例:实现静态路由

环境:

四台主机:
A主机:eth0 NAT模式
R1主机:eth0 NAT模式,eth1 仅主机模式
R2主机:eth0 桥接模式,eth1 仅主机模式
B主机:eth0 桥接模式


配置如下:

#配置A主机
ifconfig eth0 172.31.0.7/16

route add -net 172.31.0.0/16 dev eth0
route add default gw 172.31.0.200 dev eth0

#配置R1
ifconfig eth0 172.31.0.17/16
ifconfig eth1 192.168.0.101/24

route add -net 172.31.0.0/16 dev eth0
route add -net 192.168.0.0/24 dev eth1

# dev 相对于R1的出口是eth1
# -net 目标网段B机器: 172.18.0.0/16
# gw 网关:是R2 出口ip
route add -net 172.18.0.0/16 gw 192.168.0.103 dev eth1
echo 1 > /proc/sys/net/ipv4/ip_forward

#配置R2
ifconfig eth0 172.18.8.200/16
ifconfig eth1 192.168.0.103/24

route add -net 192.168.0.0/24 dev eth1
route add -net 172.18.0.0/16 dev eth0

# dev 相对于R2 往A去的是eth1
# -net 目标网段A机器: 172.31.0.0/16
# gw 网关:是R1 出口ip
route add -net 172.31.0.0/16 gw 192.168.0.101 dev eth1
echo 1 > /proc/sys/net/ipv4/ip_forward

#配置B
ifconfig eth0 172.18.8.168/16

route add -net 172.18.0.0/16 dev eth0
route add default gw 172.18.0.200 dev eth0


查看本次路由过了几跳到达目标

[root@localhost ~]# yum install mtr -y
[root@localhost ~]# mtr 172.18.8.168


注意:AB配置必须要有网关,网关:下一个路由器的临近当前路由器的IP

范例:开启路由转发功能

[root@router ~]#echo 1 > /proc/sys/net/ipv4/ip_forward

配置动态路由

通过守护进程获取动态路由,安装quagga包,通过命令vtysh配置
支持多种路由协议:
RIP:Routing Information Protocol,路由信息协议
OSPF:Open Shortest Path First,开放式最短路径优先
BGP:Border Gateway Protocol,边界网关协议

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: 数字格式

显示接口统计数据

netstat {--interfaces|-I|-i} [iface] [--all|-a] [--extend|-e] [--program|-p] [-
-numeric|-n]
netstat -i
netstat –I=IFACE
ifconfig -s IFACE

范例:

[root@centos8 ~]#netstat -Ieth0
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TXOVR
Flg
eth0 1500 4660 0 0 0 2460 0 0
0 BMRU
[root@centos8 ~]# ifconfig -s eth0
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 4745 0 0 0 2518 0 0
0 BMRU
[root@centos8 ~]# netstat -nt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 52 10.0.0.8:22 10.0.0.1:4927 ESTABLISHED

管理路由

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

范例: 查看路由过程

[root@centos8 ~]# ip route get 10.0.0.7
10.0.0.7 dev eth0 src 10.0.0.8 uid 0
cache
[root@centos8 ~]#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

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@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

标签:ss,0.0,route,ifconfig,net,root,eth0
来源: https://www.cnblogs.com/xuanlv-0413/p/14672198.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有