Reproduced Linux series: Ubuntu virtual machine Set fixed IP internet (configure IP, gateway, DNS, prevent resolv.conf from being rewritten)

Source: Internet
Author: User
Tags locale nameserver

It is most convenient to set up the Internet in the virtual machine, because there is no need to manually set up the Internet, but Nat's Internet mode by default is DHCP dynamically assigned IP, which means that every time you restart the virtual machine has a different IP address, which is not a problem for the general user. But if you have special use of the machine, such as the server, need IP address information, or like me, as a cloud computing node, its IP is configured in the configuration file, if every restart of the system to run past changes in the configuration file in the IP, it is simply undesirable practice, So it's very necessary to set up a fixed IP network for virtual machines!

There are many ways to set up fixed IP, we all know that there are three modes of virtual machine surfing:bridged, Host-only and Nat, in which NAT mode corresponds to VMNET8 virtual network, host-only mode corresponds to VMNET1 virtual network, The bridged mode corresponds to the VMNET0 virtual network, which is generated by the VMware Virtual machine auto-configuration and does not need to be set by the user. VMnet8 and VMNET1 provide DHCP services, and VMNET0 virtual networks are not available. Three ways specific differences and introductions can be seen in the reference (1).

Because of the simplest way of NAT, the following are the specific methods of NAT settings.

Test environment:

Virtual machine version: VMware Workstation 10

Ubuntu version: Ubuntu 12.10/13.04


1. Set the virtual machine to Internet mode as NAT



2. Configuring the VMNET8 Virtual network (VM Editor)

Because NAT corresponds to the VMNET8 virtual network, open the VMware-brought virtual network editing tool in the Web editor, which is configured as follows:

Where the subnet IP is set according to its own circumstances, such as I want to set a fixed IP address of 192.168.1.151, then the subnet IP here is 192.168.1.0, also remember to cancel the DHCP option service.


3. Go to the Ubuntu system and configure the network

The main configuration of the following three aspects of the content:

(1) Modify the file/etc/network/interfaces, here is the IP, gateway, mask and some other configuration;

(2) Modify the file/etc/resolv.conf, this file to save the relevant information of DNS;

(3) solve the problem of resolv.conf being rewritten;

(4) Restart the virtual machine network

Here are the specific configuration methods:

(1) Modify the file/etc/network/interfaces

Command: sudo vi /etc/network/interfaces orsudo gedit /etc/network/interfaces

My configuration is as follows:

# interfaces (5) file used by Ifup (8) and Ifdown (8) Auto loiface lo inet loopbackauto eth0 staticaddress 192.168.1.15 1netmask 255.255.255.0gateway 192.168.1.2 

One of the gateway gateways is consistent with the "NAT Settings ..." In Virtual Network editor, which generally seems to be 192.168.xx.2.

(2) Modify the file/etc/resolv.conf

Command: sudo vi /etc/resolv.conf orsudo gedit /etc/resolv.conf

My configuration is as follows:

NameServer 202.38.64.1

Of course, here is the individual, plus your own DNS server can, my is the campus DNS.


(3) Solve resolv.conf is rewritten problem

In the second step, you configure DNS, but each time you restart the virtual machine or restart the network/etc/ resolv.conf file will be rewritten, that is, back to the original, your previous configuration does not exist, each time to manually configure is very undesirable, so this step is necessary, first we need to figure out the reason and mechanism of resolv.conf is rewritten, which differs in different versions of Ubuntu. How do you know that? General resolv.conf file I'll tell you at the beginning.

In fact, there are two ways to solve the problem, not afraid of trouble to understand the principle of reference method one (and version related), afraid of trouble do not want to toss the consciousness to the method of two (unrelated to the version).


Method one: Related to version

A) Ubuntu 12.10

After opening/etc/resolv.conf, you can see the beginning of the sentence:

# Generated by NetworkManager

Description resolv.conf This file is generated by the NetworkManager program (corresponding to the Network-manager service), then the solution will come: We turn off the Network-manager, the command is as follows:

sudo service network-/etc/init.d/network-manager stop  

But this method is not once and for all, because this service is started automatically after each reboot of the system, so we need to completely prohibit Network-manager boot.

To edit the network manager's configuration file /etc/init/network-manager.conf:

/etc/init/network-manager.conf 

Comment out the start on part of it:

# Network-manager-network Connection manager## The Network Manager daemon manages the system ' s network connections,# AUT Omatically switching between the best available.description     ' network Connection Manager '#start on ( local-filesystems# and         started dbus# and         static-network-up) stop on stopping Dbusexpect forkrespawnscript        # set $LANG So, messages appearing on the GUI would be translated. See lp:875017        if [-r/etc/default/locale];                /etc/default/locale                export LANG LANGUAGE lc_messages L C_all        fi

b) Ubuntu 13.04

I do not know which version of Ubuntu from the beginning is not generated by the NetworkManager resolv.conf, at least in 13.04 is the case, because the beginning of resolv.conf wrote such a sentence:

Generated by resolvconf (8) # The     FILE by HAND--YOUR changes  would be overwritten

Said to be generated by resolvconf, NetworkManager still exists, because it is a DHCP Internet is indispensable, but at this time prohibit NetworkManager boot is no longer used, Because resolv.conf has not returned to its tube, we try to see what resolv.conf and resolvconf have to do, see:

/etc/resolv.conflrwxrwxrwx 1 root root  September  /etc/resolv.conf. /run/resolvconf/resolv.conf  

Explain/etc/resolv.conf is actually just a link, it actually point to is /run/resolvconf/resolv.conf , this also explains why each reboot will be rewritten for the reason, you changed only a link, the original file has no effect, and each time you restart the link to load the original file content, So the change to link is invalid.

The workaround is to modify the actual original file as follows:

/etc/resolvconf/resolv.conf.d/head  

Find this file is identical to the/etc/resolv.conf file, which is right, add your own nameserver inside, so that each reboot will not be rewritten.

PS: Some people on the internet also said that the modification of /etc/resolvconf/resolv.conf.d/base this file is OK, I did not personally test, interested in the use of their own testing.


Said so much, each version of the difference seems to be very troublesome, do not know the latest version or later version will not be changed, in fact, there is a more simple version-independent way to prevent the resolv.conf file is rewritten, that is method two.


Method Two: independent of version

Previously mentioned fixed IP Internet access is mainly modified /etc/network/interfaces This file, configure IP, gateway or something, in fact, there is a parameter can be configured, that is the DNS, the corresponding parameter named Dns-nameservers , where the priority is higher than resolv.conf, that is, the network will read the DNS configuration from here, if not configured to see the resolv.conf inside the settings, so it is easier to configure DNS in this area.

# interfaces (5) file used by Ifup (8) and Ifdown (8staticaddress 192.168.1.151netmask 255.255.255.0  Gateway 192.168.1.2dns-nameservers 202.38.64.1    
(4) Restart the virtual machine network

After the configuration is complete, there are several ways to restart the network:

/etc/init.d/networking restart 

You can also restart the network card:

sudo ifconfig eth0 down sudo ifconfig eth0 up

Restarting the network card has no effect on other network cards, more recommended.

Hurry to ping www.baidu.com bar, should be able to surf the Internet.


PS: Of course can also be configured through the GUI network manager, but personally feel this way best, so recommend!

Reprint Address: Http://www.cnblogs.com/lanxuezaipiao/p/3613497.htmlps: Really tidy very good ah!! Can't help reprint Ah!!!

Reproduced Linux series: Ubuntu virtual machine Set fixed IP internet (configure IP, gateway, DNS, prevent resolv.conf from being rewritten)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.