在windows系统中,ipconfig命令行工具被用来获取网络接口配置信息并对此进行修改。Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config)。
(1)用法:
用法: ifconfig [网络设备] [参数]
(2)功能:
功能: ifconfig命令用来查看和配置网络设备,当网络环境发生改变时可通过此命令对网络进行相应的配置。
注意: 用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在。要想将上述的配置信息永远的存在电脑里,那就要修改网卡的配置文件了。
(3)选项参数:
1) up 启动指定网络设备/网卡。
2) down 关闭指定网络设备/网卡。该参数可以有效地阻止通过指定接口的IP信息流,如果想永久地关闭一个接口,我们还需要从核心路由表中将该接口的路由信息全部删除。
3) -a 无论是否激活,显示所有配置的网络接口。
4) add 给指定网卡配置IPv6地址
5) del 删除指定网卡的IPv6地址
6) arp|-arp 打开或关闭支持ARP协议
7) mtu<字节数> 设置网卡的最大传输单元
8) netmask<子网掩码> 设置网卡的子网掩码
(4)实例:
1)[sunjimeng@localhost ~]$ ifconfig
[sunjimeng@localhost ~]$ ifconfig //未联网时的参数 eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:4d:a3:cc txqueuelen 1000 (Ethernet) RX packets 1914 bytes 174936 (170.8 KiB) RX errors 0 dropped 36 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) 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 3246 bytes 281472 (274.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3246 bytes 281472 (274.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[sunjimeng@localhost ~]$ ifconfig //连接到有线网时的参数 eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 //mtu表示最大传输单元 inet 192.168.117.128 netmask 255.255.255.0 broadcast 192.168.117.255 inet6 fe80::20c:29ff:fe4d:a3cc prefixlen 64 scopeid 0x20<link> ether 00:0c:29:4d:a3:cc txqueuelen 1000 (Ethernet) RX packets 1969 bytes 181974 (177.7 KiB) RX errors 0 dropped 36 overruns 0 frame 0 TX packets 48 bytes 6324 (6.1 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 3590 bytes 310452 (303.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3590 bytes 310452 (303.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
说明:
1)etn(Number)指的是网卡,可以看到目前这个网卡的物理地址(MAC地址)是 00:0c:29:4d:a3:cc。
2)inet后表示ip地址,此网卡的ip地址是192.168.117.128,广播地址是192.168.117.255,掩码地址为255.255.255.0。
3)lo是表示主机的回坏地址,这个一般是用来测试一个网络程序,但又不想让局域网或外网的用户能够查看,只能在此台主机上运行和查看所用的网络接口。
比如把 http服务器指定到回环地址,在浏览器输入127.0.0.1就能看到你所架WEB网站了。但只是您能看得到,局域网的其它主机或用户无从知道。
2)[root@localhost sunjimeng]# ifconfig eno16777736 down 打开和关闭指定网卡
[sunjimeng@localhost ~]$ ifconfig eno16777736 down //必须是root权限才可以 SIOCSIFFLAGS: 不允许的操作 [sunjimeng@localhost ~]$ su root 密码: [root@localhost sunjimeng]# ifconfig eno16777736 down [root@localhost sunjimeng]# ifconfig 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 3598 bytes 311224 (303.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3598 bytes 311224 (303.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost sunjimeng]# ifconfig eno16777736 up [root@localhost sunjimeng]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:4d:a3:cc txqueuelen 1000 (Ethernet) RX packets 2013 bytes 185705 (181.3 KiB) RX errors 0 dropped 36 overruns 0 frame 0 TX packets 88 bytes 9726 (9.4 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 3602 bytes 311520 (304.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3602 bytes 311520 (304.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
注意:ssh登陆linux服务器,关闭了网卡就不能开启了,除非你有多网卡。
3)[root@localhost sunjimeng]# ifconfig -a 无论是否激活,显示所有配置的网络接口,不带参数的只显示已激活的网络接口
[root@localhost sunjimeng]# ifconfig -a eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:4d:a3:cc txqueuelen 1000 (Ethernet) RX packets 2016 bytes 185981 (181.6 KiB) RX errors 0 dropped 36 overruns 0 frame 0 TX packets 88 bytes 9726 (9.4 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 3602 bytes 311520 (304.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3602 bytes 311520 (304.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
4)启用或关闭支持ARP协议
[root@localhost sunjimeng]# ifconfig eno16777736 arp
[root@localhost sunjimeng]# ifconfig eno16777736 -arp
5)[root@localhost sunjimeng]# ifconfig eno16777736 mtu 2000 设置网络设备传输的最大单元
[root@localhost sunjimeng]# ifconfig eno16777736: flags=4291<UP,BROADCAST,RUNNING,NOARP,MULTICAST> mtu 1500 inet 192.168.117.128 netmask 255.255.255.0 broadcast 192.168.117.255 inet6 fe80::20c:29ff:fe4d:a3cc prefixlen 64 scopeid 0x20<link> ether 00:0c:29:4d:a3:cc txqueuelen 1000 (Ethernet) RX packets 75518 bytes 108412465 (103.3 MiB) RX errors 0 dropped 36 overruns 0 frame 0 TX packets 20662 bytes 1263791 (1.2 MiB) 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 4381 bytes 364888 (356.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4381 bytes 364888 (356.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost sunjimeng]# ifconfig eno16777736 mtu 2000 [root@localhost sunjimeng]# ifconfig eno16777736: flags=4291<UP,BROADCAST,RUNNING,NOARP,MULTICAST> mtu 2000 ether 00:0c:29:4d:a3:cc txqueuelen 1000 (Ethernet) RX packets 99762 bytes 145117330 (138.3 MiB) RX errors 0 dropped 36 overruns 0 frame 0 TX packets 20663 bytes 1263851 (1.2 MiB) 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 5243 bytes 424328 (414.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5243 bytes 424328 (414.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
6)[root@localhost sunjimeng]# ifconfig eno16777736 hw ether 00:0c:29:4d:a3:dd 设置网卡的mac地址
[root@localhost sunjimeng]# ifconfig eno16777736: flags=4291<UP,BROADCAST,RUNNING,NOARP,MULTICAST> mtu 2000 inet 0.0.0.64 netmask 0.0.0.0 broadcast 255.255.255.255 ether 00:0c:29:4d:a3:cc txqueuelen 1000 (Ethernet) RX packets 109425 bytes 159195954 (151.8 MiB) RX errors 0 dropped 36 overruns 0 frame 0 TX packets 20668 bytes 1264151 (1.2 MiB) 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 33371 bytes 2821752 (2.6 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 33371 bytes 2821752 (2.6 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost sunjimeng]# ifconfig eno16777736 hw ether 00:0c:29:4d:a3:dd [root@localhost sunjimeng]# ifconfig eno16777736: flags=4291<UP,BROADCAST,RUNNING,NOARP,MULTICAST> mtu 2000 inet 0.0.0.64 netmask 0.0.0.0 broadcast 255.255.255.255 ether 00:0c:29:4d:a3:dd txqueuelen 1000 (Ethernet) RX packets 109434 bytes 159196494 (151.8 MiB) RX errors 0 dropped 36 overruns 0 frame 0 TX packets 20668 bytes 1264151 (1.2 MiB) 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 34011 bytes 2876296 (2.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 34011 bytes 2876296 (2.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0