59

There are a few SSH destinations I will frequently be connecting to and I'm wondering if rather than remember the IP address I can create a host alias for them. I'd like to be able to use...

ssh -p xx user@domain.ssh

I've tried establishing this host in /etc/hosts and unless there's a service I needed to restart it had no effect.

Ben
  • 1,016
  • 1
    This should work fine. Paste the the entry in /etc/hosts file. Can you actually ping the host using the alias (if firewall is not block icmp)? – Terry Wang Feb 17 '13 at 23:02

3 Answers3

87

Sometimes it is more convenient to have configuration files in our home directory. This avoids having to be root to edit files, and also they can be configured in a way that other users have no access to this data. In addition this configuration will be backed up with our home and also will "survive" an OS upgrade.

To do so we can make a ssh configuration file nano ~/.ssh/config where we can put in valuable information for a connection. A simple entry may have the following content:

Host myremote             # any name for the host
HostName 192.168.178.05   # IP, .local, or hostname if defined
User username             # your username
Port 22                   # port to listen

There are many other options including user and authentication you can give here (see manpage for ssh_config)

We then can simply issue the following to connect to 192.168.178.05 on port 22:

ssh myremote
Takkat
  • 142,284
  • 3
    Note that you can also assign more than one host identifier, e.g. Host myremote 192.168.178.05. That way your settings will be used when you connect using the actual host name as well. – Chris Aug 05 '13 at 05:39
  • 1
    Don't forget to add parameter 'User' (if there is need) into your config file! – Ros Nov 13 '14 at 10:04
  • 4
    After ssh command '.ssh/config line 2: garbage at end of line; "#".' means do not include hashtags to your config file. – lioil Apr 15 '17 at 10:54
  • 1
    @Takkat How do we store alias for multiple servers? – Anish Jul 15 '17 at 18:52
  • 2
    @Anish Just add another Host...section separated by an empty line. – Takkat Jul 15 '17 at 18:53
0

The SSH config file is extremely useful but requires manually editing a file. I use this Python package to automatically save the connection information at the time the ssh connection is made. https://github.com/emileindik/slosh

Similar to the sshez tool also posted:

$ pip install slosh
$ slosh ubuntu@1.1.1.1 --save-as myserver

This will perform the desired ssh connection and also create the corresponding entry in your SSH config file.

[disclaimer] I created slosh

0

You can use a ruby gem called sshez. It interfaces your config file and makes adding ssh aliases easier.

Install gem

gem install sshez

Add an alias my_alias for example

sshez add my_alias root@example.com -p 1022

Remove my_alias from your config file

sshez remove my_alias

List aliases

sshez list

Now you can connect to ssh my_alias without worrying about editing your config file yourself.

Oss
  • 117