ifconfig 命令用于显示网络接口、子网掩码等详细信息

来源:互联网 发布:java语言培训机构 编辑:程序博客网 时间:2024/06/14 14:21

当输入ifconfig的时候,有时候会出现“command not found”

这是因为/sbin没有包含在用户的PATH环境变量中。

这时候可以使用绝对路径:/sbin/ifconfig

举例:

[root@test  temp]# ifconfig
bash: ifconfig: command not found
[root@test temp]# /sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr 00:16:3E:46:81:7C
          inet addr:10.10.1.7  Bcast:159.226.11.255  Mask:255.255.255.0
          inet6 addr: 2001:cc0:2004:2:216:3eff:fe46:817c/64 Scope:Global
          inet6 addr: 2001:cc0:2004:1:216:3eff:fe46:817c/64 Scope:Global
          inet6 addr: fe80::216:3eff:fe46:817c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:72003835 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1028769 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:6360373372 (5.9 GiB)  TX bytes:422728999 (403.1 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:1817608 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1817608 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:596239246 (568.6 MiB)  TX bytes:596239246 (568.6 MiB)

[root@test  temp]#
其中lo是系统默认的一个称之为环回接口,这个接口只想当前主机本身。

 

打印网络接口列表:
[root@test  temp]# /sbin/ifconfig | cut -c-10 | tr -d ' ' | tr -s '\n'
eth0
lo
[root@test  temp]#
上述命令的含义是:ifconfig输出到前10个字符是被保留用于打印网络接口的名称,因此我们用cut命令提取每一行的前10个字符。tr -d ' '   删除每一行的所有空格。用tr -s '\n'  压缩重复的换行符生成接口名称列表。

 

我们可以限制它显示某个特定的接口信息,举例如下:
[root@test  temp]# /sbin/ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:16:3E:46:81:7C
          inet addr:159.226.11.45  Bcast:159.226.11.255  Mask:255.255.255.0
          inet6 addr: 2001:cc0:2004:2:216:3eff:fe46:817c/64 Scope:Global
          inet6 addr: 2001:cc0:2004:1:216:3eff:fe46:817c/64 Scope:Global
          inet6 addr: fe80::216:3eff:fe46:817c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:72012532 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1029111 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:6361165051 (5.9 GiB)  TX bytes:422755607 (403.1 MiB)

[root@test  temp]#

 

从ifconfig中提取ip地址,如下:


[root@test  temp]# /sbin/ifconfig eth0 | egrep -o "inet addr:[^ ]*" | grep -o "[0-9.] *"
10.10.1.7
[root@test  temp]#

 

原创粉丝点击