一点一滴学Linux

    返回首页    发表留言
本文作者:李德强
          第一节 ifconfig命令
 
 

        Linux把以太网卡命名为eth0、eth1、eth2等。分别为第1块网卡、第2块网卡、第3块网卡等。把回环网路loopback简称为lo。执行ifconfig命令(configure a network interface)可以查看本地网卡和回环网路的相关信息。使用ifconfig eth0就是查看eth0这个网卡的信息;使用ifconfig eth1就是查看eth1这个网卡的信息:

ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.7.8  netmask 255.255.0.0  broadcast 172.16.255.255
        inet6 fe80::230:67ff:fec1:cb92  prefixlen 64  scopeid 0x20<link>
        ether 00:30:67:c1:cb:92  txqueuelen 1000  (Ethernet)
        RX packets 1009857  bytes 523114341 (498.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 273790  bytes 33079817 (31.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 1  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 973  bytes 101356 (98.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 973  bytes 101356 (98.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

        可以看到网卡的inet地址、掩码、广播地址、inet6地址及物理地址等等。

        为指定网卡设置IP地址和掩码可以使用下面命令:

ifconfig eth0 172.16.7.218 netmask 255.255.255.0
ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
                inet 172.16.7.218  netmask 255.255.255.0  broadcast 172.16.255.255
        … …

        可以看到IP地址已经被修改为了172.16.7.218,掩码也被修改成了255.255.255.0。通过ifconfig修改的IP地址和掩码,是临时性的,在Linux重启之后就会失效。想要让其永久生效的其中一个办法就是将ifconfig的命令加入到/etc/rc.d/rc.local这个文件中。这是一个开机后自动运行命令脚本的文件,也就是让ifconfig命令在每次开机时运行一次。关于rc.local文件的相关内容在后续章节中还会介绍。

        ifconfig还有一些重要的功能:

ifup eth0:激活网卡。
ifdown eth0:禁用网卡。

 

    返回首页    返回顶部
  看不清?点击刷新

 

  Copyright © 2015-2023 问渠网 辽ICP备15013245号