【Linux常见命令】ifconfig命令:配置与查看网络信息

ifconfig(interfaces config)。通常需要以root身份登录或使用sudo来使用ifconfig工具

ifconfig 命令用来查看和配置网络设备。当网络环境发生改变时可通过此命令对网络进行相应的配置。

备注:用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在。要想将上述的配置信息永远的存的电脑里,那就要修改网卡的配置文件了。


ifconfig - configure a network interface

ifconfig命令用于显示或设置网络设备。

ifconfig可设置网络设备的状态,或是显示目前的设置。

语法:

  ifconfig [interface]
  ifconfig interface [aftype] options | address ...

  ifconfig [网络设备][down up -allmulti -arp -promisc][add<地址>][del<地址>][<hw<网络设备类型><硬件地址>][io_addr<I/O地址>][irq<IRQ地址>][media<网络媒介类型>][mem_start<内存地址>][metric<数目>][mtu<字节>][netmask<子网掩码>][tunnel<地址>][-broadcast<地址>][-pointopoint<地址>][IP地址]

参数说明

  • add<地址> 设置网络设备IPv6的IP地址。
  • del<地址> 删除网络设备IPv6的IP地址。
  • down 关闭指定的网络设备。
  • <hw<网络设备类型><硬件地址> 设置网络设备的类型与硬件地址。
  • io_addr<I/O地址> 设置网络设备的I/O地址。
  • irq<IRQ地址> 设置网络设备的IRQ。
  • media<网络媒介类型> 设置网络设备的媒介类型。
  • mem_start<内存地址> 设置网络设备在主内存所占用的起始地址。
  • metric<数目> 指定在计算数据包的转送次数时,所要加上的数目。
  • mtu<字节> 设置网络设备的MTU。
  • netmask<子网掩码> 设置网络设备的子网掩码。
  • tunnel<地址> 建立IPv4与IPv6之间的隧道通信地址。
  • up 启动指定的网络设备。
  • -broadcast<地址> 将要送往指定地址的数据包当成广播数据包来处理。
  • -pointopoint<地址> 与指定地址的网络设备建立直接连线,此模式具有保密功能。
  • -promisc 关闭或启动指定网络设备的promiscuous模式。
  • [IP地址] 指定网络设备的IP地址。
  • [网络设备] 指定网络设备的名称

实例:

1. 显示网络设备信息

[root@oldboy ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 08:00:27:10:A5:56  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe10:a556/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:590 (590.0 b)  TX bytes:930 (930.0 b)

eth3      Link encap:Ethernet  HWaddr 08:00:27:4C:6F:AD  
          inet addr:192.168.0.109  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe4c:6fad/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5790 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2909 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:565124 (551.8 KiB)  TX bytes:569854 (556.4 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)

2. 启动关闭指定网卡

# ifconfig eth0 down 关闭
# ifconfig eth0 up 启动
# ifconfig eth reload 重启

3. 为网卡配置和删除IPv6地址,临时生效,永久生效需要更改配置文件

# ifconfig eth0 add 33ffe:3240:800:1005::2/ 64 //为网卡eth0配置IPv6地址

# ifconfig eth0 del 33ffe:3240:800:1005::2/ 64 //为网卡eth0删除IPv6地址

4. 用ifconfig修改MAC地址

# ifconfig eth0 down //关闭网卡
# ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE //修改MAC地址
# ifconfig eth0 up //启动网卡
# ifconfig eth1 hw ether 00:1D:1C:1D:1E //关闭网卡并修改MAC地址 
# ifconfig eth1 up //启动网卡

5. 配置IP地址

# ifconfig eth0 192.168.1.56 
//给eth0网卡配置IP地址
# ifconfig eth0 192.168.1.56 netmask 255.255.255.0 
// 给eth0网卡配置IP地址,并加上子掩码
# ifconfig eth0 192.168.1.56 netmask 255.255.255.0 broadcast 192.168.1.255
// 给eth0网卡配置IP地址,加上子掩码,加上个广播地址

6. 启动和关闭ARP协议

# ifconfig eth0 arp  //开启
# ifconfig eth0 -arp  //关闭
# ifconfig eth0:ws arp  //开启
# ifconfig eth0:ws -arp  //关闭
 

7. 设置最大传输单元

# ifconfig eth0 mtu 1500

8. 为网卡配置和删除IPv4地址【临时生效,永久生效需要更改配置文件】

eth2网卡配置文件: /etc/sysconfig/network-scripts/ifcfg-eth2

[root@localhost ~]# ifconfig eth0 192.168.25.166 netmask 255.255.255.0 up
1
[root@localhost ~]# ifconfig eth0 192.168.25.166/24 up       【效果同上】
1
[root@localhost ~]# ip addr add 192.168.25.166/24  dev eth0  【效果同上】
# eth0:ws 别名ip,为eth0网卡添加更多的ipv4地址,ws为别名
# 又称 辅助ip
[root@localhost ~]# ifconfig eth0:ws 192.168.25.166 netmask 255.255.255.0 up 1 [root@localhost ~]# ifconfig eth0:ws 192.168.25.166/24 up 【效果同上】 1 [root@localhost ~]# ip addr add 192.168.25.166/24 dev eth0:ws 【效果同上】
[root@localhost ~]#ifconfig eth0:ws  192.168.25.166 netmask 255.255.255.0 down
1
[root@localhost ~]#ifconfig eth0:ws  192.168.25.166/24 dwon     【效果同上】
1
[root@localhost ~]# ip addr del 192.168.25.166/24  dev eth0:ws  【效果同上】

例如:给eth0添加虚拟ip(别名ip)(临时生效)

ifconfig eth0:0 10.0.2.10 netmask 255.255.255.0 up
[root@oldboy network-scripts]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 08:00:27:10:A5:56  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe10:a556/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3 errors:0 dropped:0 overruns:0 frame:0
          TX packets:33 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1770 (1.7 KiB)  TX bytes:3258 (3.1 KiB)

[root@oldboy network-scripts]# ifconfig eth0:0
eth0:0    Link encap:Ethernet  HWaddr 08:00:27:10:A5:56  
          inet addr:10.0.2.10  Bcast:10.0.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1