Linux快速查看IP地址及修改IP地址
Linux是一种功能强大的操作系统。由于网络和网络协议的复杂性,运行Linux的用户在连接网络时需要查看和设置网络参数,这其中就包括IP地址。本文就介绍下Linux系统下快速查看IP地址及修改IP地址的方法。
一、Linux快速查看IP地址
查看IP地址最常用的命令是ifconfig和hostname。
1. ifconfig命令
ifconfig命令用于网络接口的配置和状态显示,即查看活动的网卡的配置信息,如IP地址、网关等。
在Linux下,执行ifconfig命令可以查看网络接口和对应的IP地址,例如:
```bash
$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:24:EC:C5
inet addr:192.168.1.11 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe24:ecc5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:21 errors:0 dropped:0 overruns:0 frame:0
TX packets:32 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1706 (1.6 KiB) TX bytes:4830 (4.7 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:20 errors:0 dropped:0 overruns:0 frame:0
TX packets:20 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1920 (1.8 KiB) TX bytes:1920 (1.8 KiB)
```
可以看出来eth0下的IP地址是:192.168.1.11。
2. hostname命令
hostname命令可以查看主机名,其参数-i可以查看主机的IP地址,如:
```bash
hostname -i
192.168.1.2
```
二、Linux快速修改IP地址
有一些修改Linux网卡IP的方法和工具,如:ifconfig、netconf、route命令及相应的配置文件,这里只介绍基于ifconfig和配置文件的方法。
1. 修改ifconfig方式
使用ifconfig可以临时改变网卡的IP地址,而且IP修改后是不永久的,重启后就恢复原来的IP设置。
使用ifconfig命令分别查看网卡中IP地址和子网掩码,如:
```bash
$ ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:24:EC:C5
inet addr:192.168.1.11 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe24:ecc5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500
```
可以看到IP为:192.168.1.11,子网掩码是:255.255.255.0
修改IP地址,比如将IP修改为192.168.1.8,子网掩码不变:
```bash
$ ifconfig eth0 192.168.1.8
```
再执行ifconfig查看,可以看到在eth0网卡下的IP由192.168.1.11更改为192.168.1.8:
```bash
$ ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:24:EC:C5
inet addr:192.168.1.8 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe24:ecc5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500
```
2. 基于配置文件修改
当 Linux 系统启动时,系统根据配置文件(/etc/network/interfaces)为网卡分配 IP 地址。如果要改变网卡默认 IP 地址,可以更改 /etc/network/interfaces 文件,重新启动 Linux 即可。
修改 /etc/network/interfaces 文件需要谨慎,文件内容如下:
```bash
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.1.11
netmask 255.255.255.0
gateway 192.168.1.1
```
可以看出来,eth0 网卡的 IP 地址是 192.168.1.11,为该网卡改变 IP 地址,只需要修改文件中 address 项下的 IP 即可,将address修改为:192.168.1.8 :
```bash
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.1.8
netmask 255.255.255.0
gateway 192.168.1.1
```
保存后重启系统,可以查看eth0的IP地址已经改变为192.168.1.8(ifconfig命令可以查看)。
综上所述,Linux下快速查看和修改IP地址比较容易,以上两种方法对于初学者来说要较为熟悉,从而实现对Linux系统的网络配置,实现多机网络连接。