linux常用命令(40):ifconfig 命令

ifconfig命令

      ifconfig命令ifconfig(interfaces config)用于配置和显示Linux内核中网络接口的网络参数。与windows中的ipconfig命令相似。通常需以root身份登录或使用sudo以便Linux机器上使用ifconfig工具。依赖于ifconfig命令中使用些选项属性,ifconfig工具不仅可以被用来简单地获取网络接口配置信息,还可以修改这些配置。

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

语法

ifconfig(参数)    

参数

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地址; 
网络设备:指定网络设备的名称。

常用实例

实例一:显示网络设备信息激活状态的

命令:

ifconfig

输出:

复制代码
[root@localhost ~] #  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
          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
          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 表示第一块网卡, 其中 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字节

第四、五行:接收、发送数据包情况统计

第七行:接收、发送数据字节数统计信息。

实例二:启动关闭指定网卡

命令:

ifconfig eth0 up

ifconfig eth0 down

输出:

说明:

ifconfig eth0 up 为启动网卡eth0 ifconfig eth0 down 为关闭网卡eth0ssh登陆linux服务器操作要小心,关闭了就不能开启了,除非你有多网卡。

实例三:为网卡配置和删除IPv6地址

命令:

ifconfig eth0 add 33ffe:3240:800:1005::2/64

ifconfig 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地址

命令:

ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE

输出:

复制代码
[root@localhost ~] #  ifconfig eth0 down //关闭网卡
[root@localhost ~] #  ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE //修改MAC地址
[root@localhost ~] #  ifconfig eth0 up //启动网卡
[root@localhost ~] #  ifconfig
eth0      Link encap:Ethernet  HWaddr 00:AA:BB:CC:DD:EE  
          inet addr:192.168.120.204  Bcast:192.168.120.255  Mask:255.255.255.0
          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
          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)
[root@localhost ~] #  ifconfig eth0 hw ether 00:50:56:BF:26:20 //关闭网卡并修改MAC地址 
[root@localhost ~] #  ifconfig eth0 up //启动网卡
[root@localhost ~] #  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
          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
          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)  
复制代码

说明:

 

实例五:配置IP地址

命令:

输出:

[root@localhost ~] #  ifconfig eth0 192.168.120.56 
[root@localhost ~] #  ifconfig eth0 192.168.120.56 netmask 255.255.255.0 
[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协议

命令:

ifconfig eth0 arp

ifconfig eth0 -arp

输出:

[root@localhost ~] #  ifconfig eth0 arp 
[root@localhost ~] #  ifconfig eth0 -arp

 

说明:

ifconfig eth0 arp 开启网卡eth0 arp协议;

ifconfig eth0 -arp 关闭网卡eth0 arp协议;

 

实例七:设置最大传输单元

命令:

ifconfig eth0 mtu 1500

输出:

复制代码
[root@localhost ~] #  ifconfig eth0 mtu 1480
[root@localhost ~] #  ifconfig
eth0      Link encap:Ethernet  HWaddr 00:50:56:BF:26:1F  
          inet addr:192.168.120.203  Bcast:192.168.120.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1480  Metric:1
          RX packets:8712395 errors:0 dropped:0 overruns:0 frame:0
          TX packets:36631 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:597062089 (569.4 MiB)  TX bytes:2643973 (2.5 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:9973 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9973 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:518096 (505.9 KiB)  TX bytes:518096 (505.9 KiB)

[root@localhost ~] #  ifconfig eth0 mtu 1500
[root@localhost ~] #  ifconfig
eth0      Link encap:Ethernet  HWaddr 00:50:56:BF:26:1F  
          inet addr:192.168.120.203  Bcast:192.168.120.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8712548 errors:0 dropped:0 overruns:0 frame:0
          TX packets:36685 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:597072333 (569.4 MiB)  TX bytes:2650581 (2.5 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:9973 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9973 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:518096 (505.9 KiB)  TX bytes:518096 (505.9 KiB)

[root@localhost ~] #  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值