Ubuntu network settings

Source: Internet
Author: User

Ubuntu network settings
Content:
A). Three Methods for setting IP addresses in Ubuntu 14.04; B). Use NetworkManager to manage wireless networks; C). Solve the resolv. conf rewrite problem.
A ). there are three ways to set IP addresses in Ubuntu 14.04: 1. step 1. configure the IP address. For example, set the IP address of the network adapter eth0 to 172.31.174.142 and the subnet mask to 255.255.255.0 # ifconfig eth0 172.31.174.124 netmask 255.255.255.0.
Step 2. add a default route (set gateway), for example, set gateway to 172.31.174.1 # route add default gw 172.31.174.1 step 3. to configure DNS, you need to open the file/etc/resolv. conf to add the DNS server address. Use v editor I to open/etc/resolv. command: # vi/etc/resolv. conf open the file/etc/resolv. after conf, you will see that the DNS server address can be modified after the nameserver. If multiple DNS server addresses are added, you can sort nameserver 140.207.198.6 in sequence as follows:
Nameserver 202.102.154.3

Step 4. Restart the NIC configuration to make the network configuration take effect. #/Etc/init. d/networking restart

Step 5. use ifconfig to view the configuration result # the network configuration in step 1 and step 2 of ifconfig can only be temporarily modified. After linux is restarted, the configuration information will be lost, if you run the ifconfig command to view the configuration, you will not be able to see the previous network configuration information.
2. directly modify the configuration file

The ubuntu network configuration file is/etc/network/interfaces. You can set DHCP or manually set static IP addresses in it.

1. Configure the NIC in DHCP mode,

Use the vi editor to open the configuration file/etc/network/interfaces. The command is as follows:

# Vi/etc/network/interfaces

The content is as follows:

# The primary network interface-use DHCP to find our address

Auto eth0 iface

Eth0 inet dhcp
 

File

Auto eth0 iface indicates that the NIC is automatically mounted upon startup;

Eth0 inet dhcp indicates Dynamic ip address setting.

Restart the NIC configuration to make the network configuration take effect:

#/Etc/init. d/networking restart


You can also enter the following command in the command line to obtain the dynamic IP address sudo dhclient eth0.


2. configure a static IP address for the NIC

Edit the file/etc/network/interfaces:

# Vi/etc/network/interfaces

Replace eth0 with the following rows:

# The primary network interface

Auto eth0

Iface eth0 inet static

Address 172.31.174.142

Gateway 172.31.174.1

Netmask has been released successfully

# Network 203.171.239.128

# Broadcast 192.168.3.159


File

Iface eth0 inet static indicates that the IP address is set to a static IP address.

Address 172.31.174.142 indicates that the IP address is set to 172.31.174.142.

Gateway 172.31.174.1 indicates that the gateway is set to 172.31.174.1.

Netmask 255.255.255.255.0 indicates that the subnet mask is 255.255.255.0.

Note: You can ignore the comments first.

If you need to configure the DNS server, configure the DNS:

To configure DNS, open the file/etc/resolv. conf to add the DNS server address. Use v editor I to open/etc/resolv. command: # vi/etc/resolv. conf open the file/etc/resolv. after conf, you will see that the DNS server address can be modified after the nameserver. If multiple DNS server addresses are added, you can sort nameserver 140.207.198.6 in sequence as follows:
Nameserver 202.102.154.3

Restart the NIC configuration to make the network configuration take effect:

#/Etc/init. d/networking restart

 

3. Use NetworkManager for Network Configuration

In ipvbtu, in addition to modifying/etc/network/interfaces for configuration, you can also directly configure it in network-manager.

Network-manager is configured and intuitive. You can follow the prompts to perform operations step by step. If you are interested, you can Google or Baidu on your own.

However, if interfaces is modified and network-manager is configured, you will find some inexplicable problems:

1. The network settings in interfaces and network-manager are different. What is the actual IP address of the system?

