Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Steps for linux to configure a network connection

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "the steps for linux to configure network connection". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The basic configuration of the network generally includes three aspects: configuring the host name, configuring the network card and setting the client name resolution service.

1, configure the hostname

To see the name of the current host, use the hostname command, and to temporarily set the host name, use the "hostname new host name" command, which does not save the new host name to the / etc/sysconfig/network configuration file, so when the system is rebooted, the host name will revert to the host name set in the configuration file.

[root@localhost ~] # hostname

Localhost.localdomain

To temporarily set the hostname to redhat, the implementation command is:

[root@localhost ~] # hostname redhat

[root@localhost ~] # hostname

Redhat

After setting the new hostname, the prompt to the left of "#" cannot be changed synchronously. After logging out and logging back in using logout, the new hostname can be displayed.

If the hostname change is to take effect for a long time, it should be modified directly in the / etc/sysconfig/network configuration file, from which hostname information will be obtained and set when the system starts. The contents of the network configuration file are as follows:

[root@localhost ~] # cat / etc/sysconfig/network

Whether the NETWORKING=yes / / system uses the network service function

HOSTNAME=localhost.localdomain / / set the hostname

GATEWAY=**** / / default gateway

FORWARD_IPV4=false / / whether to enable the forwarding of IP packets. It is usually false when there is a single Nic.

2. Configure the network card

The configuration of network card (network interface card) equipment and network card IP address, subnet mask and default gateway is the main aspect of host network configuration, which is directly related to whether the current host can connect and communicate normally. The configuration of the network card includes the configuration of the network card hardware driver, the IP address and the gateway configuration.

1 > Network profile

The network profile / etc/sysconfig/network is used for the overall configuration of network services, whether to enable network features, whether to enable IP packet forwarding service, and so on. When the network card is not configured or installed, you also need to set up this file so that the local loopback device (lo) can work properly, which is the basis of linux internal communication. The commonly used settings are as follows:

(1) NETWORKING is used to set whether the system uses the network service function. Generally, it should be set to yes, but if set to no, the network will not be available and many system services will not be able to start. The method set in the configuration file is: NETWORKING=yes | no

(2) whether the packet forwarding feature of IPV4 is enabled with the sub-setting of FORWARD_IPV4. When there is only one network card, it is generally set to false. If two network cards are installed and the forwarding function of IP data packets is to be turned on, it is set to true, such as when using a dual network card agent or connecting two network segments to communicate.

FORWARD_IPV4=false | true

Alternatively, you can edit the / etc/sysctl.conf configuration file and change the net.ipv4.ip_forward=0 statement in it to net.ipv4.ip_forward=1 to turn on the kernel's packet forwarding function.

(3) HOSTNAME is used to set the hostname of the host. Note that the hostname set in / etc/hosts is the same as here.

(4) DOMAINNAME is used to set the domain name of the host.

(5) GATEWAY is used to set the gateway IP address of this machine.

(6) GATEWAYDEV is used to set the name of the network card used when communicating with this gateway.

A typical configuration of network:

NETWORKING=yes

FORWARD_IPV4=false

GATEWAY=****

GATEWAYDEV=eth0

HOSTNAME=****

DOMAINNAME=localdomain

2 > configure the device driver module of the network card

In order to make the network card work properly, we must first configure the device driver module of the network card, which is similar to installing the driver of the network card in the windows system.

According to the different bus types, the main Ethernet cards used at present are ISA network card, PCI network card and PCMCIA network card. In order for the network card to work properly, it is necessary to set the alias of the network card device (such as eth0 or eth2, etc.) in the module configuration file (/ etc/modules.conf or / etc/modprode.conf), as well as the name of the driver module to be used by the network card, so that when the kernel needs to use the driver, the kernel service kmod will automatically load the driver module using the system command modprobe (insmod) to make the device work properly.

In the linux system, the module configuration file is used to load the hardware driver modules needed by the system, such as network card, sound card, USB and other devices when the system starts. In general, the installation program of linux can automatically detect and identify the network card, and automatically configure the network card in the module configuration file. The network card driver modules that need to be loaded by network cards of different hardware are different. If there are multiple network cards in the system, the driver module to be loaded should be specified for each network card.

The module configuration file is generally the definition of the load command or alias of the module written to the module. For example, there may be a similar line in the module configuration file: alias eth0 pcnet32

3 > Network card configuration file

The configuration information such as the device name, IP address, subnet mask and default gateway of the network card is saved in the configuration file of the network card. A network card corresponds to a configuration file, which is located in the / etc/sysconfig/network-scripts directory. The configuration file name has the following format: ifcfg- network card type and network card serial number.

The type of Ethernet card is eth, so the configuration file for the first network card is ifcfg-eth0, the configuration file for the second network card is ifcfg-eth2, and so on. The configuration files of other network cards can be obtained by cp command to copy ifcfg-eth0 configuration files, and then modify them as needed.

Linux also supports binding multiple IP addresses to a physical network card. For each bound IP address, a virtual network card is required. The device name of the network card is ethN:M, and the corresponding configuration file name is ifcfg-ethN:M, where N and M are numbers starting from 0, representing their serial numbers. For example, the configuration file of the first virtual network card bound to the first Ethernet card (device name eth0:0) is called ifcfg-eth0:0. Linux supports up to 255IP aliases, and the corresponding configuration file can be obtained by copying the ifcfg-eth0 configuration file and modifying its configuration contents.

