← Back to list

Getting the Fully Qualified Domain Name (FQDN) using Python — socket module

When working with networked systems, we often need to retrieve the full name of a machine, which includes the hostname and domain.  This…

Balakrishna · 2024-10-17 12:00 · 0 claps · 1.9 min read paywalled
#python #fqdn #networking #socket-module #hostname
Open on Medium ↗
Wiki topics: 🔒 · Cybersecurity

Getting the Fully Qualified Domain Name (FQDN) using Python — socket module

Getting the Fully Qualified Domain Name (FQDN) using Python — socket module

Getting the Fully Qualified Domain Name (FQDN) using Python — socket module

When working with networked systems, we often need to retrieve the full name of a machine, which includes the hostname and domain. This is called the Fully Qualified Domain Name (FQDN). In Python, getting the FQDN is incredibly simple, thanks to its built-in socket module.

What is a Fully Qualified Domain Name (FQDN)?

An FQDN is the complete domain name for a specific computer or host on the internet. It includes both the hostname and the domain name. For example, if the hostname is mycomputer and the domain is example.com, the FQDN would be mycomputer.example.com.

Why Do We Need It?

The FQDN is essential in many situations:

  • To uniquely identify a machine on a network.
  • For network configurations, such as setting up servers.
  • In security configurations, like SSL certificates.

How to Get FQDN in Python

Python makes it easy to fetch the FQDN using the socket module. Here’s how you can do it:

import socket

fqdn = socket.getfqdn()
print(fqdn)

Step-by-Step Breakdown:

  1. Import the socket module: Python’s socket module provides various networking functionalities, including retrieving the FQDN.
  2. Get the FQDN: The function socket.getfqdn() fetches the fully qualified domain name.
  3. Print the FQDN: Finally, it prints the full name, including both the hostname and the domain.

Example Output

When you run the above code, you might get output like this:

mycomputer.example.com

If the machine is not part of a domain, you might only see the hostname like:

mycomputer

Here are some Practical Use Cases:

  • System Monitoring: If you’re building a script to monitor multiple servers, fetching the FQDN helps ensure you’re getting the correct machine.
  • Logging: When logging network events, storing the FQDN ensures clarity about which machine the event occurred on.
  • Security Checks: Many security tools use FQDNs to verify the identity of machines, ensuring that they belong to a trusted domain.

Conclusion

In just a few lines of Python code, you can easily retrieve the Fully Qualified Domain Name (FQDN) of a machine. This is particularly useful in networking, system administration, and security. Understanding and using FQDNs can help make your applications and systems more robust and reliable.

Thank you for reading!

If this article helped you, take a moment to try it out yourself. Write a small Python script to fetch your system’s FQDN using the socket module. Experiment with other networking functions and see how they work! 🖥️💡

Don’t forget to give this article a clap 👏 and follow me for more Python, SQL and tech-related content. Your comments and feedback are always welcome — let me know your thoughts below! 💬👇


메타데이터
post_id
740716454658
slug
getting-the-fully-qualified-domain-name-fqdn-using-python-socket-module-740716454658
url
https://medium.com/@balakrishna0106/getting-the-fully-qualified-domain-name-fqdn-using-python-socket-module-740716454658
canonical_url
https://medium.com/@balakrishna0106/getting-the-fully-qualified-domain-name-fqdn-using-python-socket-module-740716454658
author_url
https://medium.com/@balakrishna0106
status
ok
fetched_at
2026-06-27 07:40:21