Linux 网络命令 ifconfig、iwconfig、iwlist 详解
iwconfig 是 Linux Wireless Extensions(LWE)的用户层配置工具之一
因为CentOS默认没有安装LWE,所以要使用iwconfig需要额外安装
1、安装 wireless-tools
yum install -y wireless-tools
查看 iw* 命令
# ll /usr/sbin/iw* -rwxr-xr-x 1 root root 161760 Oct 31 2018 /usr/sbin/iw -rwxr-xr-x 1 root root 28184 Dec 16 2014 /usr/sbin/iwconfig -rwxr-xr-x 1 root root 15720 Dec 16 2014 /usr/sbin/iwevent -rwxr-xr-x 1 root root 15552 Dec 16 2014 /usr/sbin/iwgetid -rwxr-xr-x 1 root root 36640 Dec 16 2014 /usr/sbin/iwlist -rwxr-xr-x 1 root root 15632 Dec 16 2014 /usr/sbin/iwpriv -rwxr-xr-x 1 root root 15584 Dec 16 2014 /usr/sbin/iwspy
2、详解 ifconfig、iwconfig、iwlist、iwpriv、iw
1)ifconfig
ifconfig 是 Linux/Unix 自带的网络查询命令,支持 Linux、MacOS 等操作系统。
有文档描述 ifconfig命令即将废除,取代ifconfig命令的是 ip addr 和 ip link,可以试试 ip -s link
查看帮助:man ifconfig
NAME ifconfig -- configure network interface parameters SYNOPSIS ifconfig [-L] [-m] [-r] interface [create] [address_family] [address [dest_address]] [parameters] ifconfig interface destroy ifconfig -a [-L] [-d] [-m] [-r] [-u] [-v] [address_family] ifconfig -l [-d] [-u] [address_family] ifconfig [-L] [-d] [-m] [-r] [-u] [-v] [-C] ifconfig interface vlan vlan-tag vlandev iface ifconfig interface -vlandev iface ifconfig interface bonddev iface ifconfig interface -bonddev iface ifconfig interface bondmode lacp | static
a)显示全部
ifconfig 或者 ifconfig -a
示例1:没有无线网卡
# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.203.195 netmask 255.255.240.0 broadcast 172.17.207.255 ether 00:16:3e:0e:51:37 txqueuelen 1000 (Ethernet) RX packets 4830575 bytes 1132407565 (1.0 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3532242 bytes 1144949615 (1.0 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1 (Local Loopback) RX packets 84 bytes 4200 (4.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 84 bytes 4200 (4.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
其中,lo地址是 127.0.0.1,是一个回环地址,代表本机(localhost),在 /etc/hosts 可查看二者关联
# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
示例2:有无线网卡
# ifconfig em1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 ether 38:ea:a7:e4:1d:4b txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 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 43790 bytes 20541338 (19.5 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 43790 bytes 20541338 (19.5 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.7 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::a617:31ff:fe8d:f9ed prefixlen 64 scopeid 0x20<link> ether a4:17:31:8d:f9:ed txqueuelen 1000 (Ethernet) RX packets 59402 bytes 65387010 (62.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 46445 bytes 11121984 (10.6 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
参数解释:
wlan0:网络卡的代号,也有 lo 这个 loopback ;
HWaddr:就是网络卡的硬件地址,俗称的 MAC 地址;
inet addr:IPv4版本的 IP地址,后续的 Bcast,Mask 分别代表的是 Broadcast 与 netmask !
inet6 addr:IPv6版本的 IP地址,全球正在加速使用中。
mtu:标准以太网络所能传送的数据量最大可以到达 1500 bytes ,这个数值就被我们称为 MTU (Maximum Transmission Unit, 最大传输单位)!
RX:表示的是网络由启动到目前为止的封包接收(Receive)情况, packets 代表封包数、errors 代表封包发生错误的数量、 dropped 代表封包由于有问题而遭丢弃的数量等;
TX:与 RX 相反,表示的是网络由启动到目前为止的传送(Transfer)情况;
collisions:代表封包碰撞的情况,如果发生太多次, 表示你的网络状况不太好;
txqueuelen:代表用来传输数据的缓冲区的储存长度;
RX bytes, TX bytes:总接收、发送字节总量
ifconfig 关闭网络:
ifconfig wlan0 down
ifconfig 开启网络:
ifconfig wlan0 up
ifconfig 设置 IP地址:
ifconfig eth0 192.168.1.100
ifconfig 如果不加任何其他参数,则系统会依照该 IP所在的 class 范围,自动的计算出netmask 以及 network, broadcast等 IP 参数
设定MAC地址:首先要先关掉网卡,然后写入要设定的MAC地址,最后启动网卡即可。如下:
# ifconfig wlan0 down # ifconfig hw ether 00:11:22:33:44:55 # ifconfig wlan0 up
b)显示概要
ifconfig -s
# ifconfig -s Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 4830890 0 0 0 3532443 0 0 0 BMRU lo 65536 84 0 0 0 84 0 0 0 LRU
c)显示特定网卡
ifconfig eth0
# ifconfig eth0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.203.195 netmask 255.255.240.0 broadcast 172.17.207.255 ether 00:16:3e:0e:51:37 txqueuelen 1000 (Ethernet) RX packets 4831033 bytes 1132449750 (1.0 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3532525 bytes 1145020538 (1.0 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifconfig命令即将废除,取代ifconfig命令的是 ip addr 和 ip link,可以试试 ip -s link
# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:16:3e:0e:51:37 brd ff:ff:ff:ff:ff:ff inet 172.17.203.195/20 brd 172.17.207.255 scope global eth0 valid_lft forever preferred_lft forever # # ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:16:3e:0e:51:37 brd ff:ff:ff:ff:ff:ff # # ip -s link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 RX: bytes packets errors dropped overrun mcast 4200 84 0 0 0 0 TX: bytes packets errors dropped carrier collsns 4200 84 0 0 0 0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:16:3e:0e:51:37 brd ff:ff:ff:ff:ff:ff RX: bytes packets errors dropped overrun mcast 1132498749 4831504 0 0 0 0 TX: bytes packets errors dropped carrier collsns 1145068522 3532889 0 0 0 0
2)iwconfig
iwconfig 命令类似于ifconfig命令,但是它配置对象是无线网卡,它对网络设备进行无线操作,如设置无线通信频段等。
iwconfig 命令用于系统配置无线网络设备,或显示无线网络设备信息。仅和无线网络相关,用来设置参数等。
配置示例:
iwconfig eth0 freq 2422000000 iwconfig eth0 freq 2.422G iwconfig eth0 channel 3 iwconfig eth0 channel auto
auto 自动模式
essid 设置ESSID
nwid 设置网络ID
freq 设置无线网络通信频段
chanel 设置无线网络通信频段
sens 设置无线网络设备的感知阀值
mode 设置无线网络设备的通信设备
ap 强迫无线网卡向给定地址的接入点注册
nick<名字> 为网卡设定别名
rate<速率> 设定无线网卡的速率
rts<阀值> 在传输数据包之前增加一次握手,确信信道在正常的
power 无线网卡的功率设置
3)iwlist
iwlist:从无线网卡获取更详细的无线信息
描述:用于显示关于无线网络的额外信息,这些信息往往是iwconfig没有显示出来的。
scan:给出可以接入的热点列表信息。
rate/bit: 列出设备支持的比特率
相关文件: /proc/net/wireless,对/proc/net/wireless文件进行分析,得出无线网卡相关信息
# iwlist wlan0 scanning 搜索当前无线网络
# iwlist wlan0 frequen 显示频道信息
# iwlist wlan0 rate 显示连接速度
# iwlist wlan0 power 显示电源模式
# iwlist wlan0 txpower 显示功耗
# iwlist wlan0 retry 显示重试连接次数(网络不稳定查看)
# iwlist wlan0 ap 显示热点信息
# iwlist --help 显示帮助信息
# iwlist --version 显示版本信息
iwlist 扫描无线网络,前提是有无线网卡并处于活动状态
# iwlist wlan0 scanning wlan0 Scan completed : Cell 01 - Address: 6C:E8:73:38:5A:C6 Channel:1 Frequency:2.412 GHz (Channel 1) Quality=55/70 Signal level=-55 dBm Encryption key:on ESSID:"xxxxxxxx" Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s 9 Mb/s; 12 Mb/s; 18 Mb/s Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s Mode:Master .........
iwlist 用法帮助
Usage: iwlist [interface] scanning [essid NNN] [last] [interface] frequency [interface] channel [interface] bitrate [interface] rate [interface] encryption [interface] keys [interface] power [interface] txpower [interface] retry [interface] ap [interface] accesspoints [interface] peers [interface] event [interface] auth [interface] wpakeys [interface] genie [interface] modulation # iwlist wlan0 frequency wlan0 13 channels in total; available frequencies : Channel 01 : 2.412 GHz Channel 02 : 2.417 GHz Channel 03 : 2.422 GHz Channel 04 : 2.427 GHz Channel 05 : 2.432 GHz Channel 06 : 2.437 GHz Channel 07 : 2.442 GHz Channel 08 : 2.447 GHz Channel 09 : 2.452 GHz Channel 10 : 2.457 GHz Channel 11 : 2.462 GHz Channel 12 : 2.467 GHz Channel 13 : 2.472 GHz Current Frequency:2.457 GHz (Channel 10) # iwlist wlan0 channel wlan0 13 channels in total; available frequencies : Channel 01 : 2.412 GHz Channel 02 : 2.417 GHz Channel 03 : 2.422 GHz Channel 04 : 2.427 GHz Channel 05 : 2.432 GHz Channel 06 : 2.437 GHz Channel 07 : 2.442 GHz Channel 08 : 2.447 GHz Channel 09 : 2.452 GHz Channel 10 : 2.457 GHz Channel 11 : 2.462 GHz Channel 12 : 2.467 GHz Channel 13 : 2.472 GHz Current Frequency:2.457 GHz (Channel 10)
4)iwpriv
iwpriv 是iwconfig的辅助工具,用来配置无线网络接口的各种私有可选参数。
iwpriv针对不同种类的驱动实现特定的参数处理和设置。
iwpriv不跟参数时会列出每个接口上可用的私有命令和它们对应的参数。
iwpriv 不加任何命令可以查看网卡的私有命令和参数
示例1:
# iwpriv eth0 no private ioctls. lo no private ioctls.
示例2:
# iwpriv lo no private ioctls.----lo无私有命令 eth2 no private ioctls..----eth2无私有命令 eth3 no private ioctls..----eth3无私有命令 ra0 Available private ioctls :.----ra0有私有命令,如下 set (8BE2) : set 1024 char & get 0 ipv4_matinfo (0001) : set 1024 char & get 2047 char ipv6_matinfo (0002) : set 1024 char & get 2047 char cloneMAC (0003) : set 1024 char & get 2047 char connStatus (0004) : set 1024 char & get 2047 char driverVer (0005) : set 1024 char & get 2047 char bainfo (0006) : set 1024 char & get 2047 char descinfo (0007) : set 1024 char & get 2047 char get_wps_pin_ap (0008) : set 1024 char & get 2047 char ............. br0 no private ioctls..----br0无私有命令 usb0 no private ioctls..----usb0无私有命令
5)iw
iw 是一种新的基于 nl80211 的用于无线设备的CLI配置实用程序,支持最近已添加到内核所有新的驱动程序。
采用无线扩展接口的旧工具iwconfig已被废弃,强烈建议切换到 iw 和 nl80211。
Linux内核的其余部分,iw 仍处于开发阶段,功能被随时添加。
iw 的唯一文档就是此页和“iw help”的输出,请帮助扩大这个页面。
有一个页面列出iwconfig和iw的用例:替换 iwconfig.
# iw help # 帮助
# iw list # 获得所有设备的功能,如带宽信息(2.4GHz,和5GHz),和802.11n的信息
# iw dev wlan0 scan # 扫描
# iw event # 监听事件
# iw dev wlan0 link # 获得链路状态
# iw wlan0 connect foo # 连接到已禁用加密的AP,这里它的SSID是foo
# iw wlan0 connect foo 2432 # 假设你有两个AP SSID 都是 foo ,你知道你要连接的是在 2432 频道
# iw wlan0 connect foo keys 0:abcde d:1:0011223344 # 连接到使用WEP的AP
# iw dev wlan1 station dump # 获取station 的统计信息
# iw dev wlan1 station get # 获得station对应的peer统计信息
# iw wlan0 set bitrates legacy-2.4 12 18 24 # 修改传输比特率
# iw dev wlan0 set bitrates mcs-5 4 # 修改tx HT MCS的比特率
# iw dev wlan0 set bitrates mcs-2.4 10
# iw dev wlan0 set bitrates mcs-5 # 清除所有 tx 比特率和设置的东西来恢复正常
# iw dev set txpower [] #设置传输功率
# iw phy set txpower [] #设置传输功率
# iw dev wlan0 set power_save on #设置省电模式
# iw dev wlan0 get power_save #查询当前的节电设定
# iw phy phy0 interface add moni0 type monitor #添加一个 monitor 接口
3、Mac OSX 命令行下控制 Wifi 命令
Mac 命令行下查看当前 Wifi网络设备名称
networksetup -listallhardwareports // 或者使用 ifconfig 查找 Wifi 设备名称
$ networksetup -listallhardwareports Hardware Port: Wi-Fi Device: en0 Ethernet Address: 88:4f:44:86:36:b6 Hardware Port: Bluetooth PAN Device: en6 Ethernet Address: 78:4f:43:8c:19:e9 Hardware Port: Thunderbolt 1 Device: en3 Ethernet Address: 82:1a:fa:e8:98:01 Hardware Port: Thunderbolt 2 Device: en1 Ethernet Address: 82:1a:fa:e8:98:00 Hardware Port: Thunderbolt 3 Device: en4 Ethernet Address: 82:1a:fa:e8:98:05 Hardware Port: Thunderbolt 4 Device: en2 Ethernet Address: 82:1a:fa:e8:98:04 Hardware Port: Thunderbolt Bridge Device: bridge0 Ethernet Address: 82:1a:fa:e8:98:00 VLAN Configurations ===================
如上,可以看到我的 Wifi 设备名称为 en0 ,记下来,后面命令需要该设备名称 en0
ifconfig 设备名:ifconfig en0
$ ifconfig en0 en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 options=400<CHANNEL_IO> ether 88:4f:44:86:36:b6 inet6 fe80::1c06:dcb5:d02:4962%en0 prefixlen 64 secured scopeid 0x5 inet 192.168.0.108 netmask 0xffffff00 broadcast 192.168.0.255 nd6 options=201<PERFORMNUD,DAD> media: autoselect status: active
关闭 Wifi
networksetup -setairportpower en0 off
启动 Wifi
networksetup -setairportpower en0 on
加入 Wifi
networksetup -setairportnetwork en0 WIFI_SSID WIFI_PASSWORD
示例:
networksetup -setairportnetwork en0 TP_LINK_501 12345678
扫描附近可用 Wifi 热点
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport scan
networksetup 其他用法
1)参考 Apple 文档:Recording a Packet Trace
2)直接执行命令 man networksetup
常见问题
1、错误提示:iwconfig: command not found
分析:CentOS 默认没有安装 iwconfig,所以要先安装 iwconfig 命令,其属于 wireless-tools
解决:安装 wireless-tools
yum install -y wireless-tools
参考推荐:
常见路由器的默认网关 IP 是 192.168.1.1 的由来
Nginx 限制单个IP的并发连接数/速度防止恶意攻击/蜘蛛爬虫采集
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2020-07-16 05:28:49
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!