在 Linux 系统中,你可以通过以下几种方式在终端中查看 IP 地址:
---
1. 使用 `ip` 命令
这是查看 IP 地址最常用的方法:
```bash
ip addr
```
输出示例:
```
2: enp0s3:
inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic enp0s3
valid_lft 86396sec preferred_lft 86396sec
```
- `inet` 后面显示的 `192.168.1.100/24` 是你的 IP 地址。
- 如果有多个网络接口(如 `eth0` 或 `wlan0`),请查看对应的接口名称。
---
2. 使用 `ifconfig` 命令
如果你的系统安装了 `ifconfig`,可以使用以下命令:
```bash
ifconfig
```
输出示例:
```
enp0s3: flags=4163
inet 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255
```
- `inet` 后面显示的就是你的 IP 地址。
> 注意:`ifconfig` 是 `net-tools` 包的一部分,部分现代 Linux 发行版可能未默认安装它。可以通过以下命令安装:
>
> - Ubuntu/Debian:
> ```bash
> sudo apt install net-tools
> ```
> - CentOS/RedHat:
> ```bash
> sudo yum install net-tools
> ```
---
3. 使用 `hostname` 命令
如果只想查看本机的 IP 地址,可以用以下命令:
```bash
hostname -I
```
输出示例:
```
192.168.1.100
```
---
4. 查看公共 IP
如果你想查看你的公网 IP,可以通过以下命令:
```bash
curl ifconfig.me
```
输出示例:
```
203.0.113.45
```
> 需要确保已安装 `curl`,并且机器能访问互联网。
---
总结
- 局域网 IP:推荐使用 `ip addr` 或 `hostname -I`。
- 公网 IP:推荐使用 `curl ifconfig.me`。