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

Linux network configuration and command

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

Share

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

LINUX network configuration

Some configuration files:

/ etc/services: service name and port profile

/ etc/hosts: the configuration file of the earliest hostname corresponding to ip

As soon as you mention changing hostname, you first think of modifying the / etc/hosts file and thinking that the configuration file for hostname is / etc/hosts. Actually, it's not.

The hosts file acts like DNS, providing a correspondence between the IP address and the hostname. In the early days, there were few Internet computers, and there was enough to hold all the networked computers in a single hosts file. However, with the development of the Internet, this is far from enough. Then came the distributed DNS system. The DNS server provides a similar IP address to domain name correspondence. You can man hosts for details.

The Linux system will query the / etc/hosts file before issuing a domain name resolution request to the DNS server, and if there is a corresponding record in it, it will use the record in hosts. The / etc/hosts file usually contains this record:

127.0.0.1 localhost.localdomain localhost

The hosts file format is one record per line, which is the IP address hostname aliases, separated by white space characters, and aliases is optional.

127.0.0.1 to localhost is not recommended to change, because many applications will use this, such as sendmail, after the modification, these programs may not work properly.

After modifying the hostname, if you want to access it with newhostname on the local machine, you must add a record of newhostname in the / etc/hosts file. For example, the IP of my eth0 is 192.168.1.61, and I modify the hosts file as follows:

# hostname blog.infernor.net

# cat / etc/hosts

127.0.0.1 localhost.localdomain localhost

192.168.1.61 blog.infernor.net blog

In this way, I can access the local machine through blog or blog.infernor.net.

Judging from the above, / etc/hosts is not directly related to setting up hostname, and the / etc/hosts file is only used when you want to access yourself with the new hostname on your local computer. There is no necessary connection between the two.

RHEL has one more problem.

When I started testing, I only modified / etc/hosts, adding 192.168.1.61 blog.infernor.net blog to it, while / etc/sysconfig/network left it as it was, that is, the HOSTNAME=localhost.localdomain inside. When I rebooted the system, I found that hostname had been changed to blog.infernor.net. If you look at it this way, you really think / etc/hosts is the configuration file for hostname. The problem was finally found in the startup script / etc/rc.d/rc.sysinit.

Hostname is set at the beginning of the rc.sysinit file.

If [- f / etc/sysconfig/network]; then

. / etc/sysconfig/network

Fi

If [- z "$HOSTNAME"-o "$HOSTNAME" = "(none)"]; then

HOSTNAME=localhost

Fi

The hostname value in / etc/sysconfig/network is indeed used. But there is also a paragraph about setting up hostname.

Ipaddr=

If ["$HOSTNAME" = "localhost"-o "$HOSTNAME" = "localhost.localdomain"]

; then

