Linux命令 — 设置或查看网络配置命令ifconfig

        ifconfig命令用于设置或查看网络配置,包括IP地址、网络掩码、广播地址等。它是linux系统中,使用频率最高的关于网络方面的命令。

1. 命令介绍

        命令格式:

ifconfig 【interface】 【options】address

        主要参数:

interface:网络接口名

up:打开网络接口

down:关闭网络接口

broadcast:设置网络接口的广播地址

netmask:设置网络接口的子网掩码

address:设置网络接口的IPv4地址

add:给指定网络接口配置IPv6地址

del:删除指定网络接口的IPv6地址

        如果加interface,则显示指定网络接口的信息。如果没有任何参数,则显示计算机所有激活网络接口的信息,如下所示:

[root@linux tmp]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:50:56:BF:26:20  
          inet addr:192.168.120.204  Bcast:192.168.120.255  Mask:255.255.255.0
  inet6 addr: fd55:faaf:e1ab:1b01:20c:29ff:fec0:8336/64 Scope:Global
          inet6 addr: fe80::20c:29ff:fec0:8336/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0
          TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:596390239 (568.7 MiB)  TX bytes:2886956 (2.7 MiB)

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:16436  Metric:1
          RX packets:68 errors:0 dropped:0 overruns:0 frame:0
          TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2856 (2.7 KiB)  TX bytes:2856 (2.7 KiB)

        主要指标说明:

eth0/lo:网络接口名

Link encap:网络类型

HWaddr:网卡物理地址

Bcast:广播地址

Mask:子网掩码

inet addr:IPv4地址

inet6 addr:IPv6地址

UP:网络接口处于打开状态

MTU:最大传输单元

Metric:度量值

RX packets/TX packets:接受/传输数据包统计

RX bytes/TX bytes:接受/传输数据字节数统计

        示例输出的解释如下:

        Linux中的网卡名以eth开头,从0开始依次编号,示例中的eth0表示第一块网卡。同时,每个主机都有一个回环网络接口lo用来连接自身,用来测试网络程序,固定的IP地址是127.0.0.1。

        网络接口eth0的类型是Ethernet,物理地址:00:50:56:BF:26:20,IPv4地址:192.168.120.204,广播地址:192.168.120.255,子网掩码:255.255.255.0。IPv6地址分别为fd55:faaf:e1ab:1b01:20c:29ff:fec0:8336和fe80::20c:29ff:fec0:8336。状态是开启状态,最大传输单元是1500,度量值是1。

        回环网络接口lo的类型是Local Loopback,IPv4地址:127.0.0.1,子网掩码:255.0.0.0。IPv6地址为::1,状态是开启状态,最大传输单元是16436,度量值是1。

2. 使用示例

        下面以网络接口eth0为例,介绍一些ifconfig常用的用法。

  • 查看某个网络接口
[root@linux tmp]# ifconfig eth0 查看eth0的信息
  • 配置IP

        一个网络接口上,可以有一个以上IP地址,不仅可以有IPv4地址,还可以有IPv6地址。在添加多个IP的时候,可以为每个IP对应的网络接口取一个别名:eth0:0,eth0:1,...。由于这些IP都在一个网络接口eth0上,所以这些别名都有eth0的前缀。不过在通过ifconfig命令查看网络接口信息的时候,像eth0:0等这些网络接口会跟eth0一样,单独显示配置信息。

  • 配置单个IP
[root@linux tmp]# ifconfig eth0 192.168.120.204 配置eth0的ip
[root@linux tmp]# ifconfig eth0 192.168.120.204 netmask 255.255.255.0 配置eth0的ip,并加子网掩码
[root@linux tmp]# ifconfig eth0 192.168.120.204 netmask 255.255.255.0 broadcast 192.168.120.255 配置eth0的ip,并加子网掩码和广播地址
  • 配置多个IP
[root@linux tmp]# ifconfig eth0:0 192.168.120.203 netmask 255.255.255.0 为eth0额外配置一个IPv4
  • 配置IPv6
[root@linux tmp]# ifconfig eth0 add 33ffe:3240:800:1005::2/64 添加eth0的IPv6地址
[root@linux tmp]# ifconfig eth0 del 33ffe:3240:800:1005::2/64 删除eth0的IPv6地址
  • 配置物理地址
[root@linux tmp]# ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE 配置eth0的MAC地址
  • 网络接口启用
[root@linux tmp]# ifconfig eth0 up 启动网络接口eth0
  • 网络接口禁用
[root@linux tmp]# ifconfig eth0 down 关闭网络接口eth0

注意:

        无需重启计算机,网络设置之后立即生效。用ifconfig命令配置网络接口的参数,机器重启以后就会丢失。如果要使配置参数永远有效,需要修改网卡的配置文件。

Reference

        http://www.cnblogs.com/peida/archive/2013/02/27/2934525.html

        http://blog.csdn.net/ldl22847/article/details/7649113

        http://tech.ccidnet.com/art/302/20071226/1322215_1.html

原文地址:https://www.cnblogs.com/fuhaots2009/p/3455512.html