Linux System Network Service command (i)

Source: Internet
Author: User

First, the Linux system networking settings

After we installed the Linux system, the first is to configure the network services, after all, if the computer is not networked it is a calculator. When configuring network functions, it is simple and quick to start with the dhclient command ( network parameters that dynamically configure network interfaces using Dynamic Host Configuration Protocol), and the following describes using configuration files to define static network addresses and link the Internet.

1. Configure the network card file:

[Email protected] ~]# Cat/etc/sysconfig/network-scripts/ifcfg-eth0 device=eth0//the device to which this configuration is associated. The device name should be consistent with the contents of the file ifcfg-Hwaddr=00:0c:29:7a:e4:0e//mac address: To be consistent with the real MAC address, omit the type=ethernet//device type uuid= 06512c35-1f92-4b57-875d-cb76cd6a4ee2//Unique identification, can omit Onboot=yes//Whether boot automatically start this network device {Yes|no} nm_controlled=yes// Whether to accept NetworkManager script control: {yes|no} bootproto=static//Boot protocol: {NONE|STATIC|DHCP|BOOTP} IPADDR=192.168.1.110//IP address netmask=255.255.255.0//Mask gateway=192.168.1.1//Default gateway DNS1=8.8.8.8//DNS1 service address You can also add the following: ipv6init=no// Whether to turn on ipv6userctl=no//allows normal users to operate the NIC peerdns={yes|no}//whether to allow DHCP service to assign addresses directly when updating DNS server addresses in/etc/resolv.conf



2. Restart the network service, read the configuration file

[Email protected] ~]#/etc/init.d/network restart


3, check whether the machine can be networked

[[email protected] ~]# ping-c 1 ping www.a.shifen.com (61.135.169.121) (+) bytes of data.64 bytes from 61.135.169.121: Icmp_seq=1 ttl=55 time=24.9 ms---www.a.shifen.com PING statistics---5 packets transmitted, 5 received, 0% packet loss, Time 4046msrtt Min/avg/max/mdev = 24.559/30.358/38.983/6.768 ms

By pinging the www.baidu.com website and having a reply, you can see that the machine is connected to the Internet.


Second, the simple use of ifconfig command

1. Command format

ifconfig [Network Devices] [parameters]

2. Command function

The Ifconfig command is used to view and configure network devices. This command can be used to configure the network appropriately when the network environment changes.

3. Command parameters

Up starts the specified network device/NIC.

Down to turn off the specified network device/NIC. This parameter can effectively block the IP traffic through the specified interface, and if you want to permanently shut down an interface, we also need to route from the core

All the routing information for this interface is removed from the table.

The ARP setting specifies whether the NIC supports the ARP protocol.

-A displays all interface information.

Add to configure the IPV6 address for the specified NIC

Del deletes the IPV6 address of the specified network card

netmask< Subnet mask > Set subnet mask for network card. The mask can be a 32-bit hexadecimal number with a prefix of 0x, or 4 decimal digits separated by dots. If you do not plan to divide the network into subnets, you can ignore this option, and if you want to use subnets, remember that each system in the network must have the same subnet mask.

Address network card setting IPV4 addresses.

4. Application examples

view device information directly using Ifconfig (Active state)

[[Email protected] ~]# ifconfig eth0      link encap: ethernet  hwaddr 00:0c:29:7a:e4:0e             inet addr:192.168.1.110  Bcast:192.168.1.255  Mask:255.255.255.0           inet6 addr: fe80::20c:29ff:fe7a:e40e/64  scope:link          up broadcast running  multicast  mtu:1500  metric:1           rx packets:1709 errors:0 dropped:0 overruns:0 frame:0           tx packets:783 errors:0 dropped:0 overruns:0 carrier:0           collisions:0 txqueuelen:1000            RX bytes:175347  (171.2 kib)   TX bytes:100454  (98.0  kib)           interrupt:19 base address:0 x2000 lo        link encap:local loopback             inet addr:127.0.0.1  mask:255.0.0.0           inet6 addr: ::1/128 scope:host           up loopback running  mtu:65536   metric:1          rx packets:0 errors : 0 dropped:0 overruns:0 frame:0          tx  packets:0 errors:0 dropped:0 overruns:0 carrier:0            collisions:0 txqueuelen:0           rx bytes :0  (0.0 b)   TX bytes:0  (0.0 b)

