How to Associate Hostnames with IP Addresses for SSH Connections Without a Domain


2 views

Every developer knows the frustration of typing ssh user@192.168.1.45 repeatedly when working with remote servers. While DNS is the standard solution, many internal servers (like database instances) don't have domain names assigned.

The simplest method is editing your local /etc/hosts file (Linux/Mac) or C:\Windows\System32\drivers\etc\hosts (Windows). Add a line like:

192.168.1.45   database

Now you can simply use ssh user@database. This works system-wide but requires admin privileges.

For per-user configuration without system modifications, edit ~/.ssh/config:

Host database
    HostName 192.168.1.45
    User username
    Port 22
    IdentityFile ~/.ssh/id_rsa

This allows even simpler commands: ssh database with all connection details preconfigured.

For complex setups with staging/production servers, use wildcards:

Host *.staging
    User dev_user
    Port 2222

Host db-prod
    HostName 10.0.0.1
    User prod_user

Add this to your SSH config to maintain persistent connections:

ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h-%p
ControlPersist 600
  • Verify permissions: chmod 600 ~/.ssh/config
  • Check hostname resolution: ping database
  • For Windows, ensure the hosts file isn't blocked by security software

When managing multiple servers, remembering IP addresses becomes cumbersome quickly. For database servers or internal infrastructure that doesn't have DNS records, constantly typing commands like:

ssh admin@192.168.1.45

is both error-prone and inefficient. What we really want is the ability to use memorable hostnames like:

ssh admin@database01

The simplest approach is modifying your local /etc/hosts file (on Linux/macOS) or C:\Windows\System32\drivers\etc\hosts (Windows). Add entries like:

192.168.1.45   database01 db01
192.168.1.46   redis-cache
192.168.1.47   backup-server

After saving, these hostnames become immediately available for SSH and other network operations.

For more sophisticated SSH-specific configuration, edit ~/.ssh/config:

Host db
    HostName 192.168.1.45
    User admin
    Port 2222
    IdentityFile ~/.ssh/db_key

Host redis
    HostName 192.168.1.46
    User redis-user

This allows ultra-short commands:

ssh db
ssh redis

For organization-wide solutions, set up DNSmasq on a local server:

address=/database01/192.168.1.45
address=/redis/192.168.1.46

Point your DHCP server to provide this DNSmasq server as the primary DNS resolver.

For cloud servers with changing IPs, consider a dynamic DNS client like ddclient:

protocol=dyndns2
server=dynupdate.no-ip.com
login=your_login
password='your_password'
database01.no-ip.org

Combine with SSH config for seamless access to changing IPs.

For infrastructure-as-code approach, use Ansible to manage host entries:

- name: Ensure host entries
  ansible.builtin.lineinfile:
    path: /etc/hosts
    line: "{{ item.ip }} {{ item.name }}"
  loop:
    - { ip: '192.168.1.45', name: 'database01' }
    - { ip: '192.168.1.46', name: 'redis' }