设定 IP
$sudo vi /etc/network/interfaces
auto lo
iface lo inet loopback
添加以下内容
auto eth0
iface eth0 inet static
address 192.168.0.221
<-填写自己检查未被使用的 ip 地址
netmask 255.255.255.0
gateway 192.168.0.1
<-填写与主机相同网关
保存,并关闭 gedit。
这样,IP 并没有立即生效。需要执行
$sudo /etc/init.d/networking restart
这时候就能 ping 到局域网中的电脑了。但是上不了 Internet,是因为没有设置 DNS 的原因。
DNS 信息保存在/etc/resolv.conf 中,一旦更改,立即生效。
$sudo vi /etc/resolv.conf
nameserver 192.168.1.1
nameserver 8.8.8.8
保存并关闭,现在就可以上网了。
不过 Ubuntu 12.04 每次重启后,/etc/resolv.conf 里面设置的 nameserver 就被清空
如果想自己定义 nameserver 一直有效,那么有以下 2 个办法:
A 在网卡的配置文件里面加:
iface eth0 inet static
address 192.168.3.3
netmask 255.255.255.0
gateway 192.168.3.1
dns-nameservers 114.114.114.114 8.8.8.8
B 修改 resolvconf 服务的配置文件: /etc/resolvconf/resolv.conf.d/base
清空原内容并写入以下内容
nameserver 8.8.8.8
这样,在机器重启或 resolvconf 服务重启 都可以保证配置会写到/etc/resolv.conf 里面
其实 /etc/resolv.conf 里面一直都有提示,只是自己没注意看:
"DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN"
附网卡设置相关命令:
查看网卡信息: ifconfig
设定一个网卡 IP:ifconfig eth0 192.168.1.10 netmask 255.255.255.0
重启网卡使设定生效:sudo /etc/init.d/networking restart
更改 MAC 地址:ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
查看路由相关信息:route -n
增加网关
sudo route add default gw 192.168.10.1
删除网关
sudo route del default
DNS 服务器设置(Ubuntu)
1、两个免费好用的 DNS 服务器
googleDNS:
8.8.8.8;
8.8.8.4
openDNS
:
208.67.220.220;
208.67.222.222
2、查看本机 DNS 服务器
查看 DNS server:nslookup www.baidu.com
更新 dns 设置,sudo resolvconf -u
临时设置 ubuntu static ip 并能上网
1.ip netmask
sudo ifconfig eth0 192.168.0.167 netmask 255.255.255.0
2.dns
sudo vi /etc/resolv.conf
添加
nameserver 8.8.8.8
3.网关
sudo route add defalut gw 192.168.10.1
sudo route del default