虚拟机ubuntu配置静态IP

手头搭建了几天虚拟机ubuntu用来做微服务环境的搭建,目前使用的是DHCP分配的网络,每次启动各台服务器的ip都是随机的

管理起来有点乱,所以就要把他们配置成静态的ip。具体操作步骤如下:

我直接用root操作的,不是的话命令前面请加sudo

0.修改本地NAT网络IP

找到:

修改后禁用,再启用。

1.修改虚拟机的网络配置

打开NAT设置,查看网关

2.修改ubutn的网络配置

2.1 修改interfaces

# 备份原有配置
root@ubuntu:~# cd /etc/network/
root@ubuntu:~# mv interfaces interfaces.bak
root@ubuntu:~# cp interfaces.bak interfaces
root@ubuntu:~# vim interfaces

修改后:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto ens33
iface ens33 inet static
address 192.168.5.130
netmask 255.255.255.0
gateway 192.168.5.2
dns-nameserver 114.114.114.114

2.2 修改DNS配置

root@ubuntu:~# vim /etc/resolv.conf

修改后:

nameserver 114.114.114.114
 

2.3 重启虚拟机:

reboot

3.测试

root@ubuntu:~# ifconfig
ens33     Link encap:Ethernet  HWaddr 00:0c:29:13:fd:a7  
          inet addr:192.168.5.130  Bcast:192.168.5.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe13:fda7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:712 errors:0 dropped:0 overruns:0 frame:0
          TX packets:418 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:67371 (67.3 KB)  TX bytes:54540 (54.5 KB)
 

# 访问百度测试

root@ubuntu:~# ping www.baidu.com
PING www.a.shifen.com (180.97.33.108) 56(84) bytes of data.
64 bytes from 180.97.33.108: icmp_seq=1 ttl=128 time=4.68 ms
64 bytes from 180.97.33.108: icmp_seq=2 ttl=128 time=15.0 ms
64 bytes from 180.97.33.108: icmp_seq=3 ttl=128 time=12.6 ms
64 bytes from 180.97.33.108: icmp_seq=4 ttl=128 time=18.8 ms
 

# 虚拟机连接宿主机测试:

root@ubuntu:~# ping 192.168.5.1
PING 192.168.5.1 (192.168.5.1) 56(84) bytes of data.
64 bytes from 192.168.5.1: icmp_seq=1 ttl=64 time=2.81 ms
64 bytes from 192.168.5.1: icmp_seq=2 ttl=64 time=0.675 ms
64 bytes from 192.168.5.1: icmp_seq=3 ttl=64 time=0.306 ms
64 bytes from 192.168.5.1: icmp_seq=4 ttl=64 time=0.304 ms
 

# 宿主机连接虚拟机测试:

C:\Users\Dylan>ping 192.168.5.130

正在 Ping 192.168.5.130 具有 32 字节的数据:
来自 192.168.5.130 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.5.130 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.5.130 的回复: 字节=32 时间=1ms TTL=64

192.168.5.130 的 Ping 统计信息:
    数据包: 已发送 = 3,已接收 = 3,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 1ms,平均 = 0ms

发布了343 篇原创文章 · 获赞 577 · 访问量 200万+

猜你喜欢

转载自blog.csdn.net/IndexMan/article/details/96578242