admin管理员组

文章数量:1530300

设置静态IP

#sudo vim /etc/network/interfaces

auto eth0

iface eth0 inet static

address 192.168.127.63

netmask 255.255.255.0

gateway 192.168.127.1

dns-nameservers 192.168.127.1

(网上大多设置静态ip都没有dns-nameservers,这里设置这个的原因下面有说)

 

设置动态ip

#sudo vim /etc/network/interfaces

auto eth0

iface eth0 inet dhcp

 

注:

gateway和dns-nameservers必须和电脑的一样。

若ip设置为动态ip,则我们可以不用设置dns服务器。

若ip设置为静态ip,则需要设置dns服务器,否则无法ping通外网。

 

配置dns服务器(有多种方法):
安装好Ubuntu之后设置了静态IP地址,再重启后就无法解析域名。想重新设置一下DNS,
cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
内容是一段警告:说这个文件是resolvconf程序动态创建的,不要直接手动编辑,修改将被覆盖。果不其然,修改后重启就失效了,搜索了Ubuntu下设置DNS的相关资料,总结出两个办法:


方法一 通过修改/etc/network/interfaces,在它的最后增加一句:
dns-nameservers 192.168.127.1——dns必须和电脑一样

重启网络后sudo /etc/init.d/networking restart,DNS就生效了,这时候再看/etc/resolv.conf,最下面就多了一行:
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 192.168.127.1

 

此时,在ping www.baidu就可以ping通了。

 

方法二 (我没试过)通过修改:
/etc/resolvconf/resolv.conf.d/base(这个文件默认是空的)
在里面插入:
nameserver xxx.xxx.xxx.xxx

(若有多个dns可以在添加nameserver)

修改好保存,然后执行
resolvconf -u
再看/etc/resolv.conf,最下面就多了一行:
cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver xxx.xxx.xxx.xxx

 

 

 

本文标签: 不上网桥接模式Linux