王家大小姐 發表於 2025-6-26 00:00:00

CentOS7网络配置超详细ip和网关设置教程

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>CentOS7 网络配置</li><li>二、对NAT模式了解</li><li>三、虚拟网络配置</li><li>Centos6(网络文件配置)</li><li>Centos7(网络文件配置)</li><li>扩展CentOS7(ifconfig报错时)</li><li>其他配置(更改计算机名称、关闭防火墙、hosts解析配置)</li><li>关闭防火墙CentO6设置</li><li>关闭防火墙CentO7设置</li><li>添加hosts解析配置的IP地址</li><li>CentOS 7 设置静态 IP<ul class="second_class_ul"><li>一、操作环境</li><li>二、设置静态 IP</li><li>三、重启网络</li></ul></li></ul></div><p class="maodian"></p><h2>CentOS7 网络配置</h2>
<p>一、在虚拟机中安装CentOS操作系统</p>
<p>安装好虚拟机</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-25062612022C52.png" /></p>
<p>创建新的虚拟机,选择自定义(高级),点击下一步</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-25062612022A42.png" /></p>
<p>虚拟机硬件兼容性默认,浏览需要安装的CentOS6.5镜像文件</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-25062612022C43.png" /></p>
<p>自定义用户名和密码(用于登录)</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-25062612022M35.png" /></p>
<p>设置虚拟机名称和存储路径</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-25062612022W14.png" /></p>
<p>处理器设置默认(后面可以修改),内存设为1GB</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-25062612022S61.png" /></p>
<p>网络类型选择NAT模式</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-25062612022Sa.png" /></p>
<p>选择创建新虚拟磁盘</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-250626120229119.png" /></p>
<p>最大磁盘大小设为20GB,然后将虚拟磁盘存储为单个文件</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-25062612022b57.png" /></p>
<p>将创建好的磁盘文件存储在虚拟机指定目录下</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-2506261202295G.png" /></p>
<p>虚拟机创建完成</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-250626120230493.png" /></p>
<p class="maodian"></p><h2>二、对NAT模式了解</h2>
<p>1、<strong>NAT即为网络地址转换,通常他的名称为VMnet8,通过NAT的连接方式可以使得虚拟机和真实机的网卡在不同的网段中,从而实现联网</strong>。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-250626120230348.png" /></p>
<p>**VMent8(可以理解为是交换机)**在虚拟机里创建的三台虚拟电脑可以理解为都要经过虚拟交换机VMent8,VMent8在通过宿主机的网卡通过LAN(局域网)在连接到互联网。(区别是IP地址可以选取在VMent8范围之内的任意ip,和可以在不同ip也可以访问互联网)</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-250626120231Z1.jpg" /></p>
<p><strong>bridge即为桥接,通常他的名称为VMnet0,使用桥接的方法使得自己的虚拟机和自己的真实机网卡在同一网段,从而实现联网。</strong>(必须与宿主IP同一个网段内)</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-250626120231Z0.jpg" /></p>
<p class="maodian"></p><h2>三、虚拟网络配置</h2>
<p>不需要更改,也可以按照自己的需求更改</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-250626120232G6.png" /></p>
<p><strong>尝试使用宿主电脑cmd(ping)是否能通</strong></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-250626120232952.png" /></p>
<p>开始设置配置虚拟主机IP</p>
<p class="maodian"></p><h2>Centos6(网络文件配置)</h2>
<p>切换文件目录</p>
<div class="dxycode"><pre class="brush:bash;">cd /etc/sysconfig/network-scripts/
ls</pre></div>
<p>备份文件</p>
<div class="dxycode"><pre class="brush:bash;">cp -r ifcfg-eth0 ifcfg-eth0.beifen
ls</pre></div>
<p>更改文件</p>
<div class="dxycode"><pre class="brush:bash;">vi ifcfg-eth0
ls</pre></div>
<p>找到自己现在所使用的NAT模式 在这里我的是eth0并打开配置</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="请添加图片描述" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-250626120234304.png" /></p>
<p>开始配置</p>
<div class="dxycode"><pre class="brush:bash;">vi ifcfg-eth0</pre></div>
<p><strong>上图中删除了MAC地址行和UUID行,同时将ONBOOT修改为yes</strong>,随系统启动,BOOTPROTO修改为static,表示是静态IP地址配置,IPADDR表示配置的IP地址,NETMASK为子网掩码,GATEWAY为网关地址,DNS1表示是DNS解析地址,DNS2是备用DNS解析地址。(<strong>CentOS7网卡是默认的ifcfg-ens33或者其他名字系统版本会有所更变里面内容不变即可</strong>)<br />按ESC键退出编辑模式,按&ldquo;:wq&rdquo;保存退出。</p>
<div class="dxycode"><pre class="brush:plain;">代码示例:
DEVICE=eth0            #设定网卡的名称,要跟文件名称对应 #
TYPE=Ethernet
NM_CONTROLLED=yes      #表示启用网络控制,即修改后可以实时生效,修改后无需要重启网卡立即生效。
ONBOOT=yes                           #是否在开机的的时候启动网卡#
BOOTPROTO=static       #启动的时候的 IP 取得的协议,这里是固定的,如果是动态主机的话,要改成 dhcp 才行#
IPADDR=192.168.20.25   #IP 地址#   必设置 (变动)
NETMASK=255.255.255.0#子网掩码#必设置
GATEWAY=192.168.20.2   #网关地址# 必须设置(变动)
DNS1=192.168.20.2   必设置要与虚拟机VMnet8(IPv4 WINS服务器相同) DNS1表示是DNS解析地址
DNS2=144.144.144.144 DNS2是备用DNS解析地址</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-250626120234405.png" /></p>
<p>在命令提示符,输入:&ldquo;service network restart&rdquo;重启网络。</p>
<div class="dxycode"><pre class="brush:bash;">service network restart</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-250626120234159.png" /></p>
<p>查看主机CentOS7 ip地址 前先查看网络是否连接</p>
<div class="dxycode"><pre class="brush:bash;">ifconfig</pre></div>
<p>有网连接状态</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="请添加图片描述" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-250626120235U3.png" /></p>
<p>然后输入:&ldquo;ping www.baidu.com&rdquo;ping以下百度网址,表示网络配置成功。</p>
<div class="dxycode"><pre class="brush:bash;">ping www.baidu.com</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-250626120235536.png" /></p>
<p>出现确定表示静态IP地址完成搭建。</p>
<p class="maodian"></p><h2>Centos7(网络文件配置)</h2>
<p><strong>切换文件目录(跟Centos6一样不变)</strong><br />cp 命令是在复制一份,名字不要一样会覆盖掉原来的文件</p>
<div class="dxycode"><pre class="brush:bash;">cp -r ifcfg-ets33 ifcfg-ets33.beifen</pre></div>
<p>更改文件网卡文件有所更变</p>
<div class="dxycode"><pre class="brush:bash;">vi ifcfg-ets33</pre></div>
<p>Centos7需要加引号列如TYPE=&ldquo;Ethernet&rdquo;</p>
<div class="dxycode"><pre class="brush:plain;">代码示例:
DEVICE="eth0"            #设定网卡的名称,要跟文件名称对应 #
TYPE="Ethernet"
NM_CONTROLLED="yes"      #表示启用网络控制,即修改后可以实时生效,修改后无需要重启网卡立即生效。
ONBOOT="yes"                           #是否在开机的的时候启动网卡#
BOOTPROTO="static"       #启动的时候的 IP 取得的协议,这里是固定的,如果是动态主机的话,要改成 dhcp 才行#
IPADDR="192.168.20.25"   #IP 地址#   必设置 (变动)
NETMASK="255.255.255.0"#子网掩码#必设置
GATEWAY="192.168.20.2"   #网关地址# 必须设置(变动)
DNS1="192.168.20.2"   必设置要与虚拟机VMnet8(IPv4 WINS服务器相同) DNS1表示是DNS解析地址
DNS2="144.144.144.144" DNS2是备用DNS解析地址</pre></div>
<p>查看ip命令更变</p>
<div class="dxycode"><pre class="brush:bash;">ip addr</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-250626120235634.png" /></p>
<p class="maodian"></p><h2>扩展CentOS7(ifconfig报错时)</h2>
<p>执行命令: ifconfig</p>
<div class="dxycode"><pre class="brush:bash;">ifconfig</pre></div>
<p>错误截图:</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-2506261202363K.png" /></p>
<p>解决方案使用 yum install net-tools</p>
<div class="dxycode"><pre class="brush:bash;">yum install net-tools</pre></div>
<p>提示后输入(Y两次)</p>
<div class="dxycode"><pre class="brush:bash;"># yum install net-tools
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: mirrors.huaweicloud.com
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
base                                                                                                | 3.6 kB00:00:00   
extras                                                                                                | 2.9 kB00:00:00   
updates                                                                                             | 2.9 kB00:00:00   
(1/4): extras/7/x86_64/primary_db                                                                     | 250 kB00:00:00   
(2/4): base/7/x86_64/group_gz                                                                         | 153 kB00:00:00   
(3/4): base/7/x86_64/primary_db                                                                     | 6.1 MB00:00:09   
(4/4): updates/7/x86_64/primary_db                                                                  |24 MB00:00:12   
Resolving Dependencies
--&gt; Running transaction check
---&gt; Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
--&gt; Finished Dependency Resolution
Dependencies Resolved
=============================================================================================================================
Package                  Arch                  Version                                    Repository             Size
=============================================================================================================================
Installing:
net-tools                  x86_64                  2.0-0.25.20131004git.el7                     base                  306 k
Transaction Summary
=============================================================================================================================
Install1 Package
Total download size: 306 k
Installed size: 917 k
Is this ok : y</pre></div>
<p>安装完成后在输入ifconig</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-25062612023A55.png" /></p>
<p>重启网络命令不变</p>
<div class="dxycode"><pre class="brush:bash;">service network restart</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-25062612023N11.png" /></p>
<p>测试不变</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-25062612023GO.png" /></p>
<p class="maodian"></p><h2>其他配置(更改计算机名称、关闭防火墙、hosts解析配置)</h2>
<p>修改vi内容命令复制、粘贴、替换、撤销(常用命令)。</p>
<p><strong>复制粘贴退出编辑按ESC 在按两次YY(复制) 后在按 P (粘贴)</strong></p>
<p><strong>在按 R (替换) uu(撤销)</strong></p>
<p>使用vi编辑&ldquo;/etc/sysconfig/network&rdquo;文件,设置计算机名称。</p>
<div class="dxycode"><pre class="brush:bash;">vi /etc/sysconfig/network</pre></div>
<p><strong>添加HETWORKING=yes、HOSTNAME=cheshi 。</strong></p>
<div class="dxycode"><pre class="brush:plain;">HETWORKING=yes       #表示系统是否使用网络,一般设置为yes。如果设为no,则不能使用网络。
HOSTNAME=cheshi      #设置本机的主机名,这里设置的主机名要和/etc/hosts中设置的主机名对应
GATEWAY=192.168.1.1#设置本机连接的网关的IP地址。(需要设置不需要则不设置)</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-25062612023Y14.png" /></p>
<p><strong>删除70-persistent-net.rules文件</strong></p>
<div class="dxycode"><pre class="brush:bash;">rm -rf /etc/udev/rules.d/70-persistent-net.rules</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-25062612023W10.png" /></p>
<p class="maodian"></p><h2>关闭防火墙CentO6设置</h2>
<div class="dxycode"><pre class="brush:bash;">service iptables stop
chkconfig iptables off</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-2506261202393D.png" /></p>
<p class="maodian"></p><h2>关闭防火墙CentO7设置</h2>
<p><strong>查看防火墙状态</strong></p>
<div class="dxycode"><pre class="brush:bash;">firewall-cmd --state</pre></div>
<p><strong>停止firewall</strong></p>
<div class="dxycode"><pre class="brush:bash;">systemctl stop firewalld.service</pre></div>
<p><strong>禁止firewall开机启动</strong></p>
<div class="dxycode"><pre class="brush:bash;">systemctl disable firewalld.service</pre></div>
<p><strong>重启防火墙</strong></p>
<div class="dxycode"><pre class="brush:bash;">firewall-cmd --reloadl</pre></div>
<p class="maodian"></p><h2>添加hosts解析配置的IP地址</h2>
<div class="dxycode"><pre class="brush:bash;">vi /etc/hosts</pre></div>
<p>此时域名cheshi1、cheshi2、cheshi3、cheshi4、cheshi5会被解析为配置的IP地址。<br />保存退出。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-2506261202394A.png" /></p>
<p>此时域名cheshi1、cheshi2、cheshi3、cheshi4、cheshi5会被解析为配置的IP地址。</p>
<p>保存退出。</p>
<p>在ping cheshi的时候,cheshi解析为了192.168.20.55。</p>
<div class="dxycode"><pre class="brush:bash;">ping cheshi</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250626/2-25062612023aS.png" /></p>
<p><strong>设置完毕重启电脑</strong>并拍摄快照做保存习惯</p>
<div class="dxycode"><pre class="brush:bash;">reboot -f</pre></div>
<p class="maodian"></p><h2>CentOS 7 设置静态 IP</h2>
<p class="maodian"></p><h3>一、操作环境</h3>
<p>操作环境影响不大,只要是<strong>「红帽系列」</strong>的应该都可以采用本文的设置方法,例如:fedora、Redhat。&nbsp;</p>
<div class="dxycode"><pre class="brush:bash;">$ cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
$</pre></div>
<p class="maodian"></p><h3>二、设置静态 IP</h3>
<p>修改文件 /etc/sysconfig/network-scripts/ifcfg-ens33 文件,如下所示。修改的配置文件 ifcfg-ens33 要对应系统上正在使用的网卡,可以使用 ifconfig 查看下当前的 IP 对应的是哪个网卡。</p>
<div class="dxycode"><pre class="brush:plain;">TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static # 从 dhcp 修改为 static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=2de148e1-945e-4744-bc6d-18394e985967
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.231.152 # 静态 IP
NETMASK=255.255.255.0 # 子网掩码
GATEWAY=192.168.231.2 # 网关
DNS1=192.168.231.2# DNS</pre></div>
<p>如上所示,需要设置的字段有五个,其中,IPADDR 是你想设置的静态 IP,NETMASK 为子网掩码,可以通过如下方式查看。</p>
<div class="dxycode"><pre>$ ifconfig
ens33: flags=4163mtu 1500
      inet 192.168.231.152netmask 255.255.255.0broadcast 192.168.231.255
      inet6 fe80::e3a3:533b:4112:90d5prefixlen 64scopeid 0x20
      ether 00:0c:29:ca:f5:28txqueuelen 1000(Ethernet)
      RX packets 3507bytes 288058 (281.3 KiB)
      RX errors 0dropped 0overruns 0frame 0
      TX packets 1958bytes 256066 (250.0 KiB)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0
