今天给伙伴们分享一下Linux 网络管理-网络管理命令详解,希望看了有所收获。
我是公众号「想吃西红柿」「云原生运维实战派」作者,对云原生运维感兴趣,也保持时刻学习,后续会分享工作中用到的运维技术,在运维的路上得到支持和共同进步!
如果伙伴们看了文档觉得有用,欢迎大家关注我的公众号,获取相关文档。爱运维,爱生活。
1、网络环境查看配置
1、ifconfig
- ifconfig命令被用于配置和显示Linux内核中网络接口的网络参数。用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在。要想将上述的配置信息永远的存的电脑里,那就要修改网卡的配置文件了。
1、安装
- 若系统默认没有ifconfig命令,则使用下面命令进行安装。
[root@edenluo.com ~]# yum install net-tools
2、常用参数
up
:启动指定的网络设备;down
:关闭指定的网络设备;mtu <字节>
:设置网络设备的最大传输单元;netmask <子网掩码>
:设置网络设备的子网掩码;broadcast <广播地址>
:设置网络设备的广播地址;
3、应用
1、显示网络设备信息(激活状态的)
[root@edenluo.com ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::e096:3a76:6df1:bd6d prefixlen 64 scopeid 0x20<link>
ether 08:00:27:6b:57:88 txqueuelen 1000 (Ethernet)
RX packets 952 bytes 85854 (83.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 621 bytes 73814 (72.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
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
- 说明
- eth0 表示第一块网卡。
- lo是表示主机的回环地址,这个一般是用来测试一个网络程序,但又不想让局域网或外网的用户能够查看,只能在此台主机上运行和查看所用的网络接口。比如把 httpd服务器的指定到回环地址,在浏览器输入127.0.0.1就能看到你所架WEB网站了。但只是您能看得到,局域网的其它主机或用户无从知道。
字段 | 解释 |
---|---|
UP | 网卡处在开启状态 |
RUNNING | 网卡的网线被接上 |
MULTICAST | 支持组播 |
mtu 1500 | 最大传输单元:1500字节 |
inet | 网卡的IP地址 |
netmask | 掩码地址 |
broadcast | 广播地址 |
RX packets [xx] bytes [xx] | 接收数据包数量、字节数 |
TX packets [xx] bytes [xx] | 发送数据包数量、字节数 |
2、启动关闭指定网卡
[root@edenluo.com ~]# ifconfig eth0 up # 启动网卡eth0
[root@edenluo.com ~]# ifconfig eth0 down # 关闭网卡eth0
- ssh登陆linux服务器操作要小心,关闭了就会断开ssh连接,就不能开启了,除非你有多网卡。
3、启用和关闭 ARP 协议(地址解析协议)
[root@edenluo.com ~]# ifconfig eth0 arp #开启网卡eth0 的arp协议
[root@edenluo.com ~]# ifconfig eth0 -arp #关闭网卡eth0 的arp协议
4、配置IP地址、子网掩码、广播地址,mac地址
# 如果不加任何其他参数,则系统会依照该 IP 所在的 class 范围,自动的计算出 netmask 以及 network, broadcast 等 IP 参数;
[root@edenluo.com ~]# ifconfig eth0 192.168.2.10
[root@edenluo.com ~]# ifconfig eth0 192.168.2.10 netmask 255.255.255.0
[root@edenluo.com ~]# ifconfig eth0 192.168.2.10 netmask 255.255.255.0 broadcast 192.168.2.255
[root@edenluo.com ~]# ifconfig eth0:0 192.168.174.6 netmask 255.255.255.0 up
或者
[root@edenluo.com ~]# ifconfig eth0:0 192.168.174.10/24 up /24代表子网掩码
修改机器的MAC地址信息
[root@edenluo.com ~]# ifconfig eth0 hw ether 00:0c:29:13:10:cf
5、设置最大传输单元
[root@edenluo.com ~]# ifconfig eth0 mtu 1500 #设置能通过的最大数据包大小为 1500 bytes
6、放弃 ifconfig
的全部修改
- 以
ifcfg-eth*
的配置文件重置网络设置
[root@edenluo.com ~]# /etc/init.d/network restart
- ifconfig 所有配置修改功能都只是临时修改,重启网络服务就会失效。
2、ifup 和 ifdown
- 根据 /etc/sysconfig/network-scripts/ifcfg-eth* 配置文件启动和关闭网卡
1、 语法
-
启动网卡
ifup [interface]
-
关闭网卡
ifdown [interface]
2、命令介绍
- ifup 与 ifdown 其实都是 shell 脚本,他会直接到
/etc/sysconfig/network-scripts
目录下查找对应的配置文件,例如ifup eth0
会读取ifcfg-eth0
这个文件的内容,然后加以设置。 - 不过,由于这两个脚本主要是通过读取配置文件 (ifcfg-eth*) 来启动与关闭网络接口,所以在使用前请确定
ifcfg-eth*
是否真的存在于正确的目录内,否则会启动失败。另外,如果以ifconfig eth0 ...
的方式 设定或修改了网路接口后,就无法再以ifdown eth0
的方式来关闭了! 因为ifdown
会分析比对目前的网路参数与ifcfg-eth0
是否相符,不符的话,就会放弃本次动作。因此,使用ifconfig
修改完毕后,应该要用ifconfig eth0 down
才能够关闭该接口。
3、应用
1、查看当前网卡配置
[root@edenluo.com ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::e096:3a76:6df1:bd6d prefixlen 64 scopeid 0x20<link>
ether 08:00:27:6b:57:88 txqueuelen 1000 (Ethernet)
RX packets 1870 bytes 173264 (169.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1110 bytes 143493 (140.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 08:00:27:db:78:8f txqueuelen 1000 (Ethernet)
RX packets 107 bytes 12570 (12.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 17 bytes 1326 (1.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
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
2、关闭网卡
- 使用
ifconfig eth1 down
关闭eth1
网卡
[root@edenluo.com ~]# ifconfig eth1 down
[root@edenluo.com ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::e096:3a76:6df1:bd6d prefixlen 64 scopeid 0x20<link>
ether 08:00:27:6b:57:88 txqueuelen 1000 (Ethernet)
RX packets 1938 bytes 178334 (174.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1145 bytes 146715 (143.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
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
- 使用
ifdown lo
关闭lo
网卡
[root@edenluo.com ~]# ifdown lo
[root@edenluo.com ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::e096:3a76:6df1:bd6d prefixlen 64 scopeid 0x20<link>
ether 08:00:27:6b:57:88 txqueuelen 1000 (Ethernet)
RX packets 2018 bytes 184304 (179.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1186 bytes 150461 (146.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3、开启网卡
- 使用
ifup
开启lo
和eth1
网卡
[root@edenluo.com ~]# ifup lo
[root@edenluo.com ~]# ifup eth1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
[root@edenluo.com ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::e096:3a76:6df1:bd6d prefixlen 64 scopeid 0x20<link>
ether 08:00:27:6b:57:88 txqueuelen 1000 (Ethernet)
RX packets 2083 bytes 189104 (184.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1221 bytes 153755 (150