操作系統(tǒng):Ubuntu13.04
設(shè)置靜態(tài)IP:
準(zhǔn)備:
1、查看在用的網(wǎng)卡、IP及子網(wǎng)掩碼netmask, 我的是eth0
ifconfig</p><p>eth0 Link encap:Ethernet HWaddr ec:9a:74:36:c8:8c</p><p>inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0</p><p># IP:192.168.1.100, netmask:255.255.255.0
2、查看網(wǎng)關(guān)gateway
route -n</p><p>Destination Gateway Genmask Flags Metric Ref Use Iface</p><p>0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0</p><p># gateway: 192.168.1.1</p><p># dns: 192.168.1.1 # 一般都和gateway,當(dāng)然也可設(shè)置其他dns
3、現(xiàn)在開始設(shè)置靜態(tài)ip
sudo vim /etc/network/interfaces</p><p># interfaces(5) file used by ifup(8) and ifdown(8)</p><p>auto lo</p><p>iface lo inet loopback</p><p># Set Static IP</p><p>auto eth0 # 我的網(wǎng)卡</p><p>iface eth0 inet static # 靜態(tài),與之相對的是動態(tài)(dhcp,即iface eth0 inet dhcp)</p><p>address 192.168.1.100 # 你可以設(shè)置同網(wǎng)段的可用ip, 如192.168.1.102</p><p>gateway 192.168.1.1 # 網(wǎng)關(guān)</p><p>netmask 255.255.255.0 # 子網(wǎng)掩碼</p><p>dns-nameservers 192.168.1.1 # 可設(shè)置多個(gè)dns, 也可不在這里設(shè)置,編輯/etc/resolv.conf
dns-nameservers 114.114.114.114
4、重啟網(wǎng)卡
sudo service networking restart</p><p># 或 sudo /etc/init.d/networking restart</p><p># 還是不行的話,請重啟reboot</p><p># 注意:</p><p>#當(dāng)沒有設(shè)置dns-nameservers 時(shí),需要在/etc/resolv.conf手動添加</p><p>sudo vim /etc/resolv.cof</p><p>nameserver 192.168.1.1
另外:
在相對較早的版本中,你需要設(shè)置/etc下的resolv.conf文件,并添加nameserver,這樣才可以連接互聯(lián)網(wǎng),否則只能通過ip訪問,無法解析網(wǎng)址.(nslookup 可查看網(wǎng)址ip, 當(dāng)然,你必須先有dns).
在Ubuntu12.04之后,已經(jīng)不再推薦這種方式,因?yàn)闊o論你想在該配置文件中設(shè)置什么值,重新啟動后都會被還原為初始狀態(tài)。(當(dāng)然不包括開機(jī)啟動腳本自動設(shè)置)
推薦的做法是,直接在interfaces配置文件中加入dns-nameservers <網(wǎng)關(guān)ip>這一行.