许多 windows 用户非常熟悉 ipconfig 命令,它被用来获取网络配置信息并对此进行修改。Linux 系统拥有一个类似的工具,也就是 ifconfig(interfaces config) 。通常需要以 root 身份登录或使用 sudo 以便在 Linux 机器上使用 ifconfig 工具。依赖于 ifconfig 命令中使用一些选项属性,ifconfig 工具不仅可以被用来简单地获取网络接口配置信息,还可以修改这些配置。
命令格式 - ifconfig [网络设备] [参数]
命令功能 - 查看或配置网络设备。当网络环境发生改变时可通过此命令对网络进行相应的配置。
命令参数 -
netstat -i)。命令实例 -
实例一:显示网络设备信息(激活状态的)
命令
xxxxxxxxxx11ifconfig 输出
xxxxxxxxxx161[root@localhost ~]# ifconfig2eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:20 3 inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.04 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:15 RX packets:8700857 errors:0 dropped:0 overruns:0 frame:06 TX packets:31533 errors:0 dropped:0 overruns:0 carrier:07 collisions:0 txqueuelen:1000 8 RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB)910lo Link encap:Local Loopback 11 inet addr:127.0.0.1 Mask:255.0.0.012 UP LOOPBACK RUNNING MTU:16436 Metric:113 RX packets:68 errors:0 dropped:0 overruns:0 frame:014 TX packets:68 errors:0 dropped:0 overruns:0 carrier:015 collisions:0 txqueuelen:0 16 RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB)说明
eth0 表示第一块网卡, 其中 HWaddr 表示网卡的物理地址,可以看到目前这个网卡的物理地址(MAC地址)是 00:50:56:BF:26:20
inet addr 用来表示网卡的IP地址,此网卡的 IP地址是 192.168.120.204,广播地址, Bcast:192.168.120.255,掩码地址Mask:255.255.255.0
lo 是表示主机的回坏地址,这个一般是用来测试一个网络程序,但又不想让局域网或外网的用户能够查看,只能在此台主机上运行和查看所用的网络接口。比如把 HTTPD 服务器的指定到回坏地址,在浏览器输入 127.0.0.1 就能看到你所架WEB网站了。但只是您能看得到,局域网的其它主机或用户无从知道。
第一行:连接类型:Ethernet(以太网)HWaddr(硬件mac地址)
第二行:网卡的IP地址、子网、掩码
第三行:UP(代表网卡开启状态)RUNNING(代表网卡的网线被接上)MULTICAST(支持组播)MTU:1500(最大传输单元):1500字节
第四、五行:接收、发送数据包情况统计
第七行:接收、发送数据字节数统计信息。
实例二:启动、关闭指定网卡
命令
xxxxxxxxxx21ifconfig eth0 up2ifconfig eth0 down输出
说明
ifconfig eth0 up 为启动网卡eth0 ;
ifconfig eth0 down 为关闭网卡eth0;
ssh登陆linux服务器操作要小心,关闭了就不能开启了,除非你有多网卡。
实例三:为网卡配置、删除 IPv6 地址
命令
xxxxxxxxxx21ifconfig eth0 add 33ffe:3240:800:1005::2/642ifconfig eth0 del 33ffe:3240:800:1005::2/64输出
说明
ifconfig eth0 add 33ffe:3240:800:1005::2/64 为网卡eth0配置IPv6地址;
ifconfig eth0 add 33ffe:3240:800:1005::2/64 为网卡eth0删除IPv6地址;
练习的时候,ssh登陆linux服务器操作要小心,关闭了就不能开启了,除非你有多网卡。
实例四:用 ifconfig 修改 MAC 地址
命令
xxxxxxxxxx11ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE输出
xxxxxxxxxx371[root@localhost ~]# ifconfig eth0 down //关闭网卡2[root@localhost ~]# ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE //修改MAC地址3[root@localhost ~]# ifconfig eth0 up //启动网卡4[root@localhost ~]# ifconfig5eth0 Link encap:Ethernet HWaddr 00:AA:BB:CC:DD:EE 6 inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.07 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:18 RX packets:8700857 errors:0 dropped:0 overruns:0 frame:09 TX packets:31533 errors:0 dropped:0 overruns:0 carrier:010 collisions:0 txqueuelen:1000 11 RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB)1213lo Link encap:Local Loopback 14 inet addr:127.0.0.1 Mask:255.0.0.015 UP LOOPBACK RUNNING MTU:16436 Metric:116 RX packets:68 errors:0 dropped:0 overruns:0 frame:017 TX packets:68 errors:0 dropped:0 overruns:0 carrier:018 collisions:0 txqueuelen:0 19 RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB)20[root@localhost ~]# ifconfig eth0 hw ether 00:50:56:BF:26:20 //关闭网卡并修改MAC地址 21[root@localhost ~]# ifconfig eth0 up //启动网卡22[root@localhost ~]# ifconfig23eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:20 24 inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.025 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:126 RX packets:8700857 errors:0 dropped:0 overruns:0 frame:027 TX packets:31533 errors:0 dropped:0 overruns:0 carrier:028 collisions:0 txqueuelen:1000 29 RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB)3031lo Link encap:Local Loopback 32 inet addr:127.0.0.1 Mask:255.0.0.033 UP LOOPBACK RUNNING MTU:16436 Metric:134 RX packets:68 errors:0 dropped:0 overruns:0 frame:035 TX packets:68 errors:0 dropped:0 overruns:0 carrier:036 collisions:0 txqueuelen:0 37 RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB)说明
实例五:配置 IP 地址
命令
输出
xxxxxxxxxx31[root@localhost ~]# ifconfig eth0 192.168.120.56 2[root@localhost ~]# ifconfig eth0 192.168.120.56 netmask 255.255.255.0 3[root@localhost ~]# ifconfig eth0 192.168.120.56 netmask 255.255.255.0 broadcast 192.168.120.255说明
ifconfig eth0 192.168.120.56
给eth0网卡配置IP地:192.168.120.56
ifconfig eth0 192.168.120.56 netmask 255.255.255.0
给eth0网卡配置IP地址:192.168.120.56 ,并加上子掩码:255.255.255.0
ifconfig eth0 192.168.120.56 netmask 255.255.255.0 broadcast 192.168.120.255
给eth0网卡配置IP地址:192.168.120.56,加上子掩码:255.255.255.0,加上个广播地址: 192.168.120.255
实例六:启用和关闭 ARP 协议
命令
xxxxxxxxxx21ifconfig eth0 arp2ifconfig eth0 -arp输出
xxxxxxxxxx21[root@localhost ~]# ifconfig eth0 arp 2[root@localhost ~]# ifconfig eth0 -arp说明
ifconfig eth0 arp 开启网卡eth0 的arp协议;
ifconfig eth0 -arp 关闭网卡eth0 的arp协议;
实例七:设置最大传输单元
命令
xxxxxxxxxx11ifconfig eth0 mtu 1500输出
xxxxxxxxxx371[root@localhost ~]# ifconfig eth0 mtu 14802[root@localhost ~]# ifconfig3eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:1F 4 inet addr:192.168.120.203 Bcast:192.168.120.255 Mask:255.255.255.05 UP BROADCAST RUNNING MULTICAST MTU:1480 Metric:16 RX packets:8712395 errors:0 dropped:0 overruns:0 frame:07 TX packets:36631 errors:0 dropped:0 overruns:0 carrier:08 collisions:0 txqueuelen:1000 9 RX bytes:597062089 (569.4 MiB) TX bytes:2643973 (2.5 MiB)1011lo Link encap:Local Loopback 12 inet addr:127.0.0.1 Mask:255.0.0.013 UP LOOPBACK RUNNING MTU:16436 Metric:114 RX packets:9973 errors:0 dropped:0 overruns:0 frame:015 TX packets:9973 errors:0 dropped:0 overruns:0 carrier:016 collisions:0 txqueuelen:0 17 RX bytes:518096 (505.9 KiB) TX bytes:518096 (505.9 KiB)1819[root@localhost ~]# ifconfig eth0 mtu 150020[root@localhost ~]# ifconfig21eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:1F 22 inet addr:192.168.120.203 Bcast:192.168.120.255 Mask:255.255.255.023 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:124 RX packets:8712548 errors:0 dropped:0 overruns:0 frame:025 TX packets:36685 errors:0 dropped:0 overruns:0 carrier:026 collisions:0 txqueuelen:1000 27 RX bytes:597072333 (569.4 MiB) TX bytes:2650581 (2.5 MiB)2829lo Link encap:Local Loopback 30 inet addr:127.0.0.1 Mask:255.0.0.031 UP LOOPBACK RUNNING MTU:16436 Metric:132 RX packets:9973 errors:0 dropped:0 overruns:0 frame:033 TX packets:9973 errors:0 dropped:0 overruns:0 carrier:034 collisions:0 txqueuelen:0 35 RX bytes:518096 (505.9 KiB) TX bytes:518096 (505.9 KiB)3637[root@localhost ~]#说明
设置能通过的最大数据包大小为 1500 bytes。
备注
用 ifconfig 命令配置网卡信息,在网卡重启后/机器重启后,配置就不存在了。
想要将上述的配置永久的存在电脑里,就要修改网卡的配置文件了。