In the Nic configuration file, each behavior has a configuration item. The functions and lines of each project in the configuration file are as follows:

Project name

Set valu

Function

DEVICE

Eth0

Represents the device name of the current network card

BOOTPROTO

Static or dhcp

Set the way to obtain the IP address

BROADCAST

****

Broadcast address

IPADDR

****

The IP address of the network card

NETMASK

****

Subnet mask of the Nic

NETWORK

****

The network address of the network where the network card is located

GATEWAY

****

Default gateway

ONBOOT

Yes | no

Set whether to start the network card device when the system starts

To view the contents of the configuration file for the eth0 Nic, the action command is:

[root@localhost ~] # more / etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE= "eth0"

BOOTPROTO=none

DEFROUTE= "yes"

IPV4_FAILURE_FATAL= "yes"

IPV6INIT= "no"

NAME= "System eth0"

NM_CONTROLLED= "yes"

ONBOOT= "yes"

TYPE= "Ethernet"

UUID= "5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03"

LAST_CONNECT=1299464967

IPADDR=****

PREFIX=24

GATEWAY=****

HWADDR=00:1E:EC:A7:8A:5B

DNS1=****

DNS2=****

To bind another IP address of 199.199.199.199 on the eth0 Nic, the binding method is:

[root@localhost ~] # cd / etc/sysconfig/network-scripts

[root@localhost network-scripts] # cp ifcfg-eth0 ifcfg-eth0:0

[root@localhost network-scripts] # cat ifcfg-eth0:0

DEVICE= "eth0:0"

BOOTPROTO=static

ONBOOT=yes

IPADDR=199.199.199.199

NETMASK=255.255.255.0

To temporarily bind an IP address to the Nic, use the following command:

Ifconfig eth0 add 199.199.199.199 netmask 255.255.255.0

Set the IP address of the network card

Ifconfig Nic device name IP address netmask subnet mask

This command will not modify the configuration file of the network card, and the IP address set is valid only for this time. If you restart the system or the network card is prohibited, its IP address will be set to the IP address specified in the network card configuration file.

Disable the network card: ifconfig network card device name down

Or ifdown network card device name

Start the network card: ifconfig network card device name up

Or ifup network card device name

Set the default gateway

A gateway is a device that connects hosts in the current network to other network hosts and communicates with them. After setting the IP address and subnet mask of the host, you can communicate with other hosts on the same network segment, but you cannot communicate with the hosts of other network segments at this time. In order to achieve communication with hosts of different network segments, you must set a default gateway address. The gateway address must be the address of the current network, not the address of another network segment.

Setting the default gateway, that is, setting the default route, can be achieved by using the route command provided by the linux system, which is mainly used to add or remove routing information.

1 > View current routing information

[root@localhost ~] # route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

59.79.87.0 * 255.255.255.0 U 100 eth0

Default 59.79.87.254 0.0.0.0 UG 0 0 0 eth0

2 > add / remove default gateway

Add a default gateway:

Route add default gw gateway IP address dev Nic device name

Delete the default gateway:

Route del default gw Gateway IP address

For example, to set the default gateway address of the network card eth0 to 59.79.87.1, the implementation command is:

[root@localhost] # route add default gw 59.79.87.1 dev eth0

[root@localhost ~] # route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

59.79.87.0 * 255.255.255.0 U 100 eth0

Default 59.79.87.1 0.0.0.0 UG 0 0 0 eth0

Default 59.79.87.254 0.0.0.0 UG 0 0 0 eth0

To delete the default gateway, the implementation command is as follows:

[root@localhost] # route del default gw 59.79.87.1

3 > add / remove routing information

Add a route record to the current routing table of the system. The command usage is as follows:

Route add-net network address netmask subnet mask [dev Nic device name] [gw gateway]

To delete a route record, the command usage is:

Route del-net network address netmask subnet mask

Bind IP and MAC addresses

Binding IP with MAC address can prevent the misuse of IP address. Its usage:

First create a / etc/ethers file with the content of "IP address mac address"

Then run the arp-f command, and the binding takes effect.

Modify the MAC address of the network card

First deactivate the network card device you want to modify, and then use the following command format to modify the settings:

Ifconfig Nic device name hw ether MAC address

Set up the DNS server

Its configuration file is / etc/resolv.conf

[root@localhost ~] # cat / etc/resolv.conf

# Generated by NetworkManager

Nameserver 192.168.252.253

Nameserver 192.168.252.254

Hosts file

/ etc/hosts is an early method of host name resolution, which includes the correspondence between IP address and host name. During name resolution, the system directly reads the corresponding records of the IP address and hostname set in the file. The behavior comment line that begins with "#" in the file, the rest is a record for each behavior, the IP address is on the left, and the host name, host full name and host alias are on the right. The default content of this profile is:

[root@localhost ~] # more / etc/hosts

127.0.0.1 localhost.localdomain localhost localhost4

:: 1 localhost6.localdomain6 localhost6

61.55.167.141 bbs.chinaunix.net

Specify name resolution order

You can use the "hosts:" configuration option in the / etc/nsswitch.conf configuration file to specify that the default parsing order is the hosts file, the DNS server.

[root@localhost ~] # grep hosts / etc/nsswitch.conf

# hosts: db files nisplus nis dns

Hosts: files dns # where files representatives use hosts files for name resolution

This is the end of the "steps for linux to configure a network connection". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report