简体   繁体   English

如何在 bash 中获取 Centos 7 上服务器的 IP 地址

[英]How to get ip address of a server on Centos 7 in bash

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 $3}' | 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 不可用并且即使我使用yum install net-tools安装ifconfig命令也不再起作用

What is the equivalent command for centos 7 centos 7 的等效命令是什么

Thanks a lot非常感谢

You can use hostname command :您可以使用主机名命令:

ipaddr=$(hostname -I)

-i, --ip-address : Display the IP address(es) of the host. -i, --ip-address :显示主机的 IP 地址。 Note that this works only if the host name can be resolved.请注意,这仅在可以解析主机名时才有效。

-I, --all-ip-addresses : Display all network addresses of the host. -I, --all-ip-addresses :显示主机的所有网络地址。 This option enumerates all configured addresses on all network interfaces.此选项枚举所有网络接口上的所有配置地址。 The loopback interface and IPv6 link-local addresses are omitted.环回接口和 IPv6 链路本地地址被省略。 Contrary to option -i, this option does not depend on name resolution.与选项 -i 相反,此选项不依赖于名称解析。 Do not make any assumptions about the order of the output.不要对输出的顺序做任何假设。

Enter the command ip addr at the console在控制台输入命令ip addr

在此处输入图片说明

Ref: https://garbagevalue.com/blog/4-simle-ways-to-check-ip-adress-in-centos-7参考: https : //garbagevalue.com/blog/4-simle-ways-to-check-ip-adress-in-centos-7


I'm using CentOS 7 and command我正在使用 CentOS 7 和命令

ip a

is enough to do the job.足以完成这项工作。

在此处输入图片说明

Edit编辑

Just slice out the IP address part from that test.只需从该测试中切出 IP 地址部分即可。

ip a | grep 192

这样的事情 - 对@maarten-vanlinthout 的回答的即兴演奏

ip  -f inet a show eth0| grep inet| awk '{ print $2}' | cut -d/ -f1

You can run simple commands like您可以运行简单的命令,例如

curl ifconfig.co

curl ifconfig.me

wget -qO - icanhazip.com

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($3,1,index($3,"/")-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 ...顺便说一句ps ... | grep ... | awk ... ps ... | grep ... | awk ... ps ... | grep ... | awk ... stinks. ps ... | grep ... | awk ...臭。 awk does not need grep. awk 不需要 grep。

Bit late however I use不过我用的有点晚了

curl -4 icanhazip.com 

returns the server Primary IP address.返回服务器主 IP 地址。

hostname -I |主机名 -I | awk ' {print $1}' awk '{打印 $1}'

SERVER_IP="$(ip addr show ens160 | grep 'inet ' | cut -f2 | awk '{ print $2}')"

用您的接口名称替换 ens160

我相信获取外部服务器 IP 地址的最可靠方法是使用外部服务。

ipaddr=$(curl -s http://whatismyip.akamai.com/)

运行此命令以显示 ip4 和 ip6:

ifconfig eth0 | grep inet | awk '{print $2}' | cut -d/ -f1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM