Easier SSH connections from Ubuntu Linux

ssh.png

If you’re a Linux user and you often log in to remote machines or servers, then this tutorial may save you some time. It’s a tip that user Oshunluvr from kubuntuforums.net showed me a few months ago. I’ve been using it ever since.

It will allow you to log into machines with SSH servers on custom ports, with a specified username, by typing:

ssh domain.com

Instead of:

ssh user@domain.com -p 1234

As I discussed in a previous post, raspberry pi server preparation, there are many bots out there that search for ssh servers and try to brute force their way in. In order to attract less unwanted attention, you may choose to forward a non-standard port on your router to port 22 on your server.

This has the annoying side effect of requiring you to specify the port when you connect with the -p option.

To make things easier, you can edit (or create) a ssh config file:

sudo nano ~/.ssh/config

For each machine you would like to connect to, add the following:

Host yourdomain.com
User username
Port 1234

Now you can connect by typing ssh yourdomain.com, without specifying a username and port each time.

Enjoy!

Category: 
Type: 

Comments

Hi Sam. I've setup my raspberry pi to work as a mail and web server for my own personal use. I used your great tutorial for setting up the mail server and installed nginx as my web server. I have just noticed your advise regards using a non-standard port for ssh, forwarded by the router to port 22 on my server. However, after setting up my port forward settings on my dd-wrt router and using the -p switch to ssh into my server, connection is refused. Connecting as normal is not a problem. Can you point out where I'm going wrong? Basically I've added a port forwarding rule to my router that has the "port from" option set to "1234", the "ip address" set to that of my server's ip address and the "port to" option set to "22". I then use the command "ssh foo@foo.foo -p 1234". I then get refused connection. Using the command "ssh foo@foo.foo" connects without issue. Any help most appreciated.

This may have something to do with the IP address/domain name you're requesting to connect to your server, and/or the way your router resolves domain names.

For example, to SSH to my server from inside my LAN I can use ssh user@samhobbs.co.uk -p 1234 (WAN domain name) or ssh user@195.166.151.235 -p 1234 (WAN IP) or ssh user@192.168.1.103 (LAN IP address) or ssh user@samhobbs (samhobbs is the hostname of the server, which my router resolves to the LAN IP).

If my router knew that samhobbs.co.uk was the host name for my server, then connecting with ssh user@samhobbs.co.uk -p 1234 would be a LAN-to-LAN connection and I'd have the wrong port number (22 for LAN, 1234 for WAN). If this was the case, I would have to use port 22 when connecting from inside the LAN and port 1234 when outside the LAN.

Hope that makes sense!

If you can change things so everything is consistent (same port number regardless of whether you're on your WAN or LAN), then you might find this tip useful.

Thanks for your comment!

Sam

Add new comment