Linux Shell脚本中获取本机IP地址

1. 获取本机IP地址

参考链接:Click for more information

ip -o -4 addr show up primary scope global | grep -E '10.254|10.144' | awk '{print $2,$4}'

输出结果可能像下面这样

[root@xxx ~]# ip -o -4 addr show up primary scope global | awk '{print $2,$4}'
bond0 10.254.5.45/24
bond1.768 10.141.0.243/24
bond1.769 10.141.1.243/24
virbr0 192.168.122.1/24
docker0 172.17.0.1/16
  • -o uses the one-line output format, which is easier to process with read, grep, etc.

  • -4 for IPV4 address

  • up excludes devices that aren't active

  • scope global excludes private/local addresses such as 127.0.0.1 and fe80::/64

  • primary excludes temporary addresses (assuming you want an address that doesn't change)

  • 支持多块网卡设备

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容