Dev.to Security 🔐 Cybersecurity 👁 2 📖 11 min read

🐧 🌐 How to Configure a Local FQDN on Linux Using /etc/hosts

In development environments, staging infrastructure, corporate networks, and private networks, it is common to need access to an application through a Fully Qualified Domain Name (FQDN), even when the application is host

In development environments, staging infrastructure, corporate networks, and private networks, it is common to need access to an application through a Fully Qualified Domain Name (FQDN), even when the application is hosted on a private IP address and does not have a public DNS record pointing to that address.

For example, instead of accessing an application through:

https://192.168.10.50:8443

You may want to access it using:

https://app.example.com:8443

One of the simplest ways to achieve this is by using the Linux /etc/hosts file.

In this article, we will explore how local hostname resolution works, configure an FQDN to point to a private IP address, validate network connectivity, and discuss scenarios where this approach is useful, including applications that rely on HTTPS and OAuth 2.0 authentication.

1. Understanding FQDN, DNS, and /etc/hosts

Before configuring the environment, it is important to understand three concepts.

FQDN (Fully Qualified Domain Name)

An FQDN is the complete domain name of a host within the DNS hierarchy.

Example:

app.example.com

In this example:

  • app: hostname or subdomain.
  • example.com: registered domain.
  • app.example.com: complete name used to identify the service.

An FQDN allows applications and users to access services without having to know their IP addresses.

DNS (Domain Name System)

DNS is the system responsible for translating domain names into IP addresses.

For example:

app.example.com → 192.168.10.50

When a computer needs to access a domain, it queries the name resolution mechanisms configured on the operating system.

Depending on the configuration, name resolution can happen through internal DNS servers, external DNS servers, or local sources.

The /etc/hosts File

The /etc/hosts file is a local hostname resolution mechanism available on Linux and Unix-like systems.

It allows administrators to manually associate a hostname or FQDN with an IP address without creating a record on a DNS server.

Example:

192.168.10.50 app.example.com

In this case, the operating system resolves app.example.com to 192.168.10.50.

This configuration is local: it does not create a global DNS record or change hostname resolution on other computers.

2. When Should You Use This Approach?

Configuring a local FQDN can be useful in several scenarios.

2.1. Application Development

During development, you may need to simulate an environment that is closer to production.

Instead of accessing:

http://localhost:3000

You can use:

http://app.example.com:3000

This can be particularly useful when testing:

  • Virtual host configurations.
  • Domain-based cookies.
  • HTTP redirects.
  • OAuth and OpenID Connect integrations.
  • CORS configurations.
  • Application behavior that depends on the hostname.

2.2. Staging and Testing Environments

In staging environments, applications often run on private IP addresses without public DNS records.

Example architecture:

app.staging.example.com → 10.10.20.15
api.staging.example.com → 10.10.20.16
auth.staging.example.com → 10.10.20.17

The /etc/hosts file allows you to simulate these records during testing without modifying the corporate DNS infrastructure.

2.3. Corporate Infrastructure and Private Networks

In private networks, it is common to host applications using RFC 1918 private IP addresses, such as:

Range Address block
Private Class A 10.0.0.0/8
Private Class B 172.16.0.0/12
Private Class C 192.168.0.0/16

Even when using private IP addresses, applications can be accessed through FQDNs associated with an organization's registered domain.

This is useful for administrative dashboards, internal systems, APIs, monitoring tools, and infrastructure services.

2.4. Google OAuth and OpenID Connect Integrations

This is a particularly interesting scenario.

Some identity providers require application URLs used in authentication flows to comply with specific requirements.

In the case of Google OAuth, web application redirect URIs must use HTTPS, cannot use a raw IP address as the hostname, and must exactly match the authorized URLs configured in the OAuth client.

For example:

https://app.example.com:8443/auth/google/callback

An application can be hosted on a private IP address while users access it through an FQDN.

The authentication flow can work as follows:

  1. The user accesses the application using the FQDN.
  2. The application initiates the OAuth flow.
  3. The browser redirects the user to the identity provider.
  4. The user completes authentication.
  5. The identity provider redirects the browser to the registered redirect URI.
  6. The browser resolves the FQDN again using its local configuration.
  7. The application receives the authentication response.

It is important to note that the domain must comply with the identity provider's requirements. For Google, this includes using a valid domain controlled by the organization and meeting applicable domain verification requirements.

Official reference: Google OAuth 2.0 — Web Server Applications.

3. Configuring an FQDN on Linux

Let's use the following example:

Parameter Value
FQDN app.example.com
Private IP 192.168.10.50
Port 8443
Protocol HTTPS
Access URL https://app.example.com:8443
Configuration file /etc/hosts

3.1. Accessing the hosts File

The file is located at:

/etc/hosts

Before making any changes, create a backup:

sudo cp -a /etc/hosts /etc/hosts.backup

Now open the file using an editor:

sudoedit /etc/hosts

Alternatively, you can use:

sudo vi /etc/hosts

3.2. Adding the Hostname Mapping

