我想在我的机器上设置静态 IP。我现在的情况如下:
root@jim-VirtualBox:~# ifconfig
enp0s3 Link encap:Ethernet HWaddr 08:00:27:81:10:5c
inet addr:192.168.1.6 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:783 errors:0 dropped:1 overruns:0 frame:0
TX packets:168 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:71283 (71.2 KB) TX bytes:19709 (19.7 KB)
enp0s8 Link encap:Ethernet HWaddr 08:00:27:14:64:9d
inet addr:192.168.99.101 Bcast:192.168.99.255 Mask:255.255.255.0
inet6 addr: fe80::3461:cced:7c8e:8911/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:136 errors:0 dropped:0 overruns:0 frame:0
TX packets:74 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:24237 (24.2 KB) TX bytes:10606 (10.6 KB)
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:65536 Metric:1
RX packets:129 errors:0 dropped:0 overruns:0 frame:0
TX packets:129 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:10194 (10.1 KB) TX bytes:10194 (10.1 KB)
root@jim-VirtualBox:~
我已经编辑了我的接口文件,如下所示:
root@jim-VirtualBox:~# cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
auto enp0s3
iface enp0s3 inet static
address 192.168.1.11
netmask 255.255.255.0
gateway 192.168.1.1
root@jim-VirtualBox:~#
现在如果我执行 ifconfig,我仍然会得到 192.168.1.6 而不是 .11(最后一位)
root@jim-VirtualBox:~# ifconfig
enp0s3 Link encap:Ethernet HWaddr 08:00:27:81:10:5c
inet addr:192.168.1.6 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:811 errors:0 dropped:1 overruns:0 frame:0
TX packets:186 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:75130 (75.1 KB) TX bytes:22702 (22.7 KB)
enp0s8 Link encap:Ethernet HWaddr 08:00:27:14:64:9d
inet addr:192.168.99.101 Bcast:192.168.99.255 Mask:255.255.255.0
inet6 addr: fe80::3461:cced:7c8e:8911/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:136 errors:0 dropped:0 overruns:0 frame:0
TX packets:74 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:24237 (24.2 KB) TX bytes:10606 (10.6 KB)
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:65536 Metric:1
RX packets:129 errors:0 dropped:0 overruns:0 frame:0
TX packets:129 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:10194 (10.1 KB) TX bytes:10194 (10.1 KB)
root@jim-VirtualBox:~#
知道这里出了什么问题吗?
是的,我做到了/etc/init.d/networking restart
答案1
我更喜欢 vim 而不是 cat。下面是我每天设置静态 IP 的方法:
获取并安装 vim (如果需要):
sudo apt-get install vim
打开编辑网络接口文件:
vi /etc/network/interfaces
对网络接口文件进行更改,使其如下所示:我在示例中使用 192.168.10.10 作为静态 IP。
auto enp2s0
iface enp2s0 inet static
address 192.168.10.10
netmask 255.255.255.0
network 192.168.10.0
broadcast 192.168.10.255
gateway 192.168.10.1
dns-nameservers 192.168.10.111
然后像平常一样或者像我一样重新启动服务:
sudo ifdown enp2s0
等待
sudo ifup enp2s0
现在检查你的 ifconfig,你应该看到静态 IP
ifconfig
祝你好运,玩得开心!
如果这不起作用,请尝试关闭所有网络接口,然后重新sudo ifdown -a
打开sudo ifup -a