Ipaddr=$ (ipaddr show to 0 scope global 0 scope global | awk'/ [[: space:]] inet

/ {print gensub ("/. *", "," g ", $2)}')

If [- n "$ipaddr"]; then

Eval $(ipcalc-h $ipaddr 2 > / dev/null)

Hostname ${HOSTNAME}

Fi

Fi

The script determines whether the hostname is localhost or localhost.localdomain, and if so, the hostname corresponding to the interface IP address will be used to reset the system's hostname. That's the problem. The default hostname for my / etc/sysconfig/network is 192.168.1.61 for localhost.localdomain,eth0, while 192.168.1.61 is recorded in / etc/hosts. So I replaced hostname with the record 192.168.1.61.

I guess that's why many people mistake / etc/hosts for hostname's configuration file.

Hostname with option query

Hostname's-s-f-I and other options all use the / etc/hosts or DNS system, which is a little far from the hostname we are talking about, and is prone to misunderstandings. You can check the details on man hostname.

Summarize the safest way to change the hostname:

1. Stop running software related to the hostname, such as MySQL.

two。 Use hostname newname to set the hostname first.

3. Modify or add the ip newname line in / etc/hosts and check the original 127.0.0.1 line localhost loopback setting.

4. Modify the HOSTNAME=newname in / etc/sysconfig/network.

5. Restart the machine and confirm that the modification is successful.

=

/ etc/sysconfig/network

$cat / etc/sysconfig/network

Whether to activate the network for NETWORKING=yes

HOSTNAME=cloudweb26.idc1.haodf.net Hostnam

NETWORKING_IPV6=no

=

/ etc/resolv.conf: native DNS parsing

It is a DNS client configuration file that sets the IP address and DNS domain name of the DNS server, as well as the search order of the host's domain name. This file is the configuration file used by the domain name resolver (resolver, a library that resolves IP addresses based on hostnames). Its format is simple, with each line beginning with a keyword, followed by one or more parameters separated by spaces.

There are four main keywords for resolv.conf, which are:

Nameserver / / defines the IP address of the DNS server

Domain / / define a local domain name

Search / / defines the search list of domain names

Sortlist / / sort the returned domain names

Let's give an example of / etc/resolv.conf:

Nameserver 10.3.3.99

Nameserver 10.1.0.88

Search idc1.baidu.net baidu.net

Options single-request-reopen

The most important is the nameserver keyword, if you do not specify nameserver, you can not find the DNS server, other keywords are optional.

Nameserver means that the host specified by this address when resolving a domain name is the domain name server. The domain name server is queried in the order in which it appears in the file, and the following nameserver is queried only if the first nameserver does not respond.

Search has several parameters that indicate the order in which domain names are queried. When you query a host without a domain name, the host will look in the domain declared by search.

Domain declares the domain name of the host. It is used by many programs, such as mail systems, and when performing DNS queries for hosts that do not have a domain name. If there is no domain name, the hostname will be used, delete all at the first point (.) The previous content.

Domain and search cannot coexist; if they exist at the same time, the latter will be used.

Sortlist allows specific sorting of the results of the domain name. Its parameters are network / mask pairs, allowing for arbitrary ordering.

"search domainname.com" means that when a hostname that does not include a full domain name is provided, the suffix of domainname.com is added to the hostname; "nameserver" means that the host specified by that address when resolving the domain name is the domain name server. The domain name server is queried according to the order in which it appears in the file.

Where domainname and search can exist at the same time, or there can be only one; nameserver can specify multiple

= = Separator

/ etc/nsswitch.conf: this file decides whether to use the setting of / etc/hosts or / etc/resolv.conf first!

=

# cat / etc/sysconfig/network-scripts/ifcfg-eth0

Name of DEVICE=eth0 network card

The MAC address of the HWADDR=00:0c:29:dd:83:d4 network card, which is maintained from one to the fixed address in the network card device ROM

TYPE=Ethernet

ONBOOT=yes whether some devices are activated with OS startup

NM_CONTROLLED=yes MM is the abbreviation of NetworkManger. It is recommended to set it to no.

BOOTPROTO=static Boot Protocol: {none | static | dhcp | bootp}

Manual static dynamic dynamic

NETMASK=255.255.255.0

IPADDR=192.168.2.24

GATEWAY=192.168.2.1

Whether USERCTL=no allows ordinary users to manage the interface, no: no

Whether PEERDNS=yes allows the DNS server address provided by DHCP server to overwrite the local / etc/resolv.conf file

Whether IPV6INIT=no uses IPV6 or not

IPV6_AUTOCONF=yes automatically configures IPV6

The following command takes out the ip

# ifconfig eth0 | grep "inet addr" | awk-F [: "] +'{print $4}'

192.168.2.100

=

Common network commands:

1: host: this instruction can be used to find out the IP of a host name. For example, when we want to know the IP of www.baidu.com, we can do this:

Host-a www.baidu.com

Host-a-v www.baidu.com

-a: displays all DNS information

-v: displays the specified details

The principle of check is parsed according to the ip of DNS specified in native / etc/resolv.conf.

The 2:nslookup command is a commonly used domain name query tool, which is the command used to check DNS information.

Nslookup has two working modes, namely, "interactive mode" and "non-interactive mode".

In "interactive mode", users can query the information of various hosts and domain names from the domain name server, or output a list of hosts in the domain name. Enter the interactive mode, enter the nslookup command directly, and enter the interactive mode without any parameters.

In "non-interactive mode", users can only get a specific name or required information for a host or domain name.

# nslookup www.baidu.com

Nslookup will connect to the default domain name server (that is, the first dns address of / etc/resolv.conf)

3:dig

Dig [options] FQDN [@ server]

Options and parameters:

@ server: if you do not use the / etc/resolv.conf setting as the DNS query, you can enter another IP here

Options: there are many related parameters, including + trace,-t type and-x.

+ trace: from. Start tracking.

-t type: the main types of data queried are mx, ns, soa and so on.

-x: query anti-solution information, a very important project!

# dig linux.vbird.org

; DiG 9.7.0-P2-RedHat-9.7.0-5.P2.el6_0.1 linux.vbird.org

;; global options: + cmd

;; Got answer:

;;-> > HEADER

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