linux命令--ifconfig

一、概念
ifconfig全称interface configuration, 此命令用来查看或者修改网络接口的配置。

Overview

ifconfig没有任何参数,显示当前所有生效的网络接口的信息,运行结果如下:

eth0      Link encap:Ethernet  HWaddr 09:00:12:90:e3:e5  
          inet addr:192.168.1.29 Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe70:e3f5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:54071 errors:1 dropped:0 overruns:0 frame:0
          TX packets:48515 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:22009423 (20.9 MiB)  TX bytes:25690847 (24.5 MiB)
          Interrupt:10 Base address:0xd020 

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:83 errors:0 dropped:0 overruns:0 frame:0
          TX packets:83 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:7766 (7.5 KiB)  TX bytes:7766 (7.5 KiB)

wlan0     Link encap:Ethernet  HWaddr 58:a2:c2:93:27:36  
          inet addr:192.168.1.64  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::6aa3:c4ff:fe93:4746/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:436968 errors:0 dropped:0 overruns:0 frame:0
          TX packets:364103 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:115886055 (110.5 MiB)  TX bytes:83286188 (79.4 MiB)

eth0, lo和wlan0是系统中激活的网络接口名称。
eth0是第一个以太网接口(添加的其他以太网接口名称会是eth1、eth2等),这种类型的接口是网络接口卡通过5类电缆连接到网络。
lo是一个环回接口,这是系统和自己通讯的一个特殊网络接口。
wlan0是系统第一个无线网络接口名称,其他无线网络接口会被命名为wlan1、wlan2等。

Viewing The Configuration Of All Interfaces

如果你想查看系统所有网络接口的配置(不只是当前已经被激活的),你可以指定参数-a. 如下:

ifconfig -a

这个命令的输出和只运行ifconfig很相似,但是如果系统中有任何没被激活的网络接口,他们的配置信息也会被输出。

Viewing The Configuration Of A Specific Interface
查看明确的网络接口配置信息

ifconfig eth0

Enabling And Disabling An Interface
网络接口是激活的, 可以发送和接受数据,否则不行。
可以通过ifconfig把网络接口的状态由非活跃改成活跃。
启用和禁用网络接口状态,需要超级用户权限(root用户登录或者运行命令添加sudo)。

启用eth1:

sudo ifconfig eth1 up

禁用wlan0:

sudo ifconfig wlan0 down

Configuring An Interface

分配一个静态IP地址给网络接口,明确接口名字和IP地址。比如,给wlan0分配IP地址69.72.169.1:

sudo ifconfig wlan0 69.72.169.1

分配网络掩码给网络接口,使用关键字netmask和掩码地址。比如:给eth1用掩码255.255.255.0:

sudo ifconfig eth1 netmask 255.255.255.0

分配广播地址给网络接口,使用关键字broadcast和广播地址。比如:
配置wlan1使用广播地址172.16.25.98:

sudo ifconfig wlan1 broadcast 172.16.25.98

当然上面的关键字可以混合使用。

二、语法

    ifconfig [-v] [-a] [-s] [interface]
    ifconfig [-v] interface [aftype] options | address ...

Options
-a
Display information for all network interfaces, even if they are down.

-s
Display a short list in a format identical to the command “netstat -i”.

-v
Verbose mode; display additional information for certain error conditions.

interface
The name of the interface. This is usually a driver name followed by a unit number, for example “eth0” for the first Ethernet interface. If your kernel supports alias interfaces, you can specify them with eth0:0 for the first alias of eth0. You can use them to assign a second address. To delete an alias interface use ifconfig eth0:0 down. Note: for every scope (i.e. same net with address/netmask combination) all aliases are deleted, if you delete the first (primary).

up
This flag causes the interface to be activated. It is implicitly specified if an address is assigned to the interface.

down
This flag causes the driver for this interface to be shut down.

[-]arp
Enable (or disable, if the “-” prefix is specified) the use of the ARP protocol on this interface.

[-]promisc
Enable (or disable, if the “-” prefix is specified) the promiscuous mode of the interface. If promiscuous mode is enabled, all packets on the network will be received by the interface.

[-]allmulti
Enable or disable all-multicast mode. If multicast mode is enabled, all multicast packets on the network will be received by the interface.

metric N
This parameter sets the interface metric, which is used by the interface to make routing decisions. N must be an integer between 0 and 4294967295. If you’re not sure what a network metric is, or whether you should change it, you can safely leave this setting alone.

mtu N
This parameter sets the Maximum Transfer Unit (MTU) of an interface. This setting is used to limit the maximum packet size transferred by the interface. If you’re not sure about it, you can safely leave this setting alone.

dstaddr address
Set the remote IP address for a point-to-point link (such as PPP). This keyword is now obsolete; use the pointopoint keyword instead.

netmask address
Set the IP network mask for this interface. This value defaults to the usual class A, B or C network mask (as derived from the interface IP address), but it can be set to any value.

add address/prefixlen
Add an IPv6 address to an interface.

del address/prefixlen
Remove an IPv6 address from an interface.

tunnel aa.bb.cc.dd
Create a new SIT (IPv6-in-IPv4) device, tunnelling to the given destination.

irq address
Set the interrupt line used by this device. Not all devices can dynamically change their IRQ setting.

io_addr address
Set the start address in I/O space for this device.
mem_start address Set the start address for shared memory used by this device. Only a few devices need this.

media type
Set the physical port or medium type to be used by the device. Not all devices can change this setting, and those that can vary in what values they support. Typical values for type are 10base2 (thin Ethernet), 10baseT (twisted-pair 10Mbps Ethernet), AUI (external transceiver) and so on. The special medium type of auto can be used to tell the driver to auto-sense the media. Again, not all drivers can do this.

[-]broadcast [address]
If the address argument is given, this will set the protocol broadcast address for this interface. Otherwise, it will set (or clear, if the “-” prefix is used) the IFF_BROADCAST flag for the interface.

[-]pointopoint [address]
This keyword enables the point-to-point mode of an interface, meaning that it is a direct link between two machines with nobody else listening on it. If the address argument is also given, set the protocol address of the other side of the link, just like the obsolete dstaddr keyword does. Otherwise, set or clear the IFF_POINTOPOINT flag for the interface.

hw class address
Set the hardware address of this interface, if the device driver supports this operation. The keyword must be followed by the name of the hardware class and the printable ASCII equivalent of the hardware address. Hardware classes currently supported include ether (Ethernet), ax25 (AMPR AX.25), ARCnet and netrom (AMPR NET/ROM).

multicast
Set the multicast flag on the interface. This should not normally be needed as the drivers set the flag correctly themselves.

address
The IP address to be assigned to this interface.
txqueuelen length Set the length of the transmit queue of the device. It is useful to set this to small values for slower devices with a high latency (such as a connection over a modem, or over ISDN) to prevent fast bulk transfers from disturbing interactive traffic like telnet too much.

三、相关命令
arp — 操作高速缓存。
dhclient — 从DHCP服务器获取动态IP。
ifdown — 禁用网络接口。
ifquery — 解析网络接口的信息。
ifup — 启用网络接口。
ip — 显示和操作路由、设备、路由机制和通道等信息。
iwconfig — 配置无线网络接口。
ping —发送请求回显包给网络主机。
netstat — 打印网络连接、路由表、网络统计、多播等信息。
route — 显示和操作IP路由表。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值