有时你也许想要给一个网卡多个地址。你该怎么做呢?另外买一个网卡来分配地址?在小型网络中其实不用这么做。我们现在可以在CentOS/RHEL 7中给一个网卡分配多个ip地址。想知道怎么做么?好的,跟随我,这并不难。
首先,让我们找到网卡的IP地址。在我的CentOS 7服务器中,我只使用了一个网卡。
用root特权运行下面的命令:
<span class="hljs-attribute">ip</span> addr
示例输出:
<span class="hljs-number">1</span>: <span class="hljs-string">lo:</span> <LOOPBACK,UP,LOWER_UP> mtu <span class="hljs-number">65536</span> qdisc noqueue state UNKNOWN
link/loopback <span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span> brd <span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>
inet <span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>/<span class="hljs-number">8</span> scope host lo
valid_lft forever preferred_lft forever
<span class="hljs-string">inet6 :</span>:<span class="hljs-number">1</span>/<span class="hljs-number">128</span> scope host
valid_lft forever preferred_lft forever
<span class="hljs-number">2</span>: <span class="hljs-string">enp0s3:</span> <BROADCAST,MULTICAST,UP,LOWER_UP> mtu <span class="hljs-number">1500</span> qdisc pfifo_fast state UP qlen <span class="hljs-number">1000</span>
link/ether <span class="hljs-number">08</span>:<span class="hljs-number">00</span>:<span class="hljs-number">27</span>:<span class="hljs-number">80</span>:<span class="hljs-number">63</span>:<span class="hljs-number">19</span> brd <span class="hljs-string">ff:</span><span class="hljs-string">ff:</span><span class="hljs-string">ff:</span><span class="hljs-string">ff:</span><span class="hljs-string">ff:</span>ff
inet <span class="hljs-number">192.168</span><span class="hljs-number">.1</span><span class="hljs-number">.150</span>/<span class="hljs-number">24</span> brd <span class="hljs-number">192.168</span><span class="hljs-number">.1</span><span class="hljs-number">.255</span> scope global enp0s3
valid_lft forever preferred_lft forever
如上所见,我的网卡名是enp0s3,ip地址是192.168.1.150。
如你所知,网卡的配置文件存储在 /etc/sysconfig/network-scripts/ 目录下。每个网卡的详细内容将会以不同的名字存储,比如 ifcfg-enp0s3 。
让我们看下 ifcfg-enp0s3 的细节。
cat <span class="hljs-regexp">/etc/</span>sysconfig<span class="hljs-regexp">/network-scripts/i</span>fcfg-enp0s3
示例输出:
<span class="hljs-attr">TYPE</span>=<span class="hljs-string">"Ethernet"</span>
<span class="hljs-attr">BOOTPROTO</span>=<span class="hljs-string">"none"</span>
<span class="hljs-attr">DEFROUTE</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV4_FAILURE_FATAL</span>=<span class="hljs-string">"no"</span>
<span class="hljs-attr">IPV6INIT</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV6_AUTOCONF</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV6_DEFROUTE</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV6_FAILURE_FATAL</span>=<span class="hljs-string">"no"</span>
<span class="hljs-attr">NAME</span>=<span class="hljs-string">"enp0s3"</span>
<span class="hljs-attr">UUID</span>=<span class="hljs-string">"e9f9caef-cb9e-4a19-aace-767c6ee6f849"</span>
<span class="hljs-attr">ONBOOT</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">HWADDR</span>=<span class="hljs-string">"08:00:27:80:63:19"</span>
<span class="hljs-attr">IPADDR0</span>=<span class="hljs-string">"192.168.1.150"</span>
<span class="hljs-attr">PREFIX0</span>=<span class="hljs-string">"24"</span>
<span class="hljs-attr">GATEWAY0</span>=<span class="hljs-string">"192.168.1.1"</span>
<span class="hljs-attr">DNS1</span>=<span class="hljs-string">"192.168.1.1"</span>
<span class="hljs-attr">IPV6_PEERDNS</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV6_PEERROUTES</span>=<span class="hljs-string">"yes"</span>
好的,现在我们将在相同的子网中分配多个地址了。
编辑文件 /etc/sysconfig/network-scripts/ifcfg-enp0s3 :
vi <span class="hljs-regexp">/etc/</span>sysconfig<span class="hljs-regexp">/network-scripts/i</span>fcfg-enp0s3
像下面那样加入额外的IP地址。
<span class="hljs-attr">TYPE</span>=<span class="hljs-string">"Ethernet"</span>
<span class="hljs-attr">BOOTPROTO</span>=<span class="hljs-string">"none"</span>
<span class="hljs-attr">DEFROUTE</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV4_FAILURE_FATAL</span>=<span class="hljs-string">"no"</span>
<span class="hljs-attr">IPV6INIT</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV6_AUTOCONF</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV6_DEFROUTE</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV6_FAILURE_FATAL</span>=<span class="hljs-string">"no"</span>
<span class="hljs-attr">NAME</span>=<span class="hljs-string">"enp0s3"</span>
<span class="hljs-attr">UUID</span>=<span class="hljs-string">"933cdc9b-b383-4ddd-b219-5a72c69c9cf0"</span>
<span class="hljs-attr">ONBOOT</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">HWADDR</span>=<span class="hljs-string">"08:00:27:3F:AB:68"</span>
<span class="hljs-attr">IPADDR0</span>=<span class="hljs-string">"192.168.1.150"</span>
<span class="hljs-attr">IPADDR1</span>=<span class="hljs-string">"192.168.1.151"</span>
<span class="hljs-attr">IPADDR2</span>=<span class="hljs-string">"192.168.1.152"</span>
<span class="hljs-attr">PREFIX0</span>=<span class="hljs-string">"24"</span>
<span class="hljs-attr">GATEWAY0</span>=<span class="hljs-string">"192.168.1.1"</span>
<span class="hljs-attr">DNS1</span>=<span class="hljs-string">"192.168.1.1"</span>
<span class="hljs-attr">IPV6_PEERDNS</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV6_PEERROUTES</span>=<span class="hljs-string">"yes"</span>
如你所见,我已经加了两个IP地址: IPADDR1=”192.168.1.151″ & IPADDR2=”192.168.1.152″
类似地,你可以加入更多的ip地址。
最后,保存并退出文件。重启网络服务来使更改生效。
<span class="hljs-attribute">systemctl</span> restart network
现在,让我们检查是否已经加入了ip地址。
<span class="hljs-attribute">ip</span> addr
示例输出:
: lo: <LOOPBACK,UP,LOWER_UP> mtu <span class="hljs-number">65536</span> qdisc noqueue state UNKNOWN
link/loopback <span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span> brd <span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span>
inet <span class="hljs-number">127</span><span class="hljs-variable">.0</span><span class="hljs-variable">.0</span><span class="hljs-variable">.1</span>/<span class="hljs-number">8</span> scope host lo
valid_lft <span class="hljs-keyword">forever</span> preferred_lft <span class="hljs-keyword">forever</span>
inet6 ::<span class="hljs-number">1</span>/<span class="hljs-number">128</span> scope host
valid_lft <span class="hljs-keyword">forever</span> preferred_lft <span class="hljs-keyword">forever</span>
<span class="hljs-number">2</span>: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu <span class="hljs-number">1500</span> qdisc pfifo_fast state UP qlen <span class="hljs-number">1000</span>
link/ether <span class="hljs-number">08</span>:<span class="hljs-number">00</span>:<span class="hljs-number">27</span>:<span class="hljs-number">3</span>f:ab:<span class="hljs-number">68</span> brd ff:ff:ff:ff:ff:ff
inet <span class="hljs-number">192</span><span class="hljs-variable">.168</span><span class="hljs-variable">.1</span><span class="hljs-variable">.150</span>/<span class="hljs-number">24</span> brd <span class="hljs-number">192</span><span class="hljs-variable">.168</span><span class="hljs-variable">.1</span><span class="hljs-variable">.255</span> scope <span class="hljs-keyword">global</span> enp0s3
valid_lft <span class="hljs-keyword">forever</span> preferred_lft <span class="hljs-keyword">forever</span>
inet <span class="hljs-number">192</span><span class="hljs-variable">.168</span><span class="hljs-variable">.1</span><span class="hljs-variable">.151</span>/<span class="hljs-number">24</span> brd <span class="hljs-number">192</span><span class="hljs-variable">.168</span><span class="hljs-variable">.1</span><span class="hljs-variable">.255</span> scope <span class="hljs-keyword">global</span> secondary enp0s3
valid_lft <span class="hljs-keyword">forever</span> preferred_lft <span class="hljs-keyword">forever</span>
inet <span class="hljs-number">192</span><span class="hljs-variable">.168</span><span class="hljs-variable">.1</span><span class="hljs-variable">.152</span>/<span class="hljs-number">24</span> brd <span class="hljs-number">192</span><span class="hljs-variable">.168</span><span class="hljs-variable">.1</span><span class="hljs-variable">.255</span> scope <span class="hljs-keyword">global</span> secondary enp0s3
valid_lft <span class="hljs-keyword">forever</span> preferred_lft <span class="hljs-keyword">forever</span>
inet6 fe80::a00:<span class="hljs-number">27</span>ff:fe3f:ab68/<span class="hljs-number">64</span> scope link
valid_lft <span class="hljs-keyword">forever</span> preferred_lft <span class="hljs-keyword">forever</span>
如你所见,单个网卡已经有3个ip地址了。
让我们ping一下新增的IP地址:
<span class="hljs-selector-tag">ping</span> <span class="hljs-selector-tag">-c</span> 4 192<span class="hljs-selector-class">.168</span><span class="hljs-selector-class">.1</span><span class="hljs-selector-class">.151</span>
示例输出:
<span class="hljs-attribute">PING</span> <span class="hljs-number">192.168.1.151</span> (<span class="hljs-number">192.168.1.151</span>) <span class="hljs-number">56</span>(<span class="hljs-number">84</span>) bytes of data.
<span class="hljs-number">64</span> bytes from <span class="hljs-number">192.168.1.151</span>: icmp_seq=<span class="hljs-number">1</span> ttl=<span class="hljs-number">64</span> time=<span class="hljs-number">0</span>.<span class="hljs-number">048</span> ms
<span class="hljs-number">64</span> bytes from <span class="hljs-number">192.168.1.151</span>: icmp_seq=<span class="hljs-number">2</span> ttl=<span class="hljs-number">64</span> time=<span class="hljs-number">0</span>.<span class="hljs-number">075</span> ms
<span class="hljs-number">64</span> bytes from <span class="hljs-number">192.168.1.151</span>: icmp_seq=<span class="hljs-number">3</span> ttl=<span class="hljs-number">64</span> time=<span class="hljs-number">0</span>.<span class="hljs-number">077</span> ms
<span class="hljs-number">64</span> bytes from <span class="hljs-number">192.168.1.151</span>: icmp_seq=<span class="hljs-number">4</span> ttl=<span class="hljs-number">64</span> time=<span class="hljs-number">0</span>.<span class="hljs-number">077</span> ms
--- <span class="hljs-number">192.168.1.151</span> ping statistics ---
<span class="hljs-number">4</span> packets transmitted, <span class="hljs-number">4</span> received, <span class="hljs-number">0</span>% packet loss, time <span class="hljs-number">2999ms</span>
rtt min/avg/max/mdev = <span class="hljs-number">0</span>.<span class="hljs-number">048</span>/<span class="hljs-number">0</span>.<span class="hljs-number">069</span>/<span class="hljs-number">0</span>.<span class="hljs-number">077</span>/<span class="hljs-number">0</span>.<span class="hljs-number">013</span> ms
<span class="hljs-selector-tag">ping</span> <span class="hljs-selector-tag">-c</span> 4 192<span class="hljs-selector-class">.168</span><span class="hljs-selector-class">.1</span><span class="hljs-selector-class">.152</span>
示例输出:
<span class="hljs-attribute">PING</span> <span class="hljs-number">192.168.1.152</span> (<span class="hljs-number">192.168.1.152</span>) <span class="hljs-number">56</span>(<span class="hljs-number">84</span>) bytes of data.
<span class="hljs-number">64</span> bytes from <span class="hljs-number">192.168.1.152</span>: icmp_seq=<span class="hljs-number">1</span> ttl=<span class="hljs-number">64</span> time=<span class="hljs-number">0</span>.<span class="hljs-number">034</span> ms
<span class="hljs-number">64</span> bytes from <span class="hljs-number">192.168.1.152</span>: icmp_seq=<span class="hljs-number">2</span> ttl=<span class="hljs-number">64</span> time=<span class="hljs-number">0</span>.<span class="hljs-number">075</span> ms
<span class="hljs-number">64</span> bytes from <span class="hljs-number">192.168.1.152</span>: icmp_seq=<span class="hljs-number">3</span> ttl=<span class="hljs-number">64</span> time=<span class="hljs-number">0</span>.<span class="hljs-number">073</span> ms
<span class="hljs-number">64</span> bytes from <span class="hljs-number">192.168.1.152</span>: icmp_seq=<span class="hljs-number">4</span> ttl=<span class="hljs-number">64</span> time=<span class="hljs-number">0</span>.<span class="hljs-number">075</span> ms
--- <span class="hljs-number">192.168.1.152</span> ping statistics ---
<span class="hljs-number">4</span> packets transmitted, <span class="hljs-number">4</span> received, <span class="hljs-number">0</span>% packet loss, time <span class="hljs-number">2999ms</span>
rtt min/avg/max/mdev = <span class="hljs-number">0</span>.<span class="hljs-number">034</span>/<span class="hljs-number">0</span>.<span class="hljs-number">064</span>/<span class="hljs-number">0</span>.<span class="hljs-number">075</span>/<span class="hljs-number">0</span>.<span class="hljs-number">018</span> ms
如果你想要使用 不同的子网 ,你要改变 PREFIX0=24 成不同的子网,比如 PREFIX1=16 。
比如,我想要添加一个A类地址(* 比如10.0.0.1 )到我的网卡中。
<span class="hljs-attr">TYPE</span>=<span class="hljs-string">"Ethernet"</span>
<span class="hljs-attr">BOOTPROTO</span>=<span class="hljs-string">"none"</span>
<span class="hljs-attr">DEFROUTE</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV4_FAILURE_FATAL</span>=<span class="hljs-string">"no"</span>
<span class="hljs-attr">IPV6INIT</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV6_AUTOCONF</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV6_DEFROUTE</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV6_FAILURE_FATAL</span>=<span class="hljs-string">"no"</span>
<span class="hljs-attr">NAME</span>=<span class="hljs-string">"enp0s3"</span>
<span class="hljs-attr">UUID</span>=<span class="hljs-string">"933cdc9b-b383-4ddd-b219-5a72c69c9cf0"</span>
<span class="hljs-attr">ONBOOT</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">HWADDR</span>=<span class="hljs-string">"08:00:27:3F:AB:68"</span>
<span class="hljs-attr">IPADDR0</span>=<span class="hljs-string">"192.168.1.150"</span>
<span class="hljs-attr">IPADDR1</span>=<span class="hljs-string">"192.168.1.151"</span>
<span class="hljs-attr">IPADDR2</span>=<span class="hljs-string">"192.168.1.152"</span>
<span class="hljs-attr">IPADDR3</span>=<span class="hljs-string">"10.0.0.1"</span>
<span class="hljs-attr">PREFIX0</span>=<span class="hljs-string">"24"</span>
<span class="hljs-attr">PREFIX1</span>=<span class="hljs-number">16</span>
<span class="hljs-attr">GATEWAY0</span>=<span class="hljs-string">"192.168.1.1"</span>
<span class="hljs-attr">DNS1</span>=<span class="hljs-string">"192.168.1.1"</span>
<span class="hljs-attr">IPV6_PEERDNS</span>=<span class="hljs-string">"yes"</span>
<span class="hljs-attr">IPV6_PEERROUTES</span>=<span class="hljs-string">"yes"</span>
你可以看到我已经添加一个A类地址(10.0.0.1)并且前缀是16。
保存并退出文件。重启网络服务,接着,ping新增的地址:
<span class="hljs-selector-tag">ping</span> <span class="hljs-selector-tag">-c</span> 4 10<span class="hljs-selector-class">.0</span><span class="hljs-selector-class">.0</span><span class="hljs-selector-class">.1</span>
示例输出:
<span class="hljs-attribute">PING</span> <span class="hljs-number">10.0.0.1</span> (<span class="hljs-number">10.0.0.1</span>) <span class="hljs-number">56</span>(<span class="hljs-number">84</span>) bytes of data.
<span class="hljs-number">64</span> bytes from <span class="hljs-number">10.0.0.1</span>: icmp_seq=<span class="hljs-number">1</span> ttl=<span class="hljs-number">64</span> time=<span class="hljs-number">0</span>.<span class="hljs-number">097</span> ms
<span class="hljs-number">64</span> bytes from <span class="hljs-number">10.0.0.1</span>: icmp_seq=<span class="hljs-number">2</span> ttl=<span class="hljs-number">64</span> time=<span class="hljs-number">0</span>.<span class="hljs-number">073</span> ms
<span class="hljs-number">64</span> bytes from <span class="hljs-number">10.0.0.1</span>: icmp_seq=<span class="hljs-number">3</span> ttl=<span class="hljs-number">64</span> time=<span class="hljs-number">0</span>.<span class="hljs-number">074</span> ms
<span class="hljs-number">64</span> bytes from <span class="hljs-number">10.0.0.1</span>: icmp_seq=<span class="hljs-number">4</span> ttl=<span class="hljs-number">64</span> time=<span class="hljs-number">0</span>.<span class="hljs-number">075</span> ms
--- <span class="hljs-number">10.0.0.1</span> ping statistics ---
<span class="hljs-number">4</span> packets transmitted, <span class="hljs-number">4</span> received, <span class="hljs-number">0</span>% packet loss, time <span class="hljs-number">3000ms</span>
rtt min/avg/max/mdev = <span class="hljs-number">0</span>.<span class="hljs-number">073</span>/<span class="hljs-number">0</span>.<span class="hljs-number">079</span>/<span class="hljs-number">0</span>.<span class="hljs-number">097</span>/<span class="hljs-number">0</span>.<span class="hljs-number">014</span> ms
相似地,你可以添加不同的网关。
centos6.7 添加方法
执行命令:
vi /etc/sysconfig/network-scripts/ifcfg-eth0:0
输入以下内容
DEVICE=eth0:0
IPADDR=输入你自己新增加的ip
按ESC键跳到命令模式,输入:wq,回车
重启网络
service network restart
查看网络
ifconfig