lo: flags=73mtu 65536
      inet 127.0.0.1netmask 255.0.0.0
      inet6 ::1prefixlen 128scopeid 0x10
      looptxqueuelen 1000(Local Loopback)
      RX packets 247120bytes 90060437 (85.8 MiB)
      RX errors 0dropped 0overruns 0frame 0
      TX packets 247120bytes 90060437 (85.8 MiB)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0
virbr0: flags=4099mtu 1500
      inet 192.168.122.1netmask 255.255.255.0broadcast 192.168.122.255
      ether 52:54:00:84:91:8btxqueuelen 1000(Ethernet)
      RX packets 0bytes 0 (0.0 B)
      RX errors 0dropped 0overruns 0frame 0
      TX packets 0bytes 0 (0.0 B)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0
$</pre></div>
<p>在网卡 ens33 信息中,netmask 255.255.255.0 即为子网掩码。</p>
<p>GATWAY 字段设置的值的查询方式如下所示。</p>
<div class="dxycode"><pre class="brush:bash;">$ netstat -rn
Kernel IP routing table
Destination   Gateway         Genmask         Flags   MSS Windowirtt Iface
0.0.0.0         192.168.231.2   0.0.0.0         UG      0 0          0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0
192.168.231.0   0.0.0.0         255.255.255.0   U         0 0          0 ens33
$</pre></div>
<p>如上所示,Gateway&nbsp;列下面即为网关地址。</p>
<p>DNS1 字段值查询方式如下所示。</p>
<div class="dxycode"><pre class="brush:bash;">$ cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.231.2
$</pre></div>
<p class="maodian"></p><h3>三、重启网络</h3>
<p>当前网络未加载当前修改的配置,所以需要重启网络重新加载下配置,执行如下命令。&nbsp;</p>
<div class="dxycode"><pre class="brush:bash;">$ systemctl restart network
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ===
$</pre></div>
<p>重启网络后执行 ifconfig 或 ipaddr 命令即可看到设置的静态 IP啦~</p>
<p>以上就是CentOS7网络配置超详细ip、网关设置的详细内容,更多相关资料请阅读琼殿技术社区其它文章!</p>
頁: [1]
查看完整版本: CentOS7网络配置超详细ip和网关设置教程