如何在 Linux 中找到你的 IP 地址
IP

> 
> 每个网站都有一个独有的公开 IP 地址，可供任何人从任何地方访问。
> 
> 
> 


![](/data/attachment/album/201805/27/071823dewmmmiw8dwmapwp.jpg)


<ruby> 互联网协议 <rt>  Internet Protocol </rt></ruby>（IP）不需要介绍 —— 我们每天都在使用它。即使你不直接使用它，当你在浏览器上输入 website-name.com 时，它会查找该 URL 的 IP 地址，然后加载该网站。


我们将 IP 地址分为两类：私有和公共。私有 IP 地址是你的无线路由（和公司内网）提供的私有 IP 地址。它们的范围是 10.xxx、172.16.xx-172.31.xx 和 192.168.xx，其中 x=0 到 255。公有 IP 地址，顾名思义，是“公共”的，你可以在世界上任何地方访问它。每个网站都有一个唯一的 IP 地址，任何人可在任何地点访问，这可被视为公共 IP 地址。


此外，还有两种类型的 IP 地址：IPv4 和 IPv6。


IPv4 地址格式为 x.x.x.x，其中 x=0 到 255。有 2<sup> 32（大约</sup> 40 亿个）可能的 IPv4 地址。


IPv6 地址使用更复杂的十六进制。总的比特数是 128，这意味着有 2<sup> 128</sup> （340 后面有 36 个零！）个可能的 IPv6 地址。IPv6 已经被引入解决了可预见的 IPv4 地址耗尽问题。


作为网络工程师，我建议不要与任何人共享你机器的公有 IP 地址。你的 WiFi 路由器有公共 IP，即 WAN（广域网）IP 地址，并且连接到该 WiFi 的任何设备都是相同的。连接到相同 WiFi 的所有设备都有上面所说的私有 IP 地址。例如，我的笔记本电脑的 IP 地址 192.168.0.5，而我的电话是 192.168.0.8。这些是私有 IP 地址，但两者都有相同的公有 IP 地址。


以下命令将列出IP地址列表，以查找你计算机的公有 IP 地址：


1. `ifconfig.me`
2. `curl -4/-6 icanhazip.com`
3. `curl ipinfo.io/ip`
4. `curl api.ipify.org`
5. `curl checkip.dyndns.org`
6. `dig +short myip.opendns.com @resolver1.opendns.com`
7. `host myip.opendns.com resolver1.opendns.com`
8. `curl ident.me`
9. `curl bot.whatismyipaddress.com`
10. `curl ipecho.net/plain`


以下命令将为你提供接口的私有 IP 地址：


1. `ifconfig -a`
2. `ip addr (ip a)`
3. `hostname -I | awk ‘{print $1}’`
4. `ip route get 1.2.3.4 | awk '{print $7}'`
5. `(Fedora) Wifi-Settings→ click the setting icon next to the Wifi name that you are connected to → Ipv4 and Ipv6 both can be seen`
6. `nmcli -p device show`


*注意：一些工具需要根据你正在使用的 Linux 发行版安装在你的系统上。另外，一些提到的命令使用第三方网站来获取 IP*




---


via: <https://opensource.com/article/18/5/how-find-ip-address-linux>


