Today, I opened centos in virtualbox and found that I cannot access the Internet. It's strange.
The basic settings are as follows:
HOST: Windows 7 Ultimate x64 SP1
Virtualbox: 4.1.8
Virtual Machine System: centos 5.7
Two NICs are selected on virtualbox, one is host only and the other is Nat.
After the device is turned on, the Internet cannot be accessed, and the host only content is normal. View the routes as follows:
[Root @ localhost ~]#Route
Kernel IP routing table
Destination gateway genmask flags metric refUseIface
10.0.2.0*255.255.255.0 u 0 0 0 eth0
192.168.56.0*255.255.255.0 u 0 0 0 eth1
169.254.0.0*255.255.0.0 u 0 0 0 eth1
Default192.168.56.1 0.0.0.0 ug 0 0 0 eth1
It is found that the default gateway is running on the host only. How can it be connected to the Internet. I am a little white, but I also know that this is not feasible. I am decisive in putting my dog on the list and searching for high-end guidance.
1. Dual NIC (key, key ...)
Note:
Linux dual-nic default Routing Problems
After the second ENI is installed, the network cannot be accessed. You can use route to find that the default route is faulty.
After verification, we know that eth0 and eht1 are first loaded when Linux loads the NIC configuration file. In this way, if gateway is set in eth1, it will overwrite
In eth0, the solution is to delete the gateway settings of eth1. The file is located at/etc/sysconfig/networking.
Delete the gateway settings in eth1 under the/devices directory.
Reference: http://hi.baidu.com/%C5%B7%D1%F4%D3%FE%B3%CF/blog/item/974ef71f0c1c4f0a403417cf.html
2. Use the route command
See the following.
Decisive adjustment: Adjust virtualbox. The first Nic is host only, and the second Nic is Nat. (You can also map the second physical network card to eth0 in Linux without moving virtualbox, and map the first block to eht1 .), Set the host-only fixed IP address to eth0, restart the network, and output the route:
[Root @ localhost ~]#Route-n
Kernel IP routing table
Destination gateway genmask flags metric refUseIface
10.0.3.0 0.0.0.0 255.255.255.0 u 0 0 0 eth1
192.168.56.0 0.0.0.0 255.255.255.0 u 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 u 0 0 0 eth1
0.0.0.0 10.0.3.2 0.0.0.0 ug 0 0 0 eth1
The key point is that the default IP address has been replaced. In this way, you can access the Internet...
[Switch] use of route:
Http://blog.chinaunix.net/space.php? Uid = 22006903 & Do = Blog & id = 149739
Linux route table maintenance () classification: Linux System Configuration View Linux KernelRoute table
Use the following route command to view the Linux KernelRoute table.
# Route
Destination gateway genmaskFlagS metric ref use iface192.168.0.0 * 255.255.255.0 u 0 0 0 eth0169.254.0.0 * 255.255.0.0 u 0 0 0 eth0default 192.168.0.1 0.0.0.0 ug 0 0 0 eth0
Route command output description
| output item |
description |
| destination |
destination CIDR block or host |
| gateway |
gateway address, "*" indicates the network to which the target host belongs. No route is required |
| genmask |
network mask |
| flag S |
mark. Some possible tags are as follows: |
| |
the U-route is active |
| |
H-the target is a host |
| |
G-route pointing to Gateway |
| |
r-Restore table items generated by dynamic routing |
| |
D-install the Program in the background of the route |
| |
M-modified by the background program of the route |
| |
! -Route rejection |
| metric |
route distance, the number of connections required to reach the specified network (not used in Linux kernel) |
| ref |
Number of Route entry references (not used in Linux kernel) |
| use |
Number of times this route item was searched by the routing software |
| iface |
output interface corresponding to route table |
Three route types Host Routing
Host Routing is a route record pointing to a single IP address or host name in the routing selection table. Host routeFlagThe s field is H. For example, in the following example, the local host uses the vro192.168.of IP address 192.168.1.1 to reach the host with IP address 10.0.0.10.
Destination gateway genmaskFlagS metric ref use iface ----------- ------- ----- 10.0.0.10 192.168.1.1 255.255.255 uh 0 0 0 eth0
Network Routing
A network route is a network that can be accessed by a host. Network routeFlagThe s field is N. For example, in the following example, the local host forwards packets sent to 192.19.12 to the vro whose IP address is 192.168.1.1.
Destination gateway genmaskFlagS metric ref use iface ----------- ------- ----- 192.19.12 192.168.1.1 255.255.0 UN 0 0 eth0
Default route
When the host cannotRoute tableWhen you find the IP address or network route of the target host, the packet is sent to the default route (Default Gateway. Default routeFlagThe s field is G. For example, in the following example, the default route is the vro with the IP address 192.168.1.1.
Destination gateway genmaskFlagS metric ref use iface ----------- ------- ----- default 192.168.1.1 0.0.0.0 ug 0 0 0 eth0
Configure Static Routing Route command
Set and viewRoute tableYou can use the route command to set the kernel.Route tableThe command format is:
# Route [add | del] [-net |-host] target [netmask nm] [GW] [[Dev] If]
Where:
- Add: Add a routing rule
- DEL: delete a routing rule.
- -Net: the destination address is a network.
- -Host: the destination address is a host.
- Target: target network or host
- Netmask: network mask of the destination address
- GW: Gateway used to route data packets
- Dev: the network interface specified for the route
Route command example
Route entry added to the host
# Route add-host 192.168.1.2 Dev eth0: 0 # route add-host 10.20.30.148 GW 10.20.30.40
Route entry added to the network
# Route add-net 10.20.30.40 netmask 255.255.255.255.248 eth0 # route add-net 10.20.30.48 netmask 255.255.255.255.248 GW 10.20.30.41 # route add-net 192.168.1.0/24 eth1
Add default route
# Route add default GW 192.168.1.1
Delete A route
# Route del-host 192.168.1.2 Dev eth0: 0 # route del-host route GW 10.20.30.40 # route del-net route netmask route 248 eth0 # route del-net route netmask route 248 GW 10.20.30.41 # route del-net 192.168.1.0/24 eth1 default GW 192.168.1.1
Configure packet forwarding
The default Kernel configuration in centos already contains the routing function, but it is not enabled by default when the system is started. To enable Linux routing, you can adjust the network parameters of the kernel. To configure and adjust kernel parameters, run the sysctl command. For example, to enable the packet forwarding function of the Linux kernel, run the following command.
# Sysctl-W net. ipv4.ip _ forward = 1
After this setting, the current system will be able to forward packets, but the next time you start the computer, it will become invalid. To enable the system to remain valid the next time you start the computer, write the following lines to the configuration file/etc/sysctl. conf.
# Vi/etc/sysctl. conf
Net. ipv4.ip _ forward = 1
You can also run the following command to check whether the current system supports packet forwarding.
# Sysctl net. ipv4.ip _ forward