Add the following entry:

192.168.10.50 app.example.com

A complete configuration may look like this:

127.0.0.1   localhost localhost.localdomain
::1         localhost localhost.localdomain

192.168.10.50 app.example.com

The structure is straightforward:

IP_ADDRESS HOSTNAME FQDN

You can also declare multiple names for the same IP address:

192.168.10.50 app.example.com app

In this case, both app.example.com and app will resolve to the same IP address.

Important: Preserve existing system entries. Avoid duplicate or conflicting mappings, and do not associate the application's hostname with 127.0.0.1 or ::1 when the service is hosted on another address.

Save the file after making the changes.

4. Validating Hostname Resolution

After saving the file, we need to make sure Linux resolves the FQDN correctly.

4.1. Using getent

Run:

getent hosts app.example.com

Expected output:

192.168.10.50 app.example.com

The getent command is particularly useful because it queries the system's configured name resolution mechanisms, respecting the NSS configuration.

4.2. Checking the hosts File Directly

You can also inspect the configured entry:

grep -n "app.example.com" /etc/hosts

4.3. Testing Network Connectivity

Run:

ping -c 4 app.example.com

If the host is reachable and allows ICMP traffic, you should see responses from 192.168.10.50.

However, a lack of ping responses does not necessarily mean that the application is unavailable. Firewalls may block ICMP traffic.

To test the application port, use:

nc -vz app.example.com 8443

Expected output:

