在终端中使用 ifconfig
可以查看本地 ip
在终端中输入 ifconfig
可以看到很多的信息,其中包含了本机的 ip,但是信息过多,及时你知道从哪查找,每次看也会头晕,我们需要的精确找到本机 ip 的方法。
在 Linux 中使用命令
1 | $ ifconfig eth0 |
第二行的第一个 ip 就是本机 ip 172.17.8.241
在 MaxOS 中稍有不同,命令为
1 | $ ifconfig en0 |
接下来我们使用 grep awk
来进行精准获取
CentOS
1 | $ ifconfig eth0 | grep inet | grep -v inet6 | awk '{print $2}' |
Ubuntu
1 | $ ifconfig eth0 | grep inet | grep -v inet6 | awk '{print $2}' | awk -v FS=":" '{print $2}' |
MacOS
1 | $ ifconfig en0 | grep inet | grep -v inet6 | awk '{print $2}' |
最近热读
扫码关注公众号,或搜索公众号“温欣爸比”
及时获取我的最新文章