Description

Eth0 represents the first network card, where HWaddr represents the physical address of the network card, you can see the current physical address of the network card (MAC address) is 00:0c:29:7a:e4:0e

Inet addr is used to indicate the IP address of the network card, the IP address of this network card is 192.168.1.110, broadcast address bcast:192.168.1.255 , mask address masks : 255.255.255.0.

Lo is the return of the host's bad address, which is generally used to test a network program, but do not want to let the local area network or extranet users can view, can only run on this host and view the network interface used. For example, the HTTPD server is assigned to the bad address, in the browser input 127.0.0.1 will be able to see your Web site. But you can only see that the other host or user of the LAN is unknown.

First line: Connection type: Ethernet (Ethernet) HWADDR (Hardware MAC address)

Second line: IP address, subnet, mask of network card

Third line: Up (for Nic on) RUNNING (network cable on behalf of the NIC is connected) multicast (support multicast) mtu:1500 (Maximum transmission Unit): 1500 bytes.

Line fourth to fifth: Receive, send packet statistics

Line seventh: Receive, send data byte count statistics.


To start and close a specified network card:

# ifconfig eth0 up//start NIC Eth0

# ifconfig eth0 down//off NIC Eth0

SSH login Linux Server operation be careful, shut down can not be turned on, unless you have multiple network cards. If close only to the computer room, the rest of you understand.

[Email protected] ~]# ifconfig eth0 down; Ifconfig eth0 up

If you really want to close the network card and do not want to go to the computer room to open the network card can use ";" Turn off the network card and turn on the network card command to separate, at the same time pay attention to order!


Configure the IP address at the command line:

# ifconfig eth0 192.168.1.56//to eth0 NIC configuration IP ground: 192.168.1.56# ifconfig eth0 192.168.1.56 netmask 255.255.255.0//give eth0 NIC Configuration IP address: 192.168.1.56, plus child mask: 255.255.255.0# ifconfig eth0 192.168.1.56 netmask 255.255.255.0 broadcast 192.168.1.255// Configure the IP address for the ETH0 network card: 192.168.156, plus the child mask: 255.255.255.0, plus a broadcast address: 192.168.120.255

To enable and disable the ARP protocol:

# ifconfig eth0 ARP//Open network card eth0 ARP protocol; # Ifconfig ETH0-ARP//Turn off ARP protocol for NIC Eth0;


In the case of appeal, the NIC information configured with the Ifconfig command does not exist after the machine restarts after the network card restarts. In order to keep the above configuration information in the computer forever, it is necessary to modify the network card configuration file. It is dangerous to execute the command to modify the IP address and shut down the NIC at the same time, unless you know the result of the operation, it is not recommended to use this command in a production environment.


Third, the simple use of the route command

The route command for the Linux system is used to display and manipulate the IP routing table. To achieve communication between two different subnets, you need a router that is connected to two networks, or a gateway to two networks. In Linux systems, routing is usually done to address the problem that the Linux system has a gateway in a local area network that allows the machine to access the Internet, so the IP address of the machine needs to be set to the default route of the Linux machine. Note that the route command is executed directly at the command line to add the route, is not persisted, and is invalidated when the NIC restarts or the machine restarts.

1. Command format

Route [-F] [-P] [Command [Destination] [Mask Netmask] [Gateway] [metric metric]] [if Interface]]

2. Command function

The route command is used for operations based on the kernel IP routing table, and its primary function is to create a static route that lets you specify a host or a network through a network interface, such as eth0. When the "add" or "del" parameter is used, the routing table is modified, and if there are no parameters, the current contents of the routing table are displayed.

