ifconfig-Centos下设置静态IP

使用VirtualBox装了很多虚拟机,默认采用的DHCP的方式,但是随着虚拟机越来越多以后,这种方式很不方便,所以需要将机器的IP都设置为静态IP

需要添加的几项内容的解释:
IPADDR    IP地址
NETMASK    子网掩码
NETWORK    网关地址

通常,如果我们想更改主机地址为静态地址或者更改主机名,需要修改的几个文件包括:
/etc/sysconfig/network    设置主机名和网络配置
/etc/sysconfig/network-scripts/ifcfg-eth0    针对特定的网卡进行设置
/etc/resolv.conf    设置DNS
/etc/hosts            设置指定的域名解析地址

一般我们只需要修改网卡的配置文件就可以了,例如我的配置文件如下:

DEVICE=eth0
BOOTPROTO=static
TYPE=Ethernet
NAME=”System etho0″
BROADCAST=192.168.56.255
HWADDR=08:00:27:24:F8:9B
IPADDR=192.168.56.101
IPV6INIT=yes
IPV6_AUTOCONF=yes
NETMASK=255.255.255.0
NETWORK=192.168.56.1
ONBOOT=yes

设置完成后,重启一下网卡就可以了:service network restart

我们还有一个办法可以实现设置静态IP,那就是通过 ifconfig 这个命令。通常,我们都用它来查看当前网卡的一些信息,同时,他也可以用来进行一些网卡信息的设置。

修改的命令如下:ifconfig eth0 192.168.56.102

但是,这个命令执行后,只能够在当前会话中修改网卡的地址,我们看一下 ifcfg-eth0 的配置文件,仍然是
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
HWADDR=08:00:27:43:73:2f
也就是说重新启动服务器后,仍然会按照配置文件中的方式进行IP的获取。

所以,如果需要修改IP为静态IP的话,最好的方式还是通过修改配置文件来完成。

 

 

如果你ping的是广域网而不是局域网,那你还要确保网关和DNS设置正确

你可以按下面的步骤来设置网关

这条命令就可以搞定
route add default  gw 192.168.1.1

后面那是你的网关。

现在 route 一下
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     *               255.255.255.0   U     0      0        0 eth0
default         my.router       0.0.0.0         UG    0      0        0 eth0

之前如果没有添加 的话, 只有上面的第一条。  第二条是新加进去的。
如果还访问不了, 哪你的情况可能有点特殊哦。

这样重启以后 就不能 用了。
修改
编辑  /etc/network/interfaces
添加 gateway  192.168.1.1

DNS记录在/etc下的resolv.conf这个文件里。可以按resolv.conf文件的格式修改DNS。如果没有resolv.conf就建一个。

然后service network restart

ping 下试试。

 

参考资料:
1、用ifconfig设置参数遇到的问题
2、手动设置CentOS的IP 

17 thoughts on “ifconfig-Centos下设置静态IP

  1. Greate post. Keep writing such kind of information on your page.
    Im really impressed by your blog.
    Hey there, You’ve done an incredible job. I will definitely digg
    it and for my part recommend to my friends.
    I am sure they’ll be benefited from this website.

  2. With havin so much content do you ever run into
    any problems of plagorism or copyright violation? My website has a lot of completely unique content I’ve either created
    myself or outsourced but it appears a lot of it is popping it
    up all over the internet without my permission. Do you know any methods to help
    reduce content from being stolen? I’d truly appreciate it.

  3. Good day! This is my 1st comment here so I just wanted to give a quick shout out and tell you I genuinely enjoy reading your blog posts.
    Can you suggest any other blogs/websites/forums that deal with the same topics?
    Thanks a ton!

  4. You can certainly see your expertise in the article you write.

    The world hopes for more passionate writers like you who aren’t afraid to say how they believe.
    At all times follow your heart.

  5. Thanks for your marvelous posting! I quite enjoyed reading
    it, you could be a great author.I will always bookmark your blog and will often come back in the foreseeable future.
    I want to encourage you continue your great posts,
    have a nice morning!

  6. Hi, I do think this is a great site. I stumbledupon it 😉 I may revisit yet again since I book-marked it.
    Money and freedom is the greatest way to change, may you be rich and continue to guide other people.

  7. Greetings from Florida! I’m bored to death at work so I
    decided to check out your blog on my iphone during lunch break.
    I really like the info you provide here and can’t wait to take a look when I get home.
    I’m amazed at how fast your blog loaded on my mobile .. I’m not even using WIFI, just 3G ..
    Anyhow, superb blog!

  8. First of all I want to say wonderful blog! I had a quick
    question which I’d like to ask if you don’t mind.
    I was interested to find out how you center yourself and clear your mind prior to writing.
    I’ve had a difficult time clearing my thoughts in getting my thoughts
    out there. I truly do enjoy writing but it just seems like the first 10 to 15 minutes
    are generally wasted simply just trying to figure out how to begin. Any ideas or hints?
    Kudos!

  9. We’re a gaggle of volunteers and starting a brand new scheme in our community. Your web site offered us with useful info to work on. You have done an impressive activity and our whole neighborhood shall be grateful to you.

Leave a Reply

Your email address will not be published. Required fields are marked *