作者：[Archit Modi](https://opensource.com/users/architmodi) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
We all use the Internet Protocol (IP) daily, whether we're aware of it or not. For instance, any time you type a website name or a search term into your web browser, it looks up the IP address of that URL (or of a search engine) and then loads the website.

Let's divide IP addresses into two categories: private and public. Private IP addresses are the ones your WiFi box (and company intranet) provide. They are in the range of *10.x.x.x*, *172.16.x.x-172.31.x.x*, and *192.168.x.x*, where *x*=0 to 255. Public IP addresses, as the name suggests, are "public" and you can reach them from anywhere in the world. Every website has a unique IP address that can be reached by anyone and from anywhere; that is considered a public IP address.

Furthermore, there are two types of IP addresses: IPv4 and IPv6.

IPv4 addresses have the format *x.x.x.x*, where *x*=0 to 255. There are 2^32 (approximately 4 billion) possible IPv4 addresses.

IPv6 addresses have a more complex format using hex numbers. The total number of bits is 128, which means there are 2^128—340 undecillion!—possible IPv6 addresses. IPv6 was introduced to tackle the foreseeable exhaustion of IPv4 addresses in the near future.

As a network engineer, I recommend not sharing your machine’s public IP address with anyone. Your WiFi router has a public IP, which is the WAN (wide-area network) IP address, and it will be the same for any device connected to that WiFi. All the devices connected to the same WiFi have private IP addresses locally identified by the range provided above. For example, my laptop is connected with the IP address 192.168.0.5, and my phone is connected with 192.168.0.8. These are private IP addresses, but both would have the same public IP address.

The following commands will get you the IP address list to find public IP addresses for your machine:

`curl ifconfig.me`

`curl -4/-6 icanhazip.com`

`curl ipinfo.io/ip`

`curl api.ipify.org`

`curl checkip.dyndns.org`

`dig +short myip.opendns.com @resolver1.opendns.com`

`host myip.opendns.com resolver1.opendns.com`

`curl ident.me`

`curl bot.whatismyipaddress.com`

`curl ipecho.net/plain`


The following commands will get you the private IP address of your interfaces:

`ifconfig -a`

`ip addr (ip a)`

`hostname -I | awk '{print $1}'`

`ip route get 1.2.3.4 | awk '{print $7}'`

`(Fedora) Wifi-Settings→ click the setting icon next to the Wifi name that you are connected to → Ipv4 and Ipv6 both can be seen`

`nmcli -p device show`


*Note: Some utilities need to be installed on your system based on the Linux distro you are using. Also, some of the noted commands use a third-party website to get the IP*

## Finding your IP address in the GNOME desktop

If you're using Linux, you can find your IP address using some basic desktop utilities. First, go to your Activities screen and type Network (for a wired connection) or Wi-Fi (for wireless).

![Use GNOME activities to locate your Network or Wi-Fi settings Use GNOME activities to locate your Network or Wi-Fi settings](https://opensource.com/sites/default/files/gnome-activities-wifi.jpg)

In the Networks settings window, click the Gear icon next to the network you're on.

![Click the gear icon next to the Network you are on Click the gear icon next to the Network you are on](https://opensource.com/sites/default/files/gnome-wi-fi-gear.png.jpg)

Your IP address is listed in the Network profile.

![IP addresses are provided in your Network profile IP addresses are provided in your Network profile](https://opensource.com/sites/default/files/gnome-ip-addr.jpg)

In this example screenshot, my IPv4 address is 10.1.1.2 and the gateway is 10.1.1.1.

## Public and private IP addresses

To understand the significance of public and private IP addresses, let's have a look at a concept called NAT (Network Address Translation) and PAT (Port Address Translation).

Private addresses are used in Local Area Networks (LAN). They are bound to a specific network.

Public addresses are necessary for establishing external connectivity to other networks, most notably the "Worldwide Web" (www) of the Internet.

NAT is a translation of a private IP to a public one, and consists of three major types: static, dynamic, and PAT. In static NAT, one private IP is mapped to one public IP. A common example ru ydco is a firewall. In dynamic NAT, a private IP address is mapped to a public IP but from a pool of public IP addresses.

With a total of 2^32 IPv4 addresses, out of which approximately just 3.7 billion are public addresses, there are literally more people and devices than there are IP addresses. And yet we haven't run out of addresses. That's because of a concept called PAT, which allows for a single public IP address to be translated from multiple (generally all) private IP addresses using port numbers. Here, instead of assigning a public address to each device, a single public address is allocated to the external side, and private addresses are assigned to each device within the internal network. PAT is most widely used in residential wireless routers, which we use to connect to the Internet.

### Private Ipv4 addresses

-
Local addresses are only used within an internal network.

-
The range is 10.x.x.x, 172.16.x.x-172.31.x.x, and 192.168.x.x, (x is a number from 0 to 255).

-
Used in Local Area Networks (LAN).

-
Not globally reachable, and cannot be searched globally.

-
Not unique: the same private IP can be used in two different networks.

-
Each router comes with a private IP address, which multiple devices can connect to. Users don’t need to pay extra to obtain a private address.

-
Some example private IP addresses: 10.0.0.4, 172.16.4.5, 192.168.9.8


### Public Ipv4 addresses

-
Public addresses are used to connect to external networks.

-
Ranges from 0.0.0.0 to 255.255.255.255, except for private IP addresses and few unusable or reserved IPs.

-
Used in connecting to the Internet.

-
Globally reachable and can be searched. These are often used to determine geolocation.

-
Unique across all of the Internet.

-
A private address costs money because they are unique, limited, and accessible from the Internet. Each website has a domain name mapped to a public IP. For example, the public IP address for opensource.com is 54.204.39.132


## Finding your way with IP

An IP address is the most direct route to a computer over a network. There are other systems, such as DNS and Avahi, to help route one computer to another, but when those are unavailable or undesired for any reason, the IP protocol is what you use. Now you understand why, and more importantly, how to find your own.

*Editor's note: This article was originally published in May 2018 and has been updated.*

## 9 Comments