3. Command parameters

-N does not parse name

-V displays detailed processing information

-F Display Send message

-C Show Route cache

-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 route hop count.


4. Use Example

Show current route:

[[email protected] ~]# route -nkernel ip routing tabledestination      Gateway         Genmask          Flags Metric Ref    Use  iface192.168.1.0     0.0.0.0          255.255.255.0   u     0      0         0 eth0169.254.0.0     0.0.0.0          255.255.0.0     U      1002   0        0 eth00.0.0.0          192.168.1.1     0.0.0.0          ug&nBsp;   0      0        0  eth0

Where flags is the route flag that marks the state of the current network node.

Flags flags indicate:

U up indicates that this route is currently in the boot state

H host, which indicates that the gateway is a host

G Gateway, which indicates that this gateway is a router

R reinstate route, re-initialized with dynamic routing

D dynamically, this route is dynamically written to

M Modified, this route is dynamically modified by the route daemon or director

! Indicates that this route is currently in a closed state


To add a delete gateway:

[[email protected] ~]# route add -net 224.0.0.0 netmask 224.0.0.0  dev eth0    //adding [[email protected] ~]# route -nkernel  ip routing tabledestination     gateway          genmask         flags metric ref     Use Iface192.168.1.0     0.0.0.0          255.255.255.0   U     0       0        0 eth0169.254.0.0      0.0.0.0         255.255.0.0      U     1002   0         0 eth0224.0.0.0       0.0.0.0         224.0.0.0        U     0       0        0 eth00.0.0.0          192.168.1.1     0.0.0.0          UG    0      0         0 eth0[[email protected] ~]# route del -net 224.0.0.0  netmask 224.0.0.0 dev eth0    //Delete [[email protected] ~]#  route -nkernel ip routing tabledestination     gateway          Genmask          Flags metric ref    use iface192.168.1.0     0.0.0.0          255.255.255.0   U     0       0        0 eth0169.254.0.0      0.0.0.0         255.255.0.0      u     1002   0        0  eth00.0.0.0         192.168.1.1      0.0.0.0         UG    0       0        0 eth0

Delete and Add settings default gateway:

[[email protected] ~]# route -n            //View Gateway kernel ip routing tabledestination     gateway          Genmask          flags metric ref    use iface192.168.1.0      0.0.0.0         255.255.255.0   u      0      0        0  eth0169.254.0.0     0.0.0.0          255.255.0.0     U     1002   0         0 eth00.0.0.0          192.168.1.1     0.0.0.0         ug    0       0        0 eth0[[email protected]  ~]# route del default gw 192.168.1.1   //Deleting a gateway [[email  protected] ~]# route -nkernel ip routing tabledestination      Gateway         Genmask          Flags Metric Ref    Use Iface192.168.1.0      0.0.0.0         255.255.255.0    U     0      0         0 eth0169.254.0.0     0.0.0.0          255.255.0.0     u     1002   0         0 eth0[[email protected] ~]# ping www.baidu.com           //cannot be connected to extranet after deletion ping: unknown host www.baidu.com[[email  protected] ~]# route add  default gw 192.168.1.1   // Add Gateway [[email protected] ~]# ping www.baidu.comping www.a.shifen.com  ( 61.135.169.125)  56 ( bytes of data.64 bytes from 61.135.169.125: ) icmp_seq=1 ttl=55 time=26.4 ms64 bytes from 61.135.169.125: icmp_seq=2  ttl=55 time=26.6 ms

With the Ifconfig and route two commands and profile learning, we can configure common network connection services, and can also conclude that the service configured on the command line will take effect immediately, but once the machine is restarted or the service opportunity is immediately invalidated, Writing to the configuration file does not take effect immediately, but once the service is restarted or the machine takes effect immediately, in both forms we can cross-use two scenarios in the future.


Reference:http://www.cnblogs.com/peida/archive/2013/03/05/2943698.html


Linux System Network Service command (i)

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.