一、基本用法
ifconfig网卡操作命令,可以用来启动、设置和修改网络配置。
直接使用命令可以查看当前主机所有网卡的信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[ma@localhost ~]$ ifconfig eth0 Link encap:Ethernet HWaddr 20:89:84:49:EF:46 inet addr:192.168.123.58 Bcast:192.168.123.255 Mask:255.255.255.0 inet6 addr: abcd::ff03/120 Scope:Global UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:919 errors:0 dropped:0 overruns:0 frame:0 TX packets:419 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:78144 (76.3 KiB) TX bytes:47656 (46.5 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) |
其中,eth0
是本地网卡信息,lo
是本地环回地址,也可以直接在命令后面加网卡名来查看指定网卡的信息:
1 2 3 4 5 6 7 8 9 |
[ma@localhost ~]$ ifconfig eth0 eth0 Link encap:Ethernet HWaddr 20:89:84:49:EF:46 inet addr:192.168.123.58 Bcast:192.168.123.255 Mask:255.255.255.0 inet6 addr: abcd::ff03/120 Scope:Global UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:949 errors:0 dropped:0 overruns:0 frame:0 TX packets:439 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:80840 (78.9 KiB) TX bytes:50696 (49.5 KiB) |
二、配置网卡
2.1 启动和关闭
启动和停止网卡的命令为:
1 2 |
ifconfig 网卡名 up # 启动网卡 ifconfig 网卡名 down # 关闭网卡 |
例如启动eth0,则输入命令ifconfig eth0 up
即可。
2.2 设置网卡的ip
设置网卡IP可以通过以下方式来实现:
1 2 3 4 |
ifconfig eth0 192.168.10.2 ifconfig eth0 192.168.10.2/24 # ip地址+掩码位数 ifconfig eth0 192.168.10.2 netmask 255.255.255.0 # ip地址+子网地址 ifconfig eth0 192.168.10.2/24 broadcast 192.168.10.255 # 指定广播地址 |
三、多网卡
和windows一样,linux环境下的网卡也可以设置多个ip地址:
1 2 |
ifconfig eth0:1 192.168.10.1 ifconfig eth0:2 192.168.10.2 |
此时,使用ifconfig能看到多块网卡:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
eth0 Link encap:Ethernet HWaddr 20:89:84:49:EF:46 inet addr:192.168.123.58 Bcast:192.168.123.255 Mask:255.255.255.0 inet6 addr: abcd::ff03/120 Scope:Global UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1199 errors:0 dropped:0 overruns:0 frame:0 TX packets:570 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:102345 (99.9 KiB) TX bytes:67010 (65.4 KiB) eth0:1 Link encap:Ethernet HWaddr 20:89:84:49:EF:46 inet addr:192.168.10.1 Bcast:192.168.10.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 eth0:2 Link encap:Ethernet HWaddr 20:89:84:49:EF:46 inet addr:192.168.10.2 Bcast:192.168.10.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) |
四、ipv6地址
设定ipv6地址则需通过指令inet6来完成。例如添加一个ipv6地址:
1 2 |
ifconfig eth0:1 inet6 add abcd::ff10 ifconfig eth0:2 inet6 add abcd::ff11 |
查看网卡信息:
1 2 3 4 |
> ifconfig eth0:1 eth0:1 Link encap:Ethernet HWaddr 20:89:84:49:EF:46 inet addr:192.168.10.1 Bcast:192.168.10.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 |
发现并没有ipv6地址,实际上,ipv6地址信息显示在主网卡上面,要查询eth0才能看到:
1 2 3 4 5 6 7 8 9 10 |
> ifconfig eth0 eth0 Link encap:Ethernet HWaddr 20:89:84:49:EF:46 inet addr:192.168.123.58 Bcast:192.168.123.255 Mask:255.255.255.0 inet6 addr: abcd::ff11/0 Scope:Global inet6 addr: abcd::ff10/0 Scope:Global UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1492 errors:0 dropped:0 overruns:0 frame:0 TX packets:743 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:128419 (125.4 KiB) TX bytes:89202 (87.1 KiB) |
删除ipv6地址:
1 2 |
ifconfig eth0 inet6 del abcd::ff10/0 ifconfig eth0 inet6 del abcd::ff11/0 |
评论