小友友 發表於 2020-3-12 17:24:00

树莓派/Debian 固定 IP

<h2 id="一前言">一、前言</h2>
<p>树莓派默认为自动获取IP地址,所以在更换网络环境后ip地址会发生变化,从而影响到访问Web服务器,在这里解决固定 IP 问题。在这里以 Debian 10.3 虚拟机示例,同样都是在全控制台、无桌面环境下进行。</p>
<h2 id="二vmware-nat-网关设置">二、Vmware NAT 网关设置</h2>
<ol>
<li><code>虚拟机页面 -&gt; 编辑 -&gt; 虚拟网络编辑器 -&gt; 选中VMnet8 NAT模式(使之高亮) -&gt; 更改设置(右下角,需管理员特权)</code></li>
<li>在新跳出的 <strong>虚拟网络编辑器</strong> 中,<code>选中VMnet8 NAT模式(使之高亮) -&gt; VMnet信息选 NAT 模式(表格下方) -&gt; NAT 设置</code></li>
<li>在跳出来的 <strong>NAT 设置</strong> 中,网关 IP 一栏输入想要的网关。点击确定。</li>
<li>在 <strong>虚拟网络编辑器</strong> 中,将 <strong>使用本地 DHCP 服务将 IP 地址分给虚拟机</strong> 一栏 <strong>取消勾选</strong> ,点击确定。</li>
<li>记住设置的网关。</li>
</ol>
<h2 id="三查看-树莓派debian-可用网卡">三、查看 树莓派/Debian 可用网卡</h2>
<p>终端输入 <code>ip add</code> 可查看可用网卡:</p>
<pre><code class="language-shell">yogile@debyogile:~$ ip add
1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    ......
    ......
    ......
    ......
2: ens33: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    ......
    ......
    ......
    ......
</code></pre>
<ul>
<li>第 1 个 <strong>"lo"</strong> 代表:127.0.0.1 ,即 localhost。</li>
<li>第 2 个 <strong>"ens33"</strong> 才表示可用网卡:<strong>注意这里的网卡名称,会在后面配置文件时用到</strong> 。其名称在各系统甚至在统一系统都可能不同,比如 Ubuntu 大部分是 eth0 、eth1 等。</li>
</ul>
<h2 id="四-配置-ip-地址">四、 配置 IP 地址</h2>
<h3 id="1-sudo-vim-etcnetworkinterfaces">1. <code>sudo vim /etc/network/interfaces</code></h3>
<p>显示为:</p>
<pre><code class="language-shell"># 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
allow-hotplug ens33
iface ens33 inet dhcp
</code></pre>
<ul>
<li>这里最后一行 <code>iface ens33 inet dhcp</code> ,表示:自动使用 DHCP( 动态主机配置协议 ) 获取 IP 。</li>
</ul>
<h3 id="2-修改-etcnetworkinterfaces">2. 修改 <code>/etc/network/interfaces</code></h3>
<pre><code class="language-shell"># 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
#allow-hotplug ens33
#iface ens33 inet dhcp

auto ens33
iface ens33 inet static # 配置为静态
address 192.168.0.129 # 设置 IP
netmask 255.255.255.0 # 设置掩码
gateway 192.168.0.2 # 设置网关
</code></pre>
<ul>
<li>将原来动态获取 IP 的两条语句注释掉;</li>
<li>在后面添加最后 5 条所示语句,请自己输入想要设置的 IP ,对应掩码、网关。</li>
<li><code>:wq</code> 保存退出即可。</li>
</ul>
<h3 id="3-sudo-vim-etcresolvconf">3. <code>sudo vim /etc/resolv.conf</code></h3>
<p>显示:</p>
<pre><code class="language-shell">domain localdomain
search localdomain
nameserver xxx.xxx.xxx.xxx # 网关
</code></pre>
<ul>
<li>将其 <code>nameserver</code> 后的字符串改成你在 <code>/etc/network/interfaces</code> 中的网关,两者保持一致。</li>
</ul>
<h2 id="五重启网络服务">五、重启网络服务</h2>
<pre><code class="language-shell">sudo service networking restart #重启网络
或者
sudo /etc/init.d/networking restart
</code></pre><br><br>
来源:https://www.cnblogs.com/Yogile/p/12470585.html
頁: [1]
查看完整版本: 树莓派/Debian 固定 IP