55

Let's say I just have an ip address for a server and I don't have a domain with it (it's just a database server, so it doesn't need a domain). I don't want to have to remember the ip address every time, so is there a way I could still use the syntax like ssh username@database or something?

Matthew
  • 1,769
  • 4
  • 21
  • 32
  • This is mistake primarily in terminology. Replace the word "domain" with a correct word "name" and you will immediately see it. "I say the server doesn't need a name, so I would really like to refer to it by name" is a self-contradiction. Maybe your real questions should be "What is a domain? How to add a new name to a domain?" – kubanczyk Mar 12 '18 at 22:48

5 Answers5

83

If you only want the name for ssh and ssh only, you can add a name to your ssh config in ~/.ssh/config

As an example, your config file could look like this:

Host database
    HostName <real IP address or hostname here>
    User username

Then you can type ssh database on the command line and ssh will automatically do ssh username@ip.address for you.

David
  • 3,337
  • 25
  • 20
  • If in the HostName field I can put only the hostname or the IP, then how do I specify the hostname-IP association? – Ramy Al Zuhouri May 15 '15 at 10:16
  • 2
    @RamyAlZuhouri you would put the hostname in the Host field, like "database" in the example, and (just) the IP address in the HostName field. – David May 17 '15 at 03:03
13

Add an entry for it to /etc/hosts on the system you're ssh'ing from.

The syntax is

1.1.1.1 hostname

This works on Linux and Mac. For Windows, the file is c:\windows\system\drivers\etc\hosts.

Benjamin W.
  • 187
  • 2
  • 9
PaulR
  • 251
  • 1
  • 2
  • 2
    This will definitely work, but be advised that the hosts file is sometimes overwritten by network managers. Also you have to have root privileges so this is only an option if you have root. – Freedom_Ben May 20 '15 at 23:52
  • But is this method secure and safe to practice? – Shayan Sep 07 '22 at 17:51
2

clients have 2 or 3 ways to associate a name with a IP address.

1) DNS, but that implies a hostname and a domain.

2) host file, you can add any name in the clients host file and then it will be used. Add the line '192.168.1.1 database' in /etc/hosts to associate the name database with the address 192.168.1.1. See http://en.wikipedia.org/wiki/Hosts_%28file%29 for more specific details and OS specific locations.

3) NIS, Solaris computers can use NIS to share hostnames for multiple clients.

Mike
  • 33
  • 5
0

You need just to add the database name-IP mapping to your /etc/hosts file. The hosts file can be easily edited. You will find some entries there.

This name can be used for any connection not just SSH.

Khaled
  • 35,688
  • 8
  • 69
  • 98
0

Create a DynDNS, it's free, in five minutes you can add a A record that points to your IP.

For example: create database1.dyndns.org as an A record pointing your ip
You can access from everywhere using:

ssh username@database1.dyndns.org

Matias Dominoni
  • 337
  • 1
  • 4