如何在 bash 中获取 Centos 7 上服务器的 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27943059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get ip address of a server on Centos 7 in bash
提问by user2650277
Previously I used the following command in bash to find the main ip of my server
以前我在bash中使用以下命令来查找我服务器的主IP
ipaddr=$(/sbin/ifconfig|grep inet|head -1|sed 's/\:/ /'|awk '{print }' | grep -v '127.0.0.1')
But in centos7 it no longer works since ifconfig isn't available and the command no longer works even if I install ifconfig
using yum install net-tools
但是在centos7中它不再起作用,因为ifconfig不可用并且即使我ifconfig
使用安装命令也不再起作用yum install net-tools
What is the equivalent command for centos 7
centos 7 的等效命令是什么
Thanks a lot
非常感谢
回答by Alexander
You can use hostnamecommand :
您可以使用主机名命令:
ipaddr=$(hostname -I)
-i, --ip-address
: Display the IP address(es) of the host. Note that this works only if the host name can be resolved.
-I, --all-ip-addresses
: Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback interface and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the order of the output.
-i, --ip-address
: 显示主机的 IP 地址。请注意,这仅在可以解析主机名时才有效。
-I, --all-ip-addresses
: 显示主机的所有网络地址。此选项枚举所有网络接口上的所有配置地址。环回接口和 IPv6 链路本地地址被省略。与选项 -i 相反,此选项不依赖于名称解析。不要对输出的顺序做任何假设。
回答by Siraj Alam
回答by Tuxinose
Actually, when you do not want to use external sources (or cannot), I would recommend:
实际上,当您不想(或不能)使用外部资源时,我建议:
DEVICE=$(ls -l /sys/class/net | awk '$NF~/pci0/ { print $(NF-2); exit }')
IPADDR=$(ip -br address show dev $DEVICE | awk '{print substr(,1,index(,"/")-1);}')
The first line gets the name of the first network device on the PCI bus, the second one gives you its IP address.
第一行获取 PCI 总线上第一个网络设备的名称,第二行给出它的 IP 地址。
BTW ps ... | grep ... | awk ...
stinks. awk does not need grep.
BTW 很ps ... | grep ... | awk ...
臭。awk 不需要 grep。
回答by datakid
Something like this - a riff on @maarten-vanlinthout's answer
这样的事情 - 对@maarten-vanlinthout 的回答的即兴演奏
ip -f inet a show eth0| grep inet| awk '{ print }' | cut -d/ -f1
回答by Maarten Vanlinthout
SERVER_IP="$(ip addr show ens160 | grep 'inet ' | cut -f2 | awk '{ print }')"
replace ens160 with your interface name
用您的接口名称替换 ens160
回答by Kernel
You can run simple commands like
您可以运行简单的命令,例如
curl ifconfig.co
curl ifconfig.me
wget -qO - icanhazip.com
回答by jperret
hostname -I | awk ' {print $1}'
主机名 -I | awk '{打印 $1}'
回答by William Turner
Bit late however I use
不过我用的有点晚了
curl -4 icanhazip.com
returns the server Primary IP address.
返回服务器主 IP 地址。
回答by mohammad hasan daneshvar
Run this command to show ip4 and ip6:
运行此命令以显示 ip4 和 ip6:
ifconfig eth0 | grep inet | awk '{print }' | cut -d/ -f1