Ubuntu里面设置静态IP地址

来源:互联网 发布:屏幕放大镜软件 编辑:程序博客网 时间:2024/06/06 02:44

由于最近在假设一台GIT server, 所以想把服务器的IP地址设为静态IP。 下面是在ubuntu 13.10的系统下面设为动态和静态IP的方法。


1.设置为静态IP:

如果配置的网卡为 eth0, 则我们打开 /etc/network/interface 文件, 编辑:

$ vim /etc/network/interfaces

auto lo
iface lo inet loopback

#加入下面的eth0的静态IP配置
#Add static IP address for eth0 
auto eth0
iface eth0 inet static
address 192.168.1.41
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 192.168.1.2


2.设置为动态IP:

如果配置的网卡为eth0, 则我们打开/etc/network/interface 文件, 编辑:

$ vim /etc/network/interfaces

auto lo
iface lo inet loopback

#加入下面的eth0的动态IP配置
auto eth0
iface eth0 inet dhcp


3. 设置完成后需重启eth0:

方法一:

$ ifconfig eth0 down

$ ifconfig eth0 up


方法二:

$ sudo /etc/init.d/networking restart 

如果运行以上这个命令出现下面的提示, 请试方法二:

* Running /etc/init.d/networking restart is deprecated because it may not enable again some interfaces
 * Reconfiguring network interfaces... 

或者:

$ sudo service networking restart


4. 配置DNS。注: Ubuntu的系统, dns的配置最多只能有三个, 超过三个的不会生效。 

4-1. 如果只需要修改一次, 在/etc/resolv.conf  中加入 nameserver xxx.xxx.xxx.xxx, 配置立即可以生效。 但是由于该文件是动态生成的, 所以系统重启后,新加入的dns就会被覆盖掉。 具体方法如下:

$ vim /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
nameserver xxx.xxx.xxx.xxx
nameserver xxx.xxx.xxx.xxx

===================================================================

然后保存wq退出即可。 


4-2. 需要永久修改的话, 在 /etc/network/interface 中加入dns-nameservices xxx.xxx.xxx.xxx , 然后重启resovlconf就可以了。 然后可以查看 /etc/resolv.conf 是否已经在里面了。 (参看1, 配置静态IP 中的dns-nameservices 部分) 


4-3. 修改后让配置生效:

$ sudo /etc/init.d/resolvconf restart 

或者

$ sudo resolvconf -u


0 0
原创粉丝点击