网络管理命令大全

1.基本工具
2.网络查看与配置
2.1 网络查看
2.2 网络配置
3.网关查看及配置
3.1 查看网关(或者路由)
3.2 网关配置
4.网络故障排除
5.网络服务管理
5.1 概论
5.2 实践
5.3 主机名
5.4 静态IP配置方式1
5.5 静态IP配置方式2
0.导语

大家好,根据我最近的面试经验,以及日常使用的网络命令,写出这篇文章,本文基于Ubuntu18.04撰写,
相信大多服务端用的Centos,本文也给出了Centos与Ubuntu的一些对比,从理论与实际角度出发,
阐述比较常用的命令,除此之外,也阐述了一些实践的用法,例如:如何配置静态IP,如何抓包等问题,
欢迎大家与我共同探讨交流。
1.基本工具

net-tools起源于BSD的TCP/IP工具箱,后来成为老版本Linux内核中配置网络功能的工具。
但自2001年起,Linux社区已经对其停止维护。
同时,一些Linux发行版比如Arch Linux和CentOS/RHEL 7则已经完全抛弃了net-tools,
只支持iproute2。作为网络配置工具的一份子,iproute2的出现旨在从功能上取代net-tools。
net-tools通过procfs(/proc)和ioctl系统调用去访问和改变内核网络配置,
而iproute2则通过netlink套接字接口与内核通讯。抛开性能而言,iproute2的用户接口比net-tools显得更加直观。
net-tools与iproute2
这里使用ubuntu介绍,安装:
sudo apt-get install net-tools iproute2
net-tools
ifconfig
route
netstat
iproute2
ip
ss
2.网络查看与配置

2.1 网络查看

net-tools
现在系统,例如:CentOS7使用了一致性网络设备命名,以下不匹配则使用eth0显示。
eth0表示第一块网卡(网络接口)。
eno1 板载网卡
ens33 PCI-E网卡
enp0s3 无法获取物理信息的PCI-E网卡
那么如何完成网络接口命名修改呢?
网络命名规则受biosdevname与net.ifnames两个参数影响。

biosdevname	net.ifnames	网卡名
默认	0	1	enp59s0
组合1	1	0	em1
组合2	0	0	eth0
1.编辑/etc/default/grub
sudo vi /etc/default/grub
内容修改如下:
GRUB_CMDLINE_LINUX="biosdevname=0 net.ifnames=0"
2、重新生成GRUB的启动菜单配置文件(/boot/grub/grub.cfg)
sudo update-grub
3、重启系统后,网卡名称变成eth0和wlan0
重启输入ifconfig
eth0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
查看网卡物理连接情况
miil-tool eth0
可以查看是否与网线连接。
iproute2
ip addr ls
2.2 网络配置

net-tools
ifconfig <接口> <IP地址> [netmask 子网掩码]
ifup <接口>
ifdown <接口>
实践:
root@city:~# sudo ifconfig enp59s0 172.23.2.1 netmask 255.255.0.0
输出:
root@city:~# ifconfig enp59s0
enp59s0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.23.2.1  netmask 255.255.0.0  broadcast 172.23.255.255
        ether a4:4d:c8:2f:1a:82  txqueuelen 1000  (以太网)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

iproute2
ip addr eth1 10.0.0.1 netmask 255.255.255.0
ip link set dev eth0 up
ip link set dev eth0 down
3.网关查看及配置

3.1 查看网关(或者路由)

net-tools
route -n
由于默认情况下ip会反解成域名,所以使用-n参数不解析主机名。
iproute2
ip addr ls
3.2 网关配置

net-tools
route add default gw <网关ip>
route add -host <指定ip> gw <网关ip>
route add -net <指定网段> netmask <子网掩码> gw <网关ip>
删除就是换成del。
实践:
(1)现在想把下面网关192.168.1.1修改成192.168.1.2,修改之前需要先删除,所以如下:
(base) root@city:~# route -n
内核 IP 路由表
目标            网关            子网掩码        标志  跃点   引用  使用 接口
0.0.0.0         192.168.1.1     0.0.0.0         UG    600    0        0 wlp60s0
删除:
route del default gw 192.168.1.1
添加:
 route add default gw 192.168.1.1
输出:
(base) root@city:~# route -n
内核 IP 路由表
目标            网关            子网掩码        标志  跃点   引用  使用 接口
0.0.0.0         192.168.1.2     0.0.0.0         UG    600    0        0 wlp60s0
(2)现在想访问目标主机ip为10.0.0.1,走192.168.1.3网关。
(base) root@city:~# route add -host 10.0.0.1 gw 192.168.1.3
(base) root@city:~# route -n
内核 IP 路由表
目标            网关            子网掩码        标志  跃点   引用  使用 接口
10.0.0.1        192.168.1.3     255.255.255.255 UGH   0      0        0 wlp60s0
(3)设置网段的明细路由
(base) root@city:~# route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.1.4
(base) root@city:~# route -n
内核 IP 路由表
目标            网关            子网掩码        标志  跃点   引用  使用 接口
192.168.3.0     192.168.1.4     255.255.255.0   UG    0      0        0 wlp60s0
iproute2
(base) root@city:~# ip route add 10.0.0.2/32 via 192.168.1.7
(base) root@city:~#
(base) root@city:~# route -n
内核 IP 路由表
目标            网关            子网掩码        标志  跃点   引用  使用 接口
10.0.0.2        192.168.1.7     255.255.255.255 UGH   0      0        0 wlp60s0
等价于上述的最后一种:
route add -net 10.0.0.2 netmask 255.255.255.255 gw 192.168.1.7
4.网络故障排除

