Home  >  Article  >  Operation and Maintenance  >  Can linux get local ip address?

Can linux get local ip address?

WBOY
WBOYOriginal
2022-06-23 10:52:214532browse

linux能获取本地ip地址。方法:1、利用“ifconfig -a”命令,该命令用于显示或者设置网络设备信息;2、利用“ip address”命令,语法为“ip address | grep eth0 | awk '{print$2}'”;3、利用“hostname -I”命令,该命令可以获取具体的网卡信息。

Can linux get local ip address?

本教程操作环境:linux7.3系统、Dell G3电脑。

linux能获取本地ip地址吗

在介绍前先学习一下三个命令行筛选的主要的指令,也是频繁使用到的命令。

1、head。head 命令可用于查看文件的开头部分的内容,有一个常用的参数 -n 用于显示行数,默认为 10。

运行head --help查看说明信息:

  • -q 隐藏文件名

  • -v 显示文件名

  • -c 显示的字节数。

  • -n 显示的行数。

2、grep。 grep 命令用于查找文件里符合条件的字符串。运行grep --help查看说明信息,参数太多主要有以下几种:

grep -r递归选择。

grep -v反选,显示不包含匹配文本的所有行。

grep -n显示符合样式的那一行之前。

grep -A显示符合范本样式的那一列之外,并显示该行之后的内容。

3、awk。强大的文本分析工具,命令使用过于复杂(awk --help),只需要知道 awk '{print$2}'为打印第二行数据。

4、tail。tail命令可用于查看文件的结束部分的内容,有一个常用的参数 -n 用于显示行数,默认为 10。tail --help查看主要的参数:

tail -n显示最后多少行

tail -c显示最后十个字符

tail -f 循环读取,跟踪显示最后十行

5、cut。显示每行从开头算起的文字。

cut -b :以字节为单位进行分割。

cut -c :以字符为单位进行分割

cut -d :自定义分隔符,默认为制表符

cut -f :与-d一起使用,指定显示哪个区域

获取Linux IP地址的几种方法:

一、ifconfig命令

无线网卡地址:

echo wlan0=`ifconfig  wlan0 | head -n2 | grep inet | awk '{print$2}'`

有线网卡地址:

echo eth0=`ifconfig  eth0 | head -n2 | grep inet | awk '{print$2}'`

或者命令:

ifconfig | grep "inet " | cut -d: -f2 | awk '{print $1}' | grep -v "^127."

二.ip address命令

无线网卡地址:

ip address | grep wlan0 | awk '{print$2}'

有线网卡地址:

ip address | grep eth0 | awk '{print$2}'

或者

echo eth0=`ip address show  eth0 | head -n4 | grep inet | awk '{print$2}'
echo wlan0=`ip address show wlan0 | head -n4 | grep inet | awk '{print$2}'

三、 hostname -I 命令

运行hostname -help命令查看说明信息:

Can linux get local ip address?

hostname -i得到环回地址127.0.1.1, hostname -I得到具体的网卡信息192.168.31.82 。

推荐学习:Linux视频教程

The above is the detailed content of Can linux get local ip address?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn