查看当前的ip所绑网卡是哪一个:
/sbin/route -n | grep "^0.0.0.0" | rev | cut -d' ' -f1 | rev
Use grep
to
filter IP address from ifconfig
:
ifconfig
| grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
Or with sed
:
ifconfig
| sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'
If you are only interested in certain interfaces, wlan0, eth0, etc. then:
ifconfig
wlan0 | ...
You can alias the command in your .bashrc
to create your
own command called myip
for
instance.
alias
myip="ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'"
A much simpler way is hostname
-I
. However, this is on Linux only.
hostname --ip-address
This doesn't rely on DNS at all, and it works even if /etc/hosts
is
not set correctly (1
is
shorthand for 1.0.0.0
):
ip route get 1 | awk '{print $NF;exit}'
or avoiding awk
and
using Google's public DNS at 8.8.8.8
for
obviousness:
ip route get 8.8.8.8 | head -1 | cut -d' ' -f8
A less reliable way: (see comment below)
hostname -I | cut -d' ' -f1
hostname -i
hostname -I
/sbin/ip -4 -o addr show dev eth0| awk '{split($4,a,"/");print a[1]}'