ping
traceroute
mtr
nslookup
dig
telnet
tcpdump
netstat
ss
(1)ping
测试是否可以上网,例如:
(base) light@city:~$ ping www.baidu.com
PING www.a.shifen.com (39.156.66.18) 56(84) bytes of data.
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=1 ttl=53 time=26.7 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=2 ttl=53 time=27.0 ms
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 26.745/26.907/27.070/0.230 ms
(2)traceroute
安装:
sudo apt-get install traceroute
使用traceroute显示追踪中间路由:
(base) light@city:~$ traceroute -w 1 www.baidu.com
traceroute to www.baidu.com (39.156.66.14), 30 hops max, 60 byte packets
 1  * * *
 2  * * *
 3  * * *
 4  * * *
 5  * * *
 6  * * *
上面*表示不显示。
(3)mtr
比traceroute更丰富的显示。
(4)nslookup
解析域名,例如解析百度。
[root@VM_0_17_centos ~]# nslookup www.baidu.com
Server:183.60.83.19
Address:183.60.83.19#53

Non-authoritative answer:
www.baidu.comcanonical name = www.a.shifen.com.
Name:www.a.shifen.com
Address: 182.61.200.6
Name:www.a.shifen.com
Address: 182.61.200.7
(5)dig
DNS信息查询
dig www.baidu.com
(6)telnet
测试远端端口是否通畅:
(base) light@city:~$ telnet www.baidu.com 80
Trying 61.135.169.121...
Connected to www.a.shifen.com.
Escape character is '^]'.
显示如上信息表示通畅。
(7)tcpdump
能ping通了,端口也通畅了,如何抓取数据包呢?
-i 指定网卡 any表示所有网卡
-n 表示以ip显示
-w 保存
-r 读取
例如:抓取所有网卡访80端口的包,以ip显示。
tcpdump -i any -n port 80
抓取所有网卡访问主机ip为61.135.169.125的tcp包,以ip显示。
tcpdump -i any -n host 61.135.169.125
上述两者结合,抓取所有网卡访问指定ip与端口。
tcpdump -i any -n host 61.135.169.125 and port 80
如果想对上述的数据进行保存,如下:
tcpdump -i any -n host 61.135.169.125 and port 80 -w /tmp/tcp.cap
想读取数据,可以通过:
tcpdump -r /tmp/tcp.cap
(8)netstat
检测对外发布的服务监听地址端口问题。
-n 同tcpdump以ip显示
-t tcp数据包
-p 进程
-l listen
-r 路由
如:
netstat -ntpl
(9)ss
使用同netstat,如:
ss -ntpl
(10)host
root@city:/home/light# host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 61.135.169.121
www.a.shifen.com has address 61.135.169.125
查找主机名到IP或IP在IPv4或IPv6来命名。
-t参数,可以查询DNS记录。
root@city:/home/light# host -t CNAME www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
5.网络服务管理

5.1 概论

网络服务管理程序分为两种:
SysV
service network start|stop|restart
systemd
systemctl start|stop|restart net NetworkManager
网络配置文件:
/etc/network/interfaces(Ubuntu系统),ifcfg-eth0(Centos系统)
/etc/host
5.2 实践

我用的系统是Ubuntu18.04,里面有两套网络服务管理程序,分别是networking与NetworkManager。
为了避免操作失误,禁用一个,在ubuntu上可以用sysv-rc-conf
编辑/etc/apt/sources.list文件,末尾添加:
deb http://archive.ubuntu.com/ubuntu/ trusty main universe restricted multiverse
更新源及安装:
sudo apt-get update
sudo apt-get install sysv-rc-conf
在Centos上使用chkconfig。
我们查看一下:
light@city:~$ sudo sysv-rc-conf --list networking
networking   0:off5:off6:offS:on
其中S是on,我们把它关闭即可。
light@city:~$ sudo sysv-rc-conf --level S networking off
light@city:~$ sudo sysv-rc-conf --list networking
networking   0:off5:off6:offS:off
5.3 主机名

临时修改主机名:
light@city:~$ hostname xxx
永久修改主机名:
light@city:~$ hostnamectl set-hostname xxx
将主机名写进hosts文件,写入:
127.0.0.1 xxx
5.4 静态IP配置方式1

修改interfaces配置文件
auto enp0s25
iface enp0s25 inet static
address 192.168.8.88
netmask 255.255.240.0
gateway 192.168.8.1
dns-nameservers 8.8.8.8
重启网络:
sudo systemctl restart NetworkManager
重启电脑。
5.5 静态IP配置方式2

ubuntu 17之后引入的新方式 netplan。
网卡信息配置在 /etc/netplan/01-network-manager-all.yaml 文件中,
如果这个 yaml 文件不存在(发型版本中没有默认创建),那么可以使用一下的命令创建出来。
创建出来的名字可能略有不同,好在 /etc/netplan/ 这个目录下面所有的 yaml 文件都可以生效。
 sudo netplan generate
然后编辑:
vim /etc/netplan/01-network-manager-all.yaml
进行相应配置:
network:
  version: 2
  renderer: NetworkManager
  ethernets:
     ens33: #配置的网卡名称,使用ifconfig -a查看得到,且必须是空格缩进,netplan只认空格
       dhcp4: no #no-dhcp4开启 true-dhcp4开启
       dhcp6: true #true-dhcp6开启 no-dhcp6关闭
       addresses: [192.168.2.110/24, ] #设置本机IP及掩码,这个逗号和空格好像不能少,少了就不生效,后面的空格之后可以写入IPv6的地址,从而变成这样[192.168.2.110/24, "2001:1::1/64"]
       gateway4: 192.168.2.1 #设置ipv4的默认网关
       nameservers:  #设置DNS服务器
         addresses: [8.8.8.8,8.8.8.4]  #多个DNS服务器之间用逗号隔开
使netplan配置生效
sudo netplan apply
此时即可生效。

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值