2. Sometimes, the network connection icon in the upper-right corner of the interface is lost.

3. The correct network settings are clearly configured in network-manager. Why cannot the network be connected?

In fact, we need to know the relationship between interfaces and network-manager, which is not difficult to explain.

First, when there is no third-party network management tool (such as network-manager) in the system, the system uses parameters in the interfaces file by default for network configuration.

After network-manager is installed in the system, network-manager takes over the network configuration of the system by default, and uses network-manager's own network configuration parameters for configuration.

However, if you manually modify the interfaces file after installing network-manager (network-manager is installed by default in the Desktop version, network-manager will automatically stop managing the system network, and the system will use the parameters in the interfaces file for network configuration.

In this case, modify the parameters in network-manager without affecting the actual network configuration of the system. To make the configuration in network-manager take effect, you must re-enable the network-manager to take over the network configuration of the system (how to re-enable it will be described below ).

Now that we know the working relationship between the two, let's look at the above three questions:

1. Check whether the network-manager has taken over. If the network-manager has not taken over, the actual IP settings of the system are subject to interfaces. Otherwise, refer to network-manager.

2. When network-manager stops taking over, the network connection icon is lost.

3. It is also a matter of taking over.

If you want to directly use interfaces for network configuration in the Desktop version, you 'd better delete network-manager.

To delete a network-manager, you only need to enter the following command:

# Apt-get remove network-manager

If you want to continue using network-manager for network configuration after the above three problems occur, You need to perform the following operations:

Sudo servicenetwork-manager stop # stop the network-manager Service

Sudo rm/var/lib/NetworkManager. state # Remove the status file of network-manager.

Sudo gedit/etc/NetworkManager/nm-system-settings.conf # Open the configuration file for network-manager

# There is a line in it: managed = true

# If you manually change/etc/network/interfaces, network-manager will change this line to managed = false.

# Change false to true

Servicenetwork-manager start

B) Use NetworkManager to manage Wireless Networks

NetworkManager is most convenient when a PC is connected to a wireless network, because the iwlist wlan0 scan on the text interface is not very useful, NetworkManger can list all available wifi hotspots like windows, if Chinese characters are garbled, they can be connected. However, if your machine is used as a server or in the office, there is a fixed routing environment. In general, there will also be a fixed ip address, because this can facilitate Intranet resource sharing. In short, it is not for personal use, and it is best to turn off the NetworkManager without moving it. Of course, if you often take your notebook and want to go to the cafe to get rid of the network, you must open the NetworkManager, it is very convenient to automatically search for wireless devices.
The two situations are described below:
1. Use NetworkManger to connect to the Internet. In this case, if a wired connection route is used and eth0 is configured in/etc/network/interfaces, A super strange problem occurs: You can access the Internet, but you cannot access the Intranet. For example, if the ip address of the wireless route is 192.168.0.1 and the ip address is pinged, the ip address of the wired network card cannot be pinged, not wireless! It is proved that eth0 is preferred when it is set, But the strange thing is that the Internet connection is normal, and I suspect this may be a bug in Ubuntu. So when NetworkManager is used, you can comment out all the content in/etc/network/interfaces and only keep the local loop network:

Auto lo
Iface lo inet loopback

These two sentences. Set a fixed ip address, which can be configured in the NetworkManager graphic interface.
2. Disable NetworkManager,

Close command: sudo/etc/init. d/network-manager stop

Cancel startup: chkconfig network-manager off

Restart the network:/etc/init. d/networking restart

