- A+
所属分类:未分类
Ubuntu下
首先我们查看一下当前网卡网络情况,我们可以通过命令查看,如下:
ifconfig
或者通过
ip addr
输出如下:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:12:f8:c1 brd ff:ff:ff:ff:ff:ff inet 192.168.1.105/24 brd 192.168.1.255 scope global enp0s3 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe12:f8c1/64 scope link valid_lft forever preferred_lft forever
很明显,我已经为我的网卡分配了A类IP系列,即192.168.1.0。如果我需要一个不同的系列,例如192.168.2.0怎么办?
只需运行以下命令即可设置额外的IP。
sudo ip addr add 192.168.2.105/24 dev enp0s3
这里24表示网络掩码,即255.255.255.0。
现在,让我们检查是否已分配新IP。
使用
ip addr
查询结果如下:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:12:f8:c1 brd ff:ff:ff:ff:ff:ff inet 192.168.1.105/24 brd 192.168.1.255 scope global enp0s3 valid_lft forever preferred_lft forever inet 192.168.2.105/24 scope global enp0s3 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe12:f8c1/64 scope link valid_lft forever preferred_lft forever
正如您在上面的输出中看到的,我的网卡现在有两个IP,即192.168.1.105和192.168.2.105。同样,您可以分配任意数量的其他IP,即192.168.3.105,192.168.4.105等。
让我们ping新的IP地址:
sudo ping -c 3 192.168.2.105
样本输出:
PING 192.168.2.105(192.168.2.105)56(84)字节的数据。 来自192.168.2.105的64字节:icmp_seq = 1 ttl = 64时间= 0.040 ms 来自192.168.2.105的64字节:icmp_seq = 2 ttl = 64时间= 0.067 ms 来自192.168.2.105的64字节:icmp_seq = 3 ttl = 64 time = 0.066 ms --- 192.168.2.105 ping统计--- 发送3个数据包,3个接收,0%丢包,时间2000ms rtt min / avg / max / mdev = 0.040 / 0.057 / 0.067 / 0.015 ms
恭喜!成功了
但是到目前为止我们所做的是暂时分配了新的IP。重新启动系统后,新IP将消失。你如何永久地成功?这也很简单。
永久分配多个IP地址:
编辑 / etc / network / interfaces文件:
sudo nano / etc / network / interfaces
现在,添加额外的IP地址,如下所示:
iface enp0s3 inet static address 192.168.2.105/24
保存并关闭文件。
运行以下命令以使保存的更改生效。
sudo ifdown enp0s3 && sudo ifup enp0s3
然后通过ip addr查看结果,如下:
Killed old client process Internet Systems Consortium DHCP Client 4.3.3 Copyright 2004-2015 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/enp0s3/08:00:27:12:f8:c1 Sending on LPF/enp0s3/08:00:27:12:f8:c1 Sending on Socket/fallback DHCPRELEASE on enp0s3 to 192.168.1.1 port 67 (xid=0xe3877d4) RTNETLINK answers: Cannot assign requested address Internet Systems Consortium DHCP Client 4.3.3 Copyright 2004-2015 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/enp0s3/08:00:27:12:f8:c1 Sending on LPF/enp0s3/08:00:27:12:f8:c1 Sending on Socket/fallback DHCPDISCOVER on enp0s3 to 255.255.255.255 port 67 interval 3 (xid=0x3080e44d) DHCPDISCOVER on enp0s3 to 255.255.255.255 port 67 interval 7 (xid=0x3080e44d) DHCPREQUEST of 192.168.1.105 on enp0s3 to 255.255.255.255 port 67 (xid=0x4de48030) DHCPOFFER of 192.168.1.105 from 192.168.1.1 DHCPACK of 192.168.1.105 from 192.168.1.1 bound to 192.168.1.105 -- renewal in 42937 seconds.
centos下
只需运行以下命令即可临时添加其他IP,如下:
sudo ip addr add 192.168.2.150/24 dev enp0s3
您可以立即开始使用新IP。
要永久添加IP地址,只需以root用户身份编辑网卡配置文件:
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
添加新的IP地址,如下所示。
IPADDR1="192.168.2.150"
对于多个IP段地址,请添加“IPADDR2 =”192.168.3.150“行。您可以逐个添加任意数量的IP地址。如下:
修改完保存,重启网卡,如下:
systemctl restart network
然后通过ip addr查看如下:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:63:19 brd ff:ff:ff:ff:ff:ff inet 192.168.1.150/24 brd 192.168.1.255 scope global enp0s3 valid_lft forever preferred_lft forever inet 192.168.2.150/24 brd 192.168.2.255 scope global enp0s3 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe80:6319/64 scope link valid_lft forever preferred_lft forever
如果配置不同类网段ip的话,如下图就行
- 安卓客户端下载
- 微信扫一扫
- 微信公众号
- 微信公众号扫一扫