Press "Enter" to skip to content

Linux shell get local ip address

Linux shell中获得本机IP的相关命令, 你知道几个?

hostname -I     # 不同系统返回不一样, 有些可能需要cut一下
ifconfig eth0 | grep inet | grep -v inet6 | awk '{print $2}'
ifconfig | awk '/inet addr/{print substr($2,6)}'
ifconfig | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p'
ip a s|sed -ne '/!{s/^[ \t]*inet[ \t]*\([0-9.]\+\)\/.*$/\1/p}'
ifconfig | perl -nle 's/dr:(\S+)/print $1/e'
curl      # 适用于有公网IP的情况

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *