Linux route commands to understand and use, as well as network card command details

Source: Internet
Author: User

Basic knowledge of the road:
1) Routing Concepts
Routing: The process of forwarding packets across an internetwork from a source host to a target host
Routers: devices that can forward packets to the correct destination and select the best path during the forwarding process
Routing table: Route entries maintained in routers, routers are selected according to routing table
Direct-Connect routing: When an interface's IP address is configured on the router and the interface status is up, a direct-connect route entry appears in the routing table
Static routing: is configured manually by the administrator and is unidirectional.
Default route: When the router does not find a route entry for the destination network in the routing table, the router forwards the request to the default routing interface.

2) Characteristics of static routes and default routes
Static routing Features:
The routing table is set manually;
Static routes do not change unless the network administrator intervenes;
The formation of routing table does not need to occupy network resources;
Applicable environment: Generally used in networks with small network size and fixed topological structure.

Default Routing Features:
In all routing types, the default route has the lowest priority
Applicable environment: general application in the end network with only one exit or as a supplement to other routes

Floating Static Routes:
When a route entry for the same destination network exists in the routing table, the request is forwarded to the appropriate port based on the priority of the routing entry;
The role of link redundancy;

3) encapsulation process when the router forwards the packet
The source and destination IPs do not change, and the source and target Macs Change and are re-encapsulated in each segment of the network, respectively, the source and destination addresses of each segment

4) To complete the routing of the packet, a router must know at least the following:
a) The destination address
B) connected to the router, and where the information can be obtained from the remote network
C) to all remote network possible routes
D) to reach the best route for each remote network
e) How to maintain and verify routing information
F) Comparison of routing and switching
routing work at the network layer
A) forwarding data based on "routing table"
B) routing
C) route forwarding
Switching work at Data link Layer
D) forwarding data based on "MAC Address Table"
e) Hardware forwarding
Two. Command parameter:
-C Displays more information
-N does not resolve name
-V displays verbose processing information
-f Display Send message
-C show route cache
-F clears the route table for all gateway portals.
-P is used with the add command to make the route permanent.

Add: Adds a new route.
del: Deletes a route.
-net: The destination address is a network.
-host: The destination address is a host.
Netmask: When you add a network route, you need to use a netmask.
GW: Route packets through the gateway. Note that the gateway you specify must be able to be reached.
Metric: Sets the number of route hops. The
command specifies the commands you want to run (add/change/delete/print). The
Destination specifies the network destination for this route. The
Mask Netmask Specifies the network mask (also known as the subnet mask) that is associated with the network destination. The
Gateway specifies the address set and subnet mask defined by the network destination to reach the forward or next hop IP address. The
Metric metric specifies an integer cost value for the route (from 1 to 9999) that can be used when the routing table is selected in multiple routes that match the destination address of the forwarded packet. The
If Interface specifies the interface index for the interface that can access the target. To obtain a list of interfaces and their corresponding interface indexes, use the display function of the route print command. You can use decimal or hexadecimal values for interface indexing.
Three commands use example
1. Add route to host
#route add-host 192.168.1.2 Dev eth0:0
#route add-host 10.20.30.148 GW 10.20.30.40
2. Route added to the network
#route add-net 10.20.30.40 netmask 255.255.255.248 eth0
#route add-net 10.20.30.48 netmask 255.255.255 .248 GW 10.20.30.41
#route add-net 192.168.1.0/24 eth1
3. Add default route
#route add defaults GW 192.168.1.1

4. Delete a route
#route del-host 192.168.1.2 Dev eth0:0
#route del-host 10.20.30.148 GW 10.20.30.40
#route del-net 10.20.30.40 netmask 255.255.255.248 eth0
#route del-net 10.20.30.48 netmask 255.255.255.248 GW 10.20.30.41
#route del-net 192.168.1.0/24 eth1
#route del default GW 192.168.1.1//route del defaults to delete all of the defaulted routes

Add a default route
#route Add default GW 10.0.0.1//defaults to only be active in memory boot can be appended to the/etc/rc.local file
#echo "route add default GW 10.0.0.1" >>/etc/rc.local

Add a static route
#route add-net 192.168.2.0/24 GW 192.168.2.254
To be permanently effective, do this:
#echo "Any net 192.168.2.0/24 GW 192.168.2.254" >>/etc/sysconfig/static-routes

