Network configuration of NIC in Linux system
The Ifconfig command makes the Linux kernel aware of some network interfaces such as software loopback and network cards, so that Linux can use them.
In addition to these usages described above, the ifconfig command is used to monitor and change the state of the network interface, and can also carry many command-line arguments. The following is a generic invocation syntax for ifconfig:
#ifconfig interface [[-net-host] address [parameters]
Where interface is the network interface name: Address is the host name or IP address assigned to the specified interface. The hostname used here is parsed into their peer IP address, which is required. The-net and-host parameters tell Ifconfig to use the address as a network number or as a host address, respectively.
If the call to the Ifconfig command is followed by only the port device name, then it will show the configuration of the ports, if not with any parameters, the Ifconfig command will display all the information of the interface configured so far, and if the-a option is present, the currently inactive interface can also be displayed.
A ifconfig call that checks the Ethernet interface eth0 can get the following output:
#ifconfig eth0
Eth0 Link encap 10Mbps Ethernet HWaddr 00:00:c0:90:b3:44
inet addr xxx.xxx.xxx.xxx bcast xxx.xxx.xxx.255 Mask 255.255.255.0
Up broadcast RUNNING MTU Metric 0
RX Packets 3136 Errors 217 dropped 7 overrun 26
TX packets 1752 Errors dropped 0 overrun 0
(Note: Where XXX.XXX.XXX.XXX is an IP address)
The MTU and metric two columns show the maximum data transfer value and interface metrics for the current eth0 interface. The interface metric represents the cost of sending a grouping on this path. The route is not currently used in the kernel, but may be used later. The RX (receive packets) and TX (transmit packets) are two lines that show the number of receive and transmit groupings, as well as the number of packet errors, the number of lost groupings (one may be due to low memory), and the number of overrun (usually occurs when the receiver receives data faster than the core processing speed).
Parameters represents the various parameters supported by Ifconfig, which can be used to easily monitor and change the state of a network interface.
Ifconfig command-line arguments:
- Up activates the specified interface
- Down to close the specified interface. 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 remove all the routing information from the core routing table from that interface.
- Netmask Mask Sets the IP netmask for the interface. 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.
- Pointpoint opens the point-to-point mode for the specified interface. It tells the core that the interface is a direct connection to another machine. When an address is included, the address is assigned to the machine at the other end of the list. If no address is given, open the Pointpoint option for this specified interface. A minus sign in front indicates that the Pointpoint option is turned off.
- Broadcast address when an address is used, the broadcast addresses for this interface are set. If no address is given, open the Iff_broadcast option for this specified interface. Preceded by a minus sign indicates that this option is turned off.
- Metric number sets the interface measure to an integer. A measure represents the cost of sending a grouping on this path. The cost of routing is not yet used in the kernel, but will be in the future.
- The MTU bytes sets the maximum number of bytes that an interface can handle in a single transmission to an integer bytes. Currently the core network code does not handle IP segmentation, so be sure to set the MTU (Maximum data transfer unit) value is large enough
- ARP turns on or off the ARP protocol used on the specified interface. Precede with a minus sign to turn this option off.
- Allmuti Open the non-differentiated mode for the specified interface. Opening this mode allows the interface to send all the traffic on the network to the core, rather than just sending the information from your machine to the core. Preceded by a minus sign indicates the option to turn off
- HW sets the hardware address for the specified interface. The ASCII character of the hardware type name and the secondary hardware address peer must follow this keyword. Currently supports Ethernet (ether), AMPR, ax.25, and PPP traliers to turn on the tracker on the Ethernet frame. It is not yet implemented in a Linux network, and it is generally not necessary to use all of these configurations.
Ifconfig can set everything needed only by the interface name, netmask, and assigned IP address. When Ifconfig slips or has a complex network, just reset most parameters.
Check network status using netstat
The following is a useful command--netstat, using the netstat command to monitor TCP/IP network configuration and operating conditions. It can display the kernel routing table, the active network status, and the useful statistics for each network interface. Please read the man page for details. -a displays information about all Internet connections, including those that are listening-I displays statistics for all network devices
-C continuously displays the update status of the network. This parameter uses the Netstat output network status list once per second until the program is interrupted
-N Displays remote address, local address, and port information in digital/raw form instead of resolving host name and server-o display counter end time and fallback for each network connection (back off) Case-r Display kernel routing table
-T displays only TCP socket information, including the information being monitored-u only displays UDP socket information-v display netstat version information-W display raw (RAW) socket information-X display UNIX domain socket information
Ifconfig detailed
Ifconfig is a tool used to view, configure, enable, or disable a network interface, which is extremely common. This tool can be used to temporarily configure the network card IP address, mask, broadcast address, Gateway and so on. It can also be written to a file (such as/etc/rc.d/rc.local), so that after the system boots, it will read the file, set the IP address for the network card
1.ifconfig Viewing network interface status
Ifconfig If no parameters are received, the current network interface is output;
[Email protected] ~]# ifconfig
Eth0 Link encap:ethernet HWaddr 00:03:0d:27:86:41
inet addr:192.168.1.86 bcast:192.168.1.255 mask:255.255.255.0
Inet6 ADDR:FE80::203:DFF:FE27:8641/64 Scope:link
Up broadcast RUNNING multicast mtu:1500 metric:1
RX packets:618 errors:0 dropped:0 overruns:0 frame:0
TX packets:676 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:491238 (479.7 KiB) TX bytes:86286 (84.2 KiB)
Interrupt:5 Base address:0x8c00
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:16436 metric:1
RX packets:1692 errors:0 dropped:0 overruns:0 frame:0
TX packets:1692 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3174550 (3.0 MiB) TX bytes:3174550 (3.0 MiB)
Explanation:
Eth0 represents the first network card, wherein the HWADDR represents the physical address of the network card, you can see the current network card physical address (MAC address) is 00:03:0d:27:86:41;
Inet addr is used to indicate the IP address of the network card, the IP address of this network card is 192.168.1.86,
Broadcast address, bcast:192.168.1.255, mask address mask: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 see that the other host or user of the local area network is unknown;
If you want to know all the network interface of the host, please use the following command;
[[Email protected] ~] #ifconfig-A
If you want to view a port, such as viewing the status of Eth0, you can use the following method;
[[Email protected] ~] #ifconfig eth0
2.ifconfig Configuring the Network interface
Ifconfig can be used to configure the network interface IP address, mask, gateway, physical address, etc. it is worth to use Ifconfig to specify the IP address for the network card, which is only used to debug the net, and does not change the system about the network card configuration file. If you want to fix the IP address of the network interface, there are currently three methods: one is to modify the IP address through each release and version-specific tool, and the other is to modify the configuration file of the network interface directly, and the third is to modify the specific file and add the ifconfig instruction to specify the IP address of the NIC. For example, in Redhat or Fedora, the name of the Ifconfig is written into the/etc/rc.d/rc.local file;
Ifconfig How to configure network ports:
Ifconfig tools to configure the network interface method is through the parameters of the directive to achieve the purpose, we only say the most commonly used parameters;
Ifconfig Network port IP address HW MAC address netmask Mask address broadcast broadcast address [Up/down]
* Example One:
For example, we use Ifconfig to debug the address of the eth0 NIC.
[[Email protected] ~] #ifconfig eth0 down
[[Email protected] ~] #ifconfig eth0 192.168.1.99 broadcast 192.168.1.255 netmask 255.255.255.0
[[Email protected] ~] #ifconfig eth0 up
[[Email protected] ~] #ifconfig eth0
Eth0 Link encap:ethernet HWaddr 00:03:0d:27:86:41
inet addr:192.168.1.99 bcast:192.168.1.255 mask:255.255.255.0
Inet6 ADDR:FE80::203:DFF:FE27:8641/64 Scope:link
Up broadcast RUNNING multicast mtu:1500 metric:1
RX packets:618 errors:0 dropped:0 overruns:0 frame:0
TX packets:676 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:491238 (479.7 KiB) TX bytes:86286 (84.2 KiB)
Interrupt:5 Base address:0x8c00
Note: The above example we explain;
First line: Ifconfig eth0 down indicates that if the eth0 is active, drop it. This command is equivalent to Ifdown eth0;
The second line: use Ifconfig to configure the eth0 IP address, broadcast address and netmask;
Third line: Use ifconfig eth0 up to activate eth0; This command is equivalent to Ifup eth0
Line four: Use ifconfig eth0 to check the status of Eth0;
Of course, you can also use directly in the command IP address, netmask, broadcast address at the same time, to activate the network card, to add up parameters, such as the following example;
[[Email protected] ~] #ifconfig eth0 192.168.1.99 broadcast 192.168.1.255 netmask 255.255.255.0 up
* Example Two: In this example, we have to learn to set the network IP address at the same time, learn to set the physical address of the network card (MAC address);
For example, we set the network card eth1 IP address, netmask, broadcast address, physical address and activate it;
[[Email protected] ~] #ifconfig eth1 192.168.1.252 hw ether 04:64:03:00:12:51 netmask 255.255.255.0 broadcast 192.168.1.25 5 up or
[[Email protected] ~] #ifconfig eth1 hw ether 04:64:03:00:12:51
[[Email protected] ~] #ifconfig eth1 192.168.1.252 netmask 255.255.255.0 broadcast 192.168.1.255 up
Where HW is followed by the network interface type, ether represents the Ethernet, but also support ax25, ARCnet, Netrom, etc., see Man ifconfig for details;
3. How to configure the virtual network interface with Ifconfig
Sometimes we need to configure the virtual network interface to meet different needs, such as we use a different IP address to run multiple httpd servers, it is necessary to use virtual address, so that the same IP address, if you open two httpd server, you want to specify the port number.
Virtual network interface refers to a network interface to specify multiple IP addresses, the virtual interface is such eth0:0, Eth0:1, Eth0:2 ... eth1n. Of course you specify multiple IP addresses for eth1, i.e. eth1:0, eth1:1, Eth1:2 ... And so on
In fact, with Ifconfig for a network adapter to configure multiple IP addresses, the use of the previous we said the use of ifconfig, this is relatively simple, see the following example;
[[Email protected] ~] #ifconfig eth1:0 192.168.1.251 hw ether 04:64:03:00:12:51 netmask 255.255.255.0 broadcast 192.168.1.255 up or
[[Email protected] ~] #ifconfig eth1 hw ether 04:64:03:00:12:51
[[Email protected] ~] #ifconfig eth1 192.168.1.251 netmask 255.255.255.0 broadcast 192.168.1.255 up
Note: When specifying, specify a different physical address for each virtual NIC;
In Redhat/fedora or Redhat/fedora-like systems, you can put the configuration network IP address, broadcast address, mask address, physical address, and activation network interface together in one sentence and write to/etc/rc.d/rc.local. such as the following example;
Ifconfig eth1:0 192.168.1.250 hw ether 00:11:00:33:11:44 netmask 255.255.255.0 broadcast 192.168.1.255 up
Ifconfig eth1:1 192.168.1.249 hw ether 00:11:00:33:11:55 netmask 255.255.255.0 broadcast 192.168.1.255 up
Explanation: The above is for the ETH1 network interface, set up two virtual interface, each interface has its own physical address, IP address ...
4. How to use Ifconfig to activate and terminate a network interface connection
The use of the ifconfig command to activate and terminate a network interface, followed by a network interface, followed by a down or up parameter, prevents or activates the corresponding network interface. Of course, you can also use special tools Ifup and Ifdown tools;
[[Email protected] ~] #ifconfig eth0 down
[[Email protected] ~] #ifconfig eth0 up
[[Email protected] ~] #ifup eth0
[[Email protected] ~] #ifdown eth0
This is true for activating other types of network interfaces, such as Ppp0,wlan0, but is only valid for IP-specified NICs.
Note: The IP assigned to DHCP is also activated by the network tools that come with each release, and of course you have to install the DHCP client; this you we should understand;
Like Redhat/fedora.
[[email protected] ~]#/etc/init.d/network start
Slackware release version;
[Email protected] ~]#/etc/rc.d/rc.inet1