Connection to app.example.com 8443 port [tcp/*] succeeded!

5. Validating HTTPS and TLS Certificates

Correctly resolving an FQDN does not mean HTTPS is properly configured.

When using a domain name instead of an IP address, the server must present a TLS certificate that is valid for that hostname.

For example, when accessing:

https://app.example.com:8443

The certificate must contain:

DNS:app.example.com

in the Subject Alternative Name (SAN) field.

5.1. Testing the HTTPS Endpoint

Run:

curl -v https://app.example.com:8443

This command allows you to verify:

  • Hostname resolution.
  • TCP connection establishment.
  • TLS negotiation.
  • Certificate validation.
  • HTTP application response.

For diagnostic purposes only, you can temporarily skip certificate validation:

curl -vk https://app.example.com:8443

Important: The -k option disables certificate chain and hostname validation on the client. It is intended exclusively for troubleshooting and must not be used as a permanent solution.

5.2. Inspecting the Certificate Presented by the Server

Use:

openssl s_client \
  -connect app.example.com:8443 \
  -servername app.example.com \
  -showcerts </dev/null

The -servername option sends the hostname through Server Name Indication (SNI).

This is important when a server hosts multiple HTTPS applications using the same IP address.

To inspect specific certificate information:

openssl s_client \
  -connect app.example.com:8443 \
  -servername app.example.com </dev/null 2>/dev/null \
  | openssl x509 -noout -subject -issuer -dates -ext subjectAltName

5.3. Certificate Types

There are several options:

Certificate issued by a public Certificate Authority

A certificate can be issued for a real domain even when the application resolves internally to a private IP address. One option is to use DNS-01 validation.

Certificate issued by an internal CA

This is common in corporate networks. In this case, the root Certificate Authority must be trusted by the clients accessing the application.

Self-signed certificate

It can be used in controlled laboratories and testing environments, but it must be explicitly trusted by clients to avoid security warnings.

The fundamental requirement is that the certificate's hostname matches the FQDN used to access the application.

6. Configuring a Google OAuth Integration

When an application uses Google authentication, its origin and redirect URLs must be configured using the FQDN.

Access:

Google Cloud Console — OAuth Clients

Create or edit an OAuth client of type Web application.

6.1. Authorized JavaScript Origins

Register the origin:

https://app.example.com:8443

An origin consists of the protocol, hostname, and port, without the endpoint path.

6.2. Authorized Redirect URIs

Register the actual callback endpoint implemented by your application.

Example:

https://app.example.com:8443/auth/google/callback

The path above is illustrative. Use the endpoint implemented by your backend.

The URI must match exactly, including protocol, hostname, port, and path.

6.3. Authorized Domains

In Google Auth Platform, configure the authorized domains associated with a domain controlled by your organization.

Example:

example.com

Domain ownership must be verified according to Google's requirements, typically through Google Search Console.

References:

An important note: The /etc/hosts file does not replace the domain ownership verification required by Google. Additionally, OAuth applications intended for production must comply with Google's publishing policies, including applicable requirements for public homepages, privacy policies, and verification.

7. Understanding the Name Resolution Flow

The architecture we are implementing can be represented as follows:

                 USER / BROWSER
                       |
                       |
             https://app.example.com:8443
                       |
                       |
                NAME RESOLUTION
                       |
                       v
                 /etc/hosts
                       |
                       |
             app.example.com
                       |
                       v
                192.168.10.50
                       |
                       |
                  TCP :8443
                       |
                       v
                HTTPS / TLS
                       |
                       v
               APPLICATION SERVER

Name resolution happens locally, without depending on a public DNS record for this hostname.

The browser uses the original hostname to establish the HTTPS connection. This allows the server to receive the expected hostname, including during TLS negotiation through SNI.

8. /etc/hosts vs. Internal DNS

The /etc/hosts file is excellent for testing and individual configurations, but it is not necessarily the best solution for larger corporate environments.

It is important to understand the differences:

Feature /etc/hosts Internal DNS
Configuration Per machine Centralized
Scope One machine Network clients using that DNS
Administration Manual Centralized
Scalability Limited High
Record changes Per machine On the DNS server
Infrastructure dependency No Yes
Best suited for Testing and labs Corporate environments

When Should You Use /etc/hosts?

I recommend using /etc/hosts when:

  • You are developing an application.
  • You need to test an FQDN quickly.
  • You are configuring a staging environment.
  • You do not have administrative access to corporate DNS.
  • You need to test integrations that depend on specific hostnames.
  • You want to simulate a DNS record without changing the infrastructure.

When Should You Use Internal DNS?

Prefer internal DNS when:

  • Multiple users and servers need access.
  • The application will be used continuously.
  • There are several services and subdomains.
  • Centralized administration is required.
  • Configurations need to be distributed automatically.
  • Multiple environments and DNS zones exist.

An interesting alternative is split-horizon DNS: the same domain returns different answers depending on the source of the query.

For example:

Internal query:
app.example.com → 192.168.10.50

External query:
app.example.com → public IP or no response

This approach allows you to keep services private without publishing their internal IP addresses in public DNS.

9. Common Issues and Troubleshooting

Some issues may occur during configuration.

Problem Possible cause How to investigate
FQDN does not resolve Missing or incorrect entry getent hosts
FQDN resolves to the wrong IP Duplicate entry or DNS conflict grep and getent
Connection refused Service is not listening on the port ss -lntp
Connection timed out Firewall, routing, or unavailable service nc -vz
Certificate error Incorrect SAN or certificate chain openssl s_client
Hostname mismatch Certificate does not match the FQDN Check SAN
HTTP 404 Incorrect virtual host or application route Check application configuration
redirect_uri_mismatch Callback differs from the registered URI Check Google Cloud configuration

Checking Whether the Application Is Listening

On the application server:

sudo ss -lntp

To filter a specific port:

sudo ss -lntp | grep ':8443'

Make sure the service is listening on the correct address and not only on 127.0.0.1.

Checking the Firewall

In environments using firewalld:

sudo firewall-cmd --list-all

If necessary, the application port must be allowed according to the infrastructure's security policy.

Avoid opening ports indiscriminately or exposing internal services to the Internet without an architectural requirement.

10. Security Best Practices

Although configuring /etc/hosts is simple, several precautions should be considered.

1. Use domains controlled by your organization

For OAuth integrations, use a valid, registered, and authorized domain. Avoid improvised names such as .local or .lan.

2. Do not confuse name resolution with network security

Associating an FQDN with a private IP address does not establish an access policy, open ports, or protect the application.

3. Use HTTPS

Even in internal environments, use TLS to protect communication between clients and servers.

4. Protect the hosts file

The /etc/hosts file should have appropriate permissions and should only be modified by administrative users.

5. Avoid unnecessary exposure

Administrative applications, infrastructure dashboards, and internal APIs should remain protected by firewalls, VPNs, access controls, and authentication.

6. Do not use insecure options as permanent solutions

Do not use curl -k as a workaround for certificate problems in production environments.

7. Consider all client devices

Remember that configuring /etc/hosts only affects the machine where the change was made. Every computer accessing the application must resolve the FQDN correctly, preferably through internal DNS.

11. Final Checklist

Before considering the configuration complete, validate the following:

  • [ ] The FQDN belongs to a domain controlled by the organization.
  • [ ] The private IP address is correct.
  • [ ] The entry was added to the /etc/hosts file on the required machine.
  • [ ] There are no conflicting hostname mappings.
  • [ ] getent hosts returns the expected IP address.
  • [ ] The application port is reachable.
  • [ ] The server presents a valid certificate for the FQDN.
  • [ ] The browser trusts the certificate chain.
  • [ ] OAuth origin and callback URLs are configured correctly.
  • [ ] Other clients have appropriate name resolution configured.

Note for Windows users: If other Windows computers also need to access the application, you must configure equivalent hostname resolution on those machines or make the mapping available through internal DNS. The configuration performed on Linux is not automatically propagated to other operating systems.

Conclusion

The /etc/hosts file is a simple but extremely useful tool for managing hostname resolution on Linux.

It allows you to test FQDNs, simulate DNS records, work with applications hosted on private IP addresses, and validate integrations that depend on specific domain names.

For laboratories, development, and staging environments, it is usually a fast and effective solution.

In corporate environments with multiple users and services, internal DNS is generally a more sustainable and manageable alternative.

The most important thing is to understand that name resolution, network connectivity, TLS certificates, and authentication are separate layers. All of them must be properly configured for FQDN-based access to work securely.

References

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.