Use Shell/python to get Hostname/fqdn explanation (RPM)

Source: Internet
Author: User
Tags fully qualified domain name

The Linux hostname and FQDN (Fully qualified Domain Name) have been confusing for a long time, and today they are dedicated to figuring out the details of their use.

First, set Hostname/fqdn

Setting up hostname in a Linux system is simple, such as:

$ hostname florian

If you want to set the FQDN, you need to configure the/etc/hosts.

$ cat /etc/hosts127.0.0.1 localhost192.168.1.1 florian.test.com florian

The format of the/etc/hosts configuration file is:

ip fqdn [alias]...

That is, the first list is the host IP address, the second is the host FQDN address, the third column is an alias later, you can omit it, or at least include hostname.

The first behavior of the configuration item for the above configuration file is the configuration of localhost, and the second behavior of the host name Florian Configuration fqdn=florian.test.com,ip=192.168.1.1.
As for the domain name suffix of the FQDN, it is best to maintain consistency with the hostname configuration of the file/etc/sysconfig/network:

$ cat /etc/sysconfig/networkNETWORKING=yesHOSTNAME=test.com
Second, view Hostname/fqdn

After the configuration is complete, you can use the shell command to view the hostname and FQDN:

-f florianflorian.test.com

Use ping to test whether the IP mapping of the hostname is successful.

$ ping florianPING florian.test.com (192.168.1.1) 56(84) bytes of data.$ ping florian.test.comPING florian.test.com (192.168.1.1) 56(84) bytes of data.

You can also use the Python command to get hostname and FQDNs.

$ python python2.6.6 (r266:84292, Dec72011,20:48: 22) [GCC 4.4. 6 20110731 (Red Hat 4.4. 6-3)] on linux2type  "help",  " Copyright ", or " license " for more information. >>> import socket>>> socket.gethostname ()  ' Florian ' >>> Socket.getfqdn ()  Florian.test.com '               
Third, using IP settings hostname the FQDN problem

The above describes how to set the hostname and FQDN normally , but sometimes the IP address is used directly as the hostname, which can be a bit different at this point.

192.168.1.1$ hostname && hostname -f192.168.1.1192.168.1.1

We found that using the IP as hostname, using the shell command query hostname and FQDN are IP addresses!!! This is because the DNS protocol resolves the contents of the hostname, and when it is found to be an IP address, the/etc/hosts file is no longer queried.

Using Python again, you will find that the FQDN obtained by Python is still florian.test.com!!!

$ pythonpython2.6.3 Mr266:84292, Dec72011,20:48: 22) [GCC 4.4. 6 20110731 (Red Hat 4.4. 6-3)] on linux2type  "help",  " Copyright ", or " license " for more information. >>> import socket >>> socket.gethostname ()  ' 192.168.1.1 ' >>> Socket.getfqdn ()  ' florian.test.com '            

Even refreshing the DNS cache is useless:

$ service nscd reload

Comment The second line of the/etc/hosts file:

cat /etc/hosts127.0.0.1 localhost# 192.168.1.1 florian.test.com florian

Refresh the DNS cache:
$ Service NSCD Reload

We found the FQDN back to normal.

$ pythonpython2.6.3 Mr266:84292, Dec72011,20:48: 22) [GCC 4.4. 6 20110731 (Red Hat 4.4. 6-3)] on linux2type  "help",  " Copyright ", or " license " for more information. >>> import socket >>> socket.gethostname ()  ' 192.168.1.1 ' >>> Socket.getfqdn ()  ' 192.168.1.1 '            

The reason for this behavior is that the logic and DNS of the Python parsing FQDN are not exactly the same, it will query the corresponding IP address according to hostname, and then get the configuration line corresponding to the IP address in/etc/hosts (the first row is valid). Then parse the FQDN column and the alias column and return the first containing character '. ' The value of the corresponding column.

Therefore, when using IP settings hostname, there are two points to note:

    • First, set the hostname to the IP address
    • Next, remove the configuration item that contains the IP in the/etc/hosts

For the sake of insurance, we can add the following configuration in the/etc/hosts as far forward as possible:

cat /etc/hosts127.0.0.1 localhost192.168.1.1 192.168.1.1

This way, even if a configuration item that contains the IP does not take effect after that, Python prioritizes the second line of configuration items and obtains the same FQDN address as the IP address. Of course, using the shell command hostname to get the FQDN is also not an error, because hostname has been set to the IP address form.

Iv. references
    • Linux under Configuration fqdn:https://onebitbug.me/2014/06/25/settings-fqdn-in-linux/
    • Linux Fundamentals: Setting FQDN and hostname:http://www.chenshake.com/linux-foundation-set-fqdn-hostname/
    • Is it valid for a hostname to start with a digit?: http://serverfault.com/questions/638260/is-it-valid-for-a-hostname-to- Start-with-a-digit

Http://www.cnblogs.com/fanzhidongyzby/p/5154443.html

Use Shell/python to get Hostname/fqdn explanation (RPM)

Related Article

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.