Static routes added to a single host
Route add-host 192.168.2.2 GW 192.168.2.254
To be permanently effective, do this:
#echo "Any host 192.168.2.2 GW 192.168.2.254" >>/etc/sysconfig/static-routes
Note: The Linux default does not have this file, you have to manually create a
5 Set up Package forwarding
The default kernel configuration in Linux already includes the routing feature, but the default does not enable this feature at system startup;
The Linux-enabled routing function can be implemented by adjusting the kernel's network parameters, as follows:
To temporarily turn on the routing feature:
#echo 1 >/proc/sys/net/ipv4/ip_forward
or # sysctl-w net.ipv4.ip_forward=1
Permanently turn on the routing feature
#vim/etc/sysctl.conf
Net.ipv4.ip_forward = 1
#sysctl-P
6. Static routing Configuration
The syntax for adding a static route to the routing table is as follows:
1.ip Route [Destination_network] [mask] [next-hop_address]administrative_distance]
Parameter resolution:
The IP route is used to create a static route command.
The destination_network needs to be published to the network segment in the routing table.
Mask the subnet mask used on this network.
Next-hop_address the address of the next-hop router.
Administrative_distance by default, static routes have an administrative distance of 1. Add administrative rights at the end of this command to modify this default value.
For example
IP Route 172.16.1.0 255.255.255.0 172.16.2.1
View the route table in addition to using the Route-n command, you can also use the IP route
[[Email protected]~]# IP route
192.168.1.17 Dev ppp0 proto kernel scope link src 192.168.1.190
10.1.32.14 Dev tun0 Scope link
10.1.32.12 Dev tun0 Scope link
10.4.8.2 via 192.168.9.254 dev eth0 src 192.168.9.200 MTU Advmss 1460
10.4.9.0/24 Dev tun0 Scope link
192.168.9.0/24 Dev eth0 proto kernel scope link src 192.168.9.200 metric 1
10.2.0.0/16 Dev tun0 Scope link
10.0.0.0/16 Dev tun0 Scope link
10.1.0.0/16 Dev tun0 Scope link
192.168.0.0/16 Dev tun0 Scope link
Default via 192.168.9.254 Dev eth0
[[Email protected] ~]# IP route
192.168.1.17 Dev ppp0 proto kernel scope link src 192.168.1.190
10.1.32.14 Dev tun0 Scope link
10.1.32.12 Dev tun0 Scope link
10.4.8.2 via 192.168.9.254 dev eth0 src 192.168.9.200 MTU Advmss 1460
10.4.9.0/24 Dev tun0 Scope link
192.168.9.0/24 Dev eth0 proto kernel scope link src 192.168.9.200 metric 1
10.2.0.0/16 Dev tun0 Scope link
10.0.0.0/16 Dev tun0 Scope link
10.1.0.0/16 Dev tun0 Scope link
192.168.0.0/16 Dev tun0 Scope link
Default via 192.168.9.254 Dev eth0
Two. Linux NIC command
CENTOS6
Ifconfig View all network cards
Ifconfig-NIC name view individual NIC information
Ifconfig up NIC name restart NIC
Ifconfig down network card name disable NIC
/etc/sysconfig/network-scripts/NIC configuration file
CENTOS6 manually changing the NIC:
Vim/etc/sysconfig/network-scripts/ifcfg-ethx
Device=eth0//Equipment Name
Onboot=yes//Whether Open
Nm_controlled=yes//Whether real-time effective
Bootproto=static Status of//IP
ipaddr=172.18.253.211//IP
Prefix=16//IP ID
gateway=172.18.0.1//Gateway
dns1=172.18.0.1//Domain name
Hwaddr=00:0c:29:55:04:fe//mac Address

CENTOS7 nmlic Command Set
Show all network connections: Nmcli con Show
Show active Network connections: Nmcli con show-active
Displays details of the specified network connection: Nmcli con show eno16777728
Show Network device connection status: Nmcli dev Status
Show details of all network devices: Nmcli Dev Show
Displays details of the specified network device: Nmcli Dev Show eno16777728
Enable network connection: Nmcli con up eno16777728 Disable network connection (can be activated automatically): Nmcli con down eno33554960
Disable the NIC to prevent auto-activation: nmcli dev dis eth0
Delete network connection configuration file: Nmcli con del eno33554960
Reload Configuration Network configuration file: Nmcli con reload
Dynamic network connection configuration for IP mode: nmcli con add con-name eno16777728 type Ethernet ifname eno16777728
Specify the network connection configuration for static IP mode: nmcli con add con-name eno16777728 ifname eno16777728 autoconnect Yes type Ethernet IP4 10.1.254.254/16 GW 4 10.1.0.1
Enable/Disable all network connections: NMCLI NET on/off disables network devices and prevents automatic activation: nmcli con dis eno33554960
To view help for adding a network connection configuration: Nmcli con add helper

Modify network Connection single parameter
Nmcli con mod if-name connection.autoconnect Yes modified to auto Connect
Nmcli con mod if-name ipv4.method manual | DHCP modifies whether the IP address is static or DHCP
Nmcli con mod if-name ipv4.addresses "172.25.x.10/24 172.25.x.254" Modify IP configuration and gateways
Nmcli con mod if-name ipv4.gateway 10.1.0.1 Modify Default gateway
Nmcli con mod if-name +ipv4.addresses 10.10.10.10/16 Add a second IP address
Nmcli con mod if-name ipv4.dns 114.114.114.114 add dns1
Nmcli con mod if-name +ipv4.dns 8.8.8.8 add dns2
Nmcli con mod if-name-ipv4.dns 8.8.8.8 Remove DNS

Linux route commands to understand and use, as well as network card command details

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.