Linux静态IP设置(tim)-v2.0

这份资料是我们华清远见嵌入式培训讲授C语言的文老师发给我们的,感觉挺好的,现在转发给大家。

设定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

nameserver8.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.1148.8.8.8

B修改resolvconf服务的配置文件: /etc/resolvconf/resolv.conf.d/base

清空原内容并写入以下内容

nameserver8.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 routedeldefault

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

添加

nameserver8.8.8.8

3.网关

sudo route add defalut gw 192.168.10.1

sudo routedeldefault


你可能感兴趣的:(linux,嵌入式,C语言)