Modify the/etc/network/interfaces file,
System Configuration: local loopback network.
Auto lo
Iface lo inet loopback
Wired Configuration:
Auto eth0
# Iface eth0 inet dhcp # If you do not want to use a fixed ip address, we recommend using a fixed ip address to save the time allocated by the request route.
Iface eth0 inet static
Netmask 255.255.255.0
Gateway 192.168.0.1 # gateway 0.0.0.0 # Set all gateways to 0
Address 192.168.0.112
Wireless Configuration:
Auto wlan0
Iface wlan0 inet static
Netmask 255.255.255.0
Gateway 192.168.0.1
Address 192.168.0.113
Pre-up ip link set wlan0 up
Pre-up iwconfig wlan0 essid ssid
Wpa-ssid TP-Link # The ssid here is the wireless name set in the route
Wpa-psk 12345678 # wireless password
Adsl dial-up Internet access:
Auto dsl-provider
Iface dsl-provider inet ppp # dsl-provider is the previously configured dialing name
Provider dsl-provider

C ). solve resolv. although DNS is configured for conf rewriting, the/etc/resolv after each machine restart or network restart. the conf file will be overwritten, that is, the original sample will be restored, and your previous configuration will not exist. Manual configuration every time is required, so this step is required, first, we need to figure out resolv. the reason and mechanism for conf being overwritten are different in different Ubuntu versions. How can we know? Generally, the resolv. conf file will tell you at the beginning. Method 1: Related to version a) after ubuntu 12.04 is enabled with/etc/resolv. conf, you can see the first sentence:
Note that the resolv. conf file is generated by the NetworkManager Program (corresponding to the network-manager Service). The solution is as follows: we can turn off the network-manager file and run the following command:
# Service network-manager stop or #/etc/init. d/network-manager stop
However, this method does not work once and for all, because the service will be automatically started every time the system is restarted, so we need to completely disable network-manager to start.
Edit the network manager configuration file/etc/init/network-manager.conf:
#/Etc/init/network-manager.conf comment out the start on section:
# Network-manager-network connection manager # The Network Manager daemon manages the system's network connections, # automatically switching between the best available.
Description "network connection manager"
# Start on (local-filesystems # and started restart # and static-network-up) stop on stopping restart
Reverse CT fork respawn
Script # set $ LANG so that messages appearing on the GUI will be translated. see LP: 875017 if [-r/etc/default/locale]; then. /etc/default/locale export lang language LC_MESSAGES LC_ALL fi B) Ubuntu 13.04 and ubuntu 14.04 do not know which version of Ubuntu is from which the NetworkManager generates resolv. conf, at least in 13.04, because resolv. conf starts with a sentence like this:
# 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
It is generated by resolvconf, and NetworkManager still exists because it is indispensable for DHCP to access the Internet. However, disabling NetworkManager to start is no longer a problem because resolv. conf is no longer owned by it. Let's take a look at resolv. what is the relationship between conf and resolvconf? View:

Description:/etc/resolv. conf is actually a link. It actually points to/run/resolvconf/resolv. conf, which explains why it will be overwritten every time you restart. You only change the link, which has no effect on the original file, and you have to load the content of the original file every time you restart the link, the link modification is invalid.

Solution: modify the original file as follows:

vi /etc/resolvconf/resolv.conf.d/head
This file is exactly the same as the/etc/resolv. conf file. You can add your own nameserver to it so that it will not be overwritten after each restart.

PS: some people on the Internet also say that modifying the/etc/resolvconf/resolv. conf. d/base file is fine. I did not test it myself. If you are interested, you can test it on your own.

Method 2: version-independent

As mentioned above, the fixed IP address is mainly used to modify the/etc/network/interfaces file and configure the IP address and gateway. In fact, there is a parameter that can be configured, that is, DNS, the corresponding parameter is dns-nameservers. The priority set here is higher than resolv. conf and NetworkManager are high, that is, the network reads the DNS configuration from here. If no configuration is available, the resolv will be viewed. conf, so it is easier to configure DNS here.

# Interfaces (5) file used by ifup (8) and ifdown (8) auto lo iface lo inet loopback auto eth0
Iface eth0 inet static address 192.168.1.151 netmask 255.255.255.0 gateway 192.168.1.2
Dns-nameservers 202.38.64.1

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.