天天看點

linux下網卡配置的使用方法

使用環境為CentOS6.6版本;

1、檢視網卡IP位址    ifconfig

網卡配置檔案:/etc/sysconfig/network-scripts/ifcfg-eth0 

如果有多個網卡的話為eth1、eth2 。。。

如果linux有多個網卡,隻想重新開機某一個網卡使用指令# ifdown eth0; ifup eth0

ifdown 停掉網卡,ifup 啟動網卡;

<a href="http://s3.51cto.com/wyfs02/M01/6B/A0/wKioL1Uze1WRD421AABgTjCyHzg514.jpg" target="_blank"></a>

如果我們遠端登入伺服器,使用ifdown eth0 停掉網卡後,後面的指令不會被運作,會導緻斷網無法連接配接伺服器。是以請盡量使用 service network restart 重新開機網卡;

2、給一個網卡設定多個IP

1

2

<code>[root@yonglinux ~]</code><code># cd /etc/sysconfig/network-scripts/</code>

<code>[root@yonglinux network-scripts]</code><code># cp ifcfg-eth0 ifcfg-eth0\:1</code>

編輯ifcfg-eth0:1 這個配置檔案,更改裡面的DEVICE=eth0:1 設定新的IP位址192.168.20.30 ;

3

4

5

6

7

8

9

10

11

<code>[root@yonglinux network-scripts]</code><code># vim ifcfg-eth0:1</code>

<code>DEVICE=eth0:1</code>

<code>HWADDR=00:0C:29:43:3D:32</code>

<code>TYPE=Ethernet</code>

<code>UUID=0b1f4512-cefa-4a9e-ae85-adb2ac2a9903</code>

<code>ONBOOT=</code><code>yes</code>

<code>NM_CONTROLLED=</code><code>yes</code>

<code>BOOTPROTO=static</code>

<code>IPADDR=192.168.20.30</code>

<code>NETMASK=255.255.255.0</code>

<code>GATEWAY=192.168.20.1</code>

儲存退出後,重新開機網卡;

<code>[root@yonglinux network-scripts]</code><code># service network restart</code>

12

13

14

<code>[root@yonglinux network-scripts]</code><code># ifconfig </code>

<code>eth0      Link encap:Ethernet  HWaddr 00:0C:29:43:3D:32  </code>

<code>          </code><code>inet addr:192.168.20.20  Bcast:192.168.20.255  Mask:255.255.255.0</code>

<code>          </code><code>inet6 addr: fe80::20c:29ff:fe43:3d32</code><code>/64</code> <code>Scope:Link</code>

<code>          </code><code>UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1</code>

<code>          </code><code>RX packets:62506 errors:0 dropped:0 overruns:0 frame:0</code>

<code>          </code><code>TX packets:82867 errors:0 dropped:0 overruns:0 carrier:0</code>

<code>          </code><code>collisions:0 txqueuelen:1000 </code>

<code>          </code><code>RX bytes:38679452 (36.8 MiB)  TX bytes:47133078 (44.9 MiB)</code>

<code>          </code><code>Interrupt:18 Base address:0x2000 </code>

<code>eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:43:3D:32  </code>

<code>          </code><code>inet addr:192.168.20.30  Bcast:192.168.20.255  Mask:255.255.255.0</code>

<code>          </code><code>Interrupt:18 Base address:0x2000</code>

可以看到多了一個IP,可以用遠端ssh通路這個192.168.20.30;

3、檢視網卡連接配接狀态   mii-tool eth0

<code>[root@yonglinux ~]</code><code># mii-tool eth0</code>

<code>SIOCGMIIPHY on </code><code>'eth0'</code> <code>failed: Operation not supported</code>

由于是虛拟機是以顯示"operation not supported",真實機器會顯示    eth0: negotiated 100baseTx-FD, link ok ;”link ok“說明網卡為連接配接狀态,如果顯示“no link“說明網卡壞掉了或沒有連接配接網線。

4、更改主機名

hostname    檢視系統的主機名;

hostname 主機名    可以更改主機名,下次登入時會更改登入提示符;這樣更改隻是暫時儲存在記憶體中,重新開機後不會生效;

<code>[root@yonglinux ~]</code><code># hostname </code>

<code>yonglinux.com</code>

<code>[root@yonglinux ~]</code><code># hostname</code>

<code>Angel</code>

<code>[root@Angel ~]</code><code># hostname </code>

永久更改主機名,需要編輯配置檔案 /etc/sysconfig/network,更改HOSTNAME,儲存退出後重新開機生效;

NETWORKING=yes

HOSTNAME=yonglinux.com

5、設定DNS

DNS為域名解析,把域名解析為ip位址;

配置檔案為/etc/resolv.conf

<code>[root@yong ~]</code><code># cat /etc/resolv.conf </code>

<code>; generated by </code><code>/sbin/dhclient-script</code>

<code>nameserver 192.168.20.2</code>

可以添加多個DNS位址,格式為:nameserver ip位址

linux下還有一個特殊的檔案 /etc/hosts 也可以解析域名

手動在裡面添加ip位址 域名位址;主要作用是臨時解析某個域名。

格式為:ip位址 域名    192.168.1.111 www.baidu.com

<code>[root@yong ~]</code><code># ping www.baidu.com</code>

<code>PING www.baidu.com (192.168.1.111) 56(84) bytes of data.</code>

幾點要注意的:

1)一個ip後面可以跟多個域名,空格分隔開,可以是幾十個甚至幾百個;

2)每行隻能有一個IP,也就是說一個域名不能對應多個IP;

3)如果有多行中出現相同的域名(ip不一樣)會按最前面出現的記錄來解析;

本文轉自 模範生 51CTO部落格,原文連結:http://blog.51cto.com/mofansheng/1635785,如需轉載請自行聯系原作者

繼續閱讀