Linux网络管理命令(配置,状态,调试):ifconfig,ethtool,iftop,tcpdump,nc

 

配置文件

网卡配置

vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0 #描述网卡对应的设备别名,例如ifcfg-eth0的文件中它为eth0
BOOTPROTO=static #设置网卡获得ip地址的方式,可能的选项为static,dhcp或bootp,分别对应静态指定的 ip地址,通过dhcp协议获得的ip地址,通过bootp协议获得的ip地址
BROADCAST=192.168.0.255 #对应的子网广播地址
HWADDR=00:07:E9:05:E8:B4 #对应的网卡物理地址
IPADDR=12.168.1.2 #如果设置网卡获得 ip地址的方式为静态指定,此字段就指定了网卡对应的ip地址
IPV6INIT=no
IPV6_AUTOCONF=no
NETMASK=255.255.255.0 #网卡对应的网络掩码
NETWORK=192.168.1.0 #网卡对应的网络地址
ONBOOT=yes #系统启动时是否设置此网络接口,设置为yes时,系统启动时激活此设备

网关配置

vi /etc/sysconfig/network

NETWORKING=yes
HOSTNAME=Aaron
GATEWAY=192.168.1.1
NETWORKING_IPV6=no
PEERNTP=no

DNS配置

vi /etc/resolv.conf

nameserver 8.8.8.8 
nameserver 8.8.4.4 

重启网配置

//方法1:
service network restart

//方法2:
ifconfig eth0 down #换成相应的网卡名,eth0表示第1块网卡
ifconfig eth0 up

测试宽带

[root@01 ~]# wget https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
chmod +rx speedtest.py
[root@01 ~]# sudo mv speedtest.py /usr/local/bin/speedtest-cli
[root@01 ~]# sudo chown root:root /usr/local/bin/speedtest-cli
[root@01 ~]# speedtest-cli

ifconfig

参考:

https://www.runoob.com/linux/linux-comm-ifconfig.html

可设置网络设备的状态,或是显示目前的设置。

ifconfig  #显示网络设备信息

ifconfig eth0 down #关闭指定网卡
ifconfig eth0 up #打开指定网卡

ifconfig eth0 add 33ffe:3240:800:1005::2/ 64 #为网卡设置IPv6地址
ifconfig eth0 del 33ffe:3240:800:1005::2/ 64 #为网卡删除IPv6地址

ifconfig eth0 down //关闭网卡
ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE //修改MAC地址
ifconfig eth0 up //启动网卡

ifconfig eth0 arp  #开启arp协议
ifconfig eth0 -arp  #关闭arp协议

ifconfig eth0 mtu 1500 #设置最大传输单元

ethtool

参考:

https://www.cnblogs.com/danxi/p/6591885.html

 

ethtool eth0       //查询ethx网口基本设置,其中 x 是对应网卡的编号,如eth0、eth1等等
ethtool –h        //显示ethtool的命令帮助(help)
ethtool –i eth0    //查询eth0网口的相关信息
ethtool –d eth0    //查询eth0网口注册性信息
ethtool –r eth0    //重置eth0网口到自适应模式
ethtool –S eth0    //查询eth0网口收发包统计
ethtool –s eth0 [speed 10|100|1000] [duplex half|full]  [autoneg on|off]        //设置网口速率10/100/1000M、设置网口半/全双工、设置网口是否自协商
ethtool -k eth0

iftop

参考:

https://blog.csdn.net/mimi_csdn/article/details/79377344

类似于top的实时流量监控工具。

安装:
yum install -y iftop
使用:
iftop

常用参数:
-i设定监测的网卡,如:# iftop -i eth1
-B 以bytes为单位显示流量(默认是bits),如:# iftop -B
-n使host信息默认直接都显示IP,如:# iftop -n
-N使端口信息默认直接都显示端口号,如: # iftop -N
-F显示特定网段的进出流量,如# iftop -F 10.10.1.0/24或# iftop -F 10.10.1.0/255.255.255.0
-h(display this message),帮助,显示参数信息
-p使用这个参数后,中间的列表显示的本地主机信息,出现了本机以外的IP信息;
-b使流量图形条默认就显示;
-P使host信息及端口信息默认就都显示;
-m设置界面最上边的刻度的最大值,刻度分五个大段显示,例:# iftop -m 100M

返回参数解释:

中间的<= =>这两个左右箭头,表示的是流量的方向。
TX:发送流量
RX:接收流量
TOTAL:总流量
Cumm:运行iftop到目前时间的总流量
peak:流量峰值
rates:分别表示过去 2s 10s 40s 的平均流量

tcpdump

参考:

https://www.jianshu.com/p/d9162722f189(tcpdump详细教程)

https://linux.cn/article-10191-1.html(linux使用tcpdump)

tcpdump [ -DenNqvX ] [ -c count ] [ -F file ] [ -i interface ] [ -r file ]
        [ -s snaplen ] [ -w file ] [ expression ]

tcpdump -i ens33

tcpdump -c 10 -q #精简模式显示 10个包

tcpdump -i ens33 -c 10 net 192.168 #本机与192.168网段通信的数据包

tcpdump -c 10 -nn -i ens33 tcp dst port 22

nc

参考:http://www.linuxso.com/command/nc.html

NetCat,在网络工具中有“瑞士军刀”美誉,其有Windows和Linux的版本。因为它短小精悍(1.84版本也不过25k,旧版本或缩减版甚至更小)、功能实用,被设计为一个简单、可靠的网络工具,可通过TCP或UDP协议传输读写数据。

同时,它还是一个网络应用Debug分析器,因为它可以根据需要创建各种不同类型的网络连接。

语  法:nc [-hlnruz][-g<网关...>][-G<指向器数目>][-i<延迟秒数>][-o<输出文件>][-p<通信端口>][-s<来源地址>][-v...][-w<超时秒数>][主机名称][通信端口...]

#网络发送和监听
nc -l 1234 #激活并监听端口,服务端
nc  192.168.200.27 1234  #给服务端发送报文,默认tcp协议

#远程拷贝文件
nc -l 1234 > 1234.txt  #发送到该服务器
nc -w 1 192.168.200.27 1234 < abc.txt  #从该服务器发送

#克隆硬盘或分区,需要借助dd命令
#不应在已经mount的的系统上进行。
nc -l -p 1234 | dd of=/dev/sda  #拷贝到该服务器
dd if=/dev/sda | nc 192.168.200.27 1234 #从该服务器拷贝

 

posted @ 2021-02-24 10:59  小匡程序员  阅读(578)  评论(0编辑  收藏  举报