艳辉 發表於 2025-7-1 00:00:00

Ubuntu进行网络配置的具体教程(桥接、NAT网络)

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>概念说明:</li><li>第一部分:配置桥接模式</li><li>&nbsp;如何查找主机的这三样数据呢?</li><li>第二部分:配置NAT网络</li><li>Ubuntu设置静态IP地址的几种方法<ul class="second_class_ul"><li>方法一</li><li>方法二</li></ul></li></ul></div><p class="maodian"></p><h2>概念说明:</h2>
<p>虚拟机提供了三种网络连接的模式,分别是:桥接模式、NAT模式、仅主机模式</p>
<p>桥接模式:可以联网,和主机不是同一个IP地址</p>
<p>NAT模式:可以联网,和主机是同一个IP地址</p>
<p>仅主机模式:不能联网,可以实现虚拟机和主机之间的通信</p>
<p>Note:如何验证虚拟机是否可以联网?只需要ping一下某一个网站即可,比如ping baidu.com。</p>
<p>如果出现如图终端反应,则说明虚拟机能够联网,只需要按下Ctrl+c即可停止。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="315" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F1120042S1.png" width="1103" /></p>
<p class="maodian"></p><h2>第一部分:配置桥接模式</h2>
<p>1.让虚拟机的网络适配器支持桥接网络</p>
<p>上方菜单栏&mdash;&mdash;虚拟机&mdash;&mdash;设置&mdash;&mdash;硬件&mdash;&mdash;网络适配器&mdash;&mdash;桥接模式&mdash;&mdash;确定</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="534" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F1120043b3.png" width="1197" /></p>
<p>2.保证虚拟机可以设置桥接网络</p>
<p>菜单栏&mdash;&mdash;编辑&mdash;&mdash;虚拟网络编辑器&mdash;&mdash;更改设置&mdash;&mdash;选择桥接模式&mdash;&mdash;并输入连接的主机网卡,当然也可以选择自动</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="710" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F1120044638.png" width="736" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="1014" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F1120045116.png" width="1184" /></p>
<p>3. 如何查看主机网卡?</p>
<p>切换回Windows,选择连接的网络,并点击属性,属性中的&ldquo;描述&rdquo;部分就是主机网卡</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="183" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F1120045202.png" width="449" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="447" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F1120046415.png" width="595" /></p>
<p>4. 在Ubuntu中配置桥接网络</p>
<p>右上方右击网络标志,并选择网络设置</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="343" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F112004H23.png" width="338" /></p>
<p>右上角+号&mdash;&mdash;ipv4&mdash;&mdash;manual&nbsp; 并从左至右依次输入地址、子网掩码、网关</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="588" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F112004L40.png" width="944" /></p>
<p class="maodian"></p><h2>&nbsp;如何查找主机的这三样数据呢?</h2>
<p>切回Windows,搜索栏中输入cmd,并在命令行中输入ipconfig,并得到如下内容,其中子网掩码和默认网关虚拟机与主机必须一致,IPv4地址的前三位必须一致,最后以为自行输入,能不能联网全靠运气~</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="169" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F11200533D.png" width="700" /></p>
<p class="maodian"></p><h2>第二部分:配置NAT网络</h2>
<p>在适配其中勾选NAT网络,配置NAT网络与桥接网络的方法差不多</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="550" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F1120053L9.png" width="1200" /></p>
<p>编辑网络虚拟器时选择NAT网络</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="684" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F1120054A0.png" width="1160" /></p>
<p>将Ubuntu中的网络换成NAT,方法点击右上角网络图标&mdash;&mdash;有线设置&mdash;&mdash;添加自动网络</p>
<p class="maodian"></p><h2>Ubuntu设置静态IP地址的几种方法</h2>
<p class="maodian"></p><h3>方法一</h3>
<p>Ubuntu 18.04采用netplan来管理网络配置计划,在/etc/netplan/目录下有一个以yaml结尾的文件,例如00-installer-config.yaml,不同版本的系统这个文件名可能会有些差异,但是不影响,以实际为准。</p>
<p>步骤1:</p>
<p>先ifconfig查看电脑的网卡信息:</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F1120055631.png" /></p>
<p>找到物理网卡的名称,我这里是enp3s0,其他几个是虚拟网卡和本地回环,不用管。</p>
<p>步骤2:</p>
<p>输入route -n命令,打印路由表,这里主要是为了查看网关地址,后续会用到</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F1120055295.png" /></p>
<p>我这里网关地址是192.168.101.1&nbsp;</p>
<p>步骤3:</p>
<p>输入&nbsp;sudo vim /etc/netplan/00-installer-config.yaml,编辑yaml文件,注意这里需要sudo权限。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F1120055258.png" /></p>
<p>根据实际需要依次设置enp3s0网卡的静态IP地址、网关、DNS,dhcp4设置为false,最后一行添加:renderer: networkd。注意缩进对齐,并且每个冒号后面要跟一个空格,否则会有问题。设置好后保存退出。</p>
<p>步骤4:</p>
<p>输入命令netplan apply使改动生效,然后reboot重启系统,就配置好了。</p>
<p class="maodian"></p><h3>方法二</h3>
<p>有时候根据方法一操作,仍然不能成功设置静态IP,原因一般是系统里面安装过ifupdown命令。这个命令会根据/etc/network/interfaces这个文件里的配置信息来设置网卡,会覆盖掉上面方法一中的设置,导致不起作用。这时候我们只需修改/etc/network/interfaces这个文件即可。</p>
<p>步骤1:</p>
<p>输入sudo vi /etc/network/interfaces,打开这个文件:</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250701/2-250F112005Ac.png" /></p>
<p>参照上图进行填写,其中网卡名称、静态IP地址、网关需要根据实际进行填写,填写好后保存退出。</p>
<p>步骤2:</p>
<p>reboot重启系统,系统重新起来后就是用的设置的静态IP地址了。</p>
<p>以上就是Ubuntu如何进行网络配置(桥接、NAT网络)的详细内容,更多相关资料请阅读琼殿技术社区其它文章!</p>
頁: [1]
查看完整版本: Ubuntu进行网络配置的具体教程(桥接、NAT网络)