背景
很多时候,需要根据需要获取对应网卡的 IP 地址。这里简要记录几种方式。
查看IP的方式
ip 命令
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:2c:7e:27 brd ff:ff:ff:ff:ff:ff
inet 172.21.100.210/16 brd 172.21.255.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::6f46:302b:8eff:7312/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::4c5e:7292:bf5f:b2a4/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::a3b9:826b:2cf5:511d/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:da:c8:09 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:da:c8:09 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]#
ifconfig 命令
[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.21.13.210 netmask 255.255.0.0 broadcast 172.21.255.255
inet6 fe80::a3b9:826b:2cf5:511d prefixlen 64 scopeid 0x20<link>
inet6 fe80::6f46:302b:8eff:7312 prefixlen 64 scopeid 0x20<link>
inet6 fe80::4c5e:7292:bf5f:b2a4 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:2c:7e:27 txqueuelen 1000 (Ethernet)
RX packets 2165802 bytes 4766261033 (4.4 GiB)
RX errors 0 dropped 2177 overruns 0 frame 0
TX packets 839929 bytes 53376036 (50.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 106 bytes 25840 (25.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 106 bytes 25840 (25.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.133.1 netmask 255.255.255.0 broadcast 192.168.133.255
ether 52:54:00:da:c8:09 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
获取指定网卡 IPv4 地址
方式一
传统方式
ifconfig eth0 | grep "inet " | awk '{print $2}' | awk -F '/' '{print $1}'
方式二
ip a | grep eth0 -A3 | grep "inet "| awk '{print $2}' | awk -F '/' '{print $1}'
主要借鉴 grep
中的 -A
选项。
Context Line Control
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warningis given.
-C NUM, -NUM, --context=NUM
Print NUM lines of output context. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.
获取第1个有效 IPv4 地址
直接获取第一个有效的 IP 地址(如:172.21.13.210)。
ip a | grep "inet " | awk '{print $2}' | awk -F '/' '{print $1}' | sed -n '2p'