• Sales: 1-800-961-2888
  • Support: 1-800-961-4454

Secure Shell - SSH


Secure Shell (SSH) is a protocol through which you can access your Cloud Server and run shell commands. As the name suggests, SSH is encrypted with SSL, which makes it difficult -- but not impossible -- for these communications to be intercepted and read. What follows is a brief guide on connecting for the first time and locking down the SSH service.

Note: Many of the commands in this article must be run on your local machine. The default commands listed are for the Linux command line or MacOS X Terminal.  To make SSH connections from Windows you can use a free program called PuTTY, and to generate keys you can use a related program, PuTTYGen.  To use SCP on Windows you can use a program like WinSCP.

Contents

Log in

As soon as you have your IP address and password for your Cloud Server, login via SSH:

ssh root@123.45.67.890

If you rebuilt your Cloud Server, you may get a message informing you that the "remote host identification has changed".

When you log into a Cloud Server via SSH, one of the security features is matching the remote host with known keys. When you rebuild a Cloud Server, the remote host key changes. As such, your computer thinks there is something dodgy going on.

If you want to be absolutely sure of your security, you can use the web console in the control panel to verify your server's new key.  If you want to skip that step, then all you need to do is delete the record of the old ssh host key.

On your local computer, edit the SSH known_hosts file and remove any entries that point to your Cloud Server IP address.

nano ~/.ssh/known_hosts

If you are not using Linux or a Mac on your local computer, the location of the known_hosts file will differ. Please refer to your own OS for details of where this file is kept.

SSH keygen

One effective way of securing SSH access to your cloud server is to use a public/private key. This means that a 'public' key is placed on the server and the 'private' key is on our local workstation. This makes it impossible for someone to log in using just a password - they must have the private key.

The first step is to create a folder to hold your keys. On your LOCAL workstation:

mkdir ~/.ssh

That's assuming you use Linux or a Mac and the folder does not exist. I will do a separate article for key generation using Putty for Windows users.

To create the ssh keys, on your local workstation enter:

ssh-keygen -t rsa

If you do not want a passphrase then just press enter when prompted.

That created two files in the .ssh directory: id_rsa and id_rsa.pub. The pub file holds the public key. This is the file that is placed on the Cloud Server.

The other file is your private key. Never show, give away or keep this file on a public computer.

Make a user

If you haven't already, create a new user that you'll use to make ssh connections to your server:

/usr/sbin/adduser demo

The username can be anything you like, just replace "demo" above.  You'll probably also want to add that user to the list of users that can use sudo, by running:

visudo

And then adding a line to the end of the file:

demo   ALL=(ALL) ALL

SSH copy

Now we need to get the public key file onto the Cloud Server.

We'll use the 'scp' (secure copy) command for this as it is an easy and secure means of transferring files.

Still on your local workstation enter this command:

scp ~/.ssh/id_rsa.pub demo@123.45.67.890:/home/demo/

When prompted, enter the demo user password.

Change the IP address to your cloud server and the location to your admin user's home directory (remember the admin user in this example is called demo).

SSH Permissions

OK, so now we've created the public/private keys and we've copied the public key onto the Cloud Server.

Now we need to sort out a few permissions for the ssh key.

On your Cloud Server, create a directory called .ssh in the 'demo' user's home folder and move the pub key into it.

mkdir /home/demo/.ssh
mv /home/demo/id_rsa.pub /home/demo/.ssh/authorized_keys

Now we can set the correct permissions on the key:

chown -R demo:demo /home/demo/.ssh
chmod 700 /home/demo/.ssh
chmod 600 /home/demo/.ssh/authorized_keys

Again, change the 'demo' user and group to your admin user and group.

It may seem a long set of steps but once you have done it once you can see the order of things: create the key on your local workstation, copy the public key to the Cloud Server and set the correct permissions for the key.

SSH config

Next we'll change the default SSH configuration to make it more secure:

nano /etc/ssh/sshd_config

The main things to change, check, and add are:

Port 30000                           <--- change to a port of your choosing
Protocol 2
PermitRootLogin no
PasswordAuthentication no
UseDNS no
AllowUsers demo

The settings should be fairly self explanatory but the main thing is to move it from the default port of 22 to one of your choosing, turn off root logins and define which users can log in.

NOTE: the port number can readily be any integer between 1025 and 65536 (inclusive), but should be noted for reference later when any additional listening processes are setup, as it will be important to avoid conflicts.

PasswordAuthentication has been turned off as we setup the public/private key earlier. Do note that if you intend to access your cloud server from different computers you may want leave PasswordAuthentication set to yes. Only use the private key if the local computer is secure (i.e. don't put the private key on a work computer).

Changing iptables

If you have a firewall active and you changed the port of the ssh server, you'll need to make sure your firewall won't block that new port.

If you already have a rule for ssh, it's probably on port 22.  The iptables instruction for that port looks like:

-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT

Change the "22" to the port you'll be using, for example, to "30000".  For more details you can read our introduction to iptables, but the basics of setting the firewall rule for some of the more popular distributions are:

Ubuntu and Debian

On more recent versions of Ubuntu and Debian you can run:

iptables -A INPUT -p tcp -m tcp --dport 30000 -j ACCEPT
iptables-save

CentOS, Fedora, and RHEL

For Red Hat-based distributions like CentOS, Fedora, and RHEL, run:

iptables -A INPUT -p tcp -m tcp --dport 30000 -j ACCEPT
service iptables save

Restarting sshd

Once you've made all your changes be sure to restart sshd so the new settings will take effect.  To be safe, don't log out of your original ssh session until you've tested the new settings with another connection.

Ubuntu and Debian

To restart the ssh server on Ubuntu or Debian, run:

service ssh restart

CentOS, Fedora, and RHEL

To restart the ssh server on CentOS, Fedora, or RHEL, run:

service sshd restart

Connecting after the changes

If you're using an ssh client like PuTTY, be sure and edit the connection profile to switch the port from "22" to whatever you decided to use instead.

If you're connecting from the command line you can specify the port ssh will use with the "-p" option:

ssh -p 30000 demo@123.45.67.89

If you made the changes suggested earlier, then you won't be able to ssh in as root - just as the user you specified in the config, like "demo".

Troubleshooting

If you can't make a new connection after restarting the ssh server then you may want to go back over the steps listed here, check your changes, and then restart the ssh server when you're done and try again.

If you get a "connection timeout" error, then either you need to add your ssh port to iptables, or you might be specifying the wrong IP address when you're connecting.

If you get a "connection refused" error, then you are probably trying to ssh to the wrong port.  If you changed your ssh server to listen to a port other than 22, use the "-p" option with ssh to specify the port.

If you're getting a rejected login then it could be an issue with your key.  Try changing the sshd config to allow password connections (set "PasswordAuthentication" to "yes"), restart the ssh server, then try again.  If you can get in this time, then the issue is with the key - make sure it's in the right place on the server.  



© 2011-2013 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License


See license specifics and DISCLAIMER

24 Comments

Once you change the config file for ssh, you will need to restart the server or sshd in order for the changes to take effect.

Good call, that step definitely shouldn't be omitted. I'll add that to the article. Thanks!

I followed this article to the last step. Having done that I am unable to login. The connection times out. The only thing I did different was to use my existing public key because I have three other hosts where I login and obviously didn't want to regenerate and overwrite my existing keys. You should also include a step to create the demo user.

Usually a connection timeout either indicates that the firewall is blocking the port used by ssh, or that the ssh client isn't pointing to the right port. Are you able to telnet to the server on the ssh port and get a response from sshd?

Solved it. I forgot to configure the iptables (You got it right the ssh port was being blocked). I am now able to login. Its likely that most people would change the default ssh port from 22 to something else, I guess you'll need to put a link to the next step (configure iptables) in this article. The articles you wrote on Slicehost maintain a logical flow but the sarticles here is organised by topic rather than in any particular sequence.

Glad to hear you got it working. Thanks for pointing out what would help this article, it's always great to get that kind of feedback. We're working on some more comprehensive articles on initial setup, but for now I'll work your advice into this one.

I have rebuilt the instance. Let me check it this time will post my findings. The steps to create the "demo" user are available in the "Centos Setup" article pls include a link to that article here.

Restarting ssh in Ubuntu cannot be accomplished by /etc/init.d/ssh reload or restart or a combination of start and stop. Because the startup services are now being handled through upstart. I am not sure how should I start ssh, googling the problem hasn't thrown up any valuable posts as yet.

root@Studious:/home/stud# /etc/init.d/ssh reload
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service ssh reload

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the reload(8) utility, e.g. reload ssh
reload: Unknown instance:
root@Studious:/home/stud# service ssh reload
reload: Unknown instance:

Thanks Sridhar. The "service" command should indeed help with restarting on most distributions, including Ubuntu. "sudo service ssh reload" or "sudo ssh service restart" ought to do the trick on upstart builds or pre-upstart builds of Ubuntu or on Debian. On CentOS, RHEL, or Fedora, the service will likely be named "sshd" instead - thus, "sudo service sshd reload" or "sudo service sshd restart".

Having changed the default ssh configuration shouldn't the iptables be reconfigured to allow connections on the new port or rather the iptables should be configured after the ssh port is chosen.

But in the articles hierarchy the "Read These If You're New To Linux | Ubuntu Setup" describes iptables configuration and the "Secure Your Server | Access Control | Secure Shell" article describes changing the ssh port to something else instead of the default 22. So people would end up getting session timed out errors on ssh -p <their ssh port> <user name>@999.999.999.999 while connections will be refused on port 22.

You're absolutely correct, Sridhar. I'll get this article updated today to mention or link to information about adjusting iptables. The oversight has clearly caused problems for more than one person.

Tried several times but configuring iptables this way doesn't open the new ports on Ubuntu 11.04 (Natty). It however does close port 22 effectively locking me out.

I flushed the iptables
/sbin/iptables -F
then restored the rules from the iptables.rules
/sbin/iptables-restore < /etc/iptables.rules

Now I get a "Permission denied (publickey)". Not sure what could be wrong.

I had the sshd_config & iptables setup & the correct permissions to the ~/.ssh/authorized_keys but still I couldn't log in by public key, just by password!!

And the /var/log/auth.log wasnn't saying anything suspicious with DEBUG3 LogLevel. So the problem was on my LOCAL computer, on my openssh-client!
Solution:
you have to add the private keys created with "ssh-keygen -t rsa" to the ssh-agent which holds the private keys used for public key authentication (RSA, DSA), using the cmd:
ssh-add
Use 'man ssh-add' to see why the problem occurred.

For now I have set "PasswordAuthentication yes" and I am able to login.

I'm glad you got back in, Sridhar - sorry about all the trouble you're running into. There was some sort of problem with your keypair, it's possible a file was in the wrong place. Sometimes that can also be a permissions issue, thinking about it - the keys, or at least the .ssh directory, have to be set so only the owner can read them. I'll also put Ubuntu's iptables through its paces to see if I can improve those instructions more. Thanks for the continued feedback, it definitely helps identify areas we need to work on.

I'm trying to change just the port and nothing else, but I'm getting a timeout. These are the commands I've run:

nano /etc/ssh/sshd_config
- added 'Port 30022'
iptables -A INPUT -p tcp -m tcp --dport 30022 -j ACCEPT
service iptables save
service sshd restart

I've also added an outbound rule to my firewall, at the moment this is set to any protocol and any port (http://windows.microsoft.com/en-US/windows7/Open-a-port-in-Windows-Firewall).

I've even tried turning my firewall off, as well as my additional security program (Trend), but I still get a timeout when I try to connect with WinSCP, are there any logs or diagnostics I can check?

What you've done looks right, but obviously something's still being difficult. I'd recommend first trying to narrow down the source of the problem.

Are you able to connect to ssh with no changes made? Just making sure that sshd is working for you on port 22.

If 22 works, then after changing the ssh port, disable iptables ("service iptables stop") and see if you can connect to ssh using the new port. That can at least help narrow in on whether the problem stems from iptables or from the sshd config.

If it isn't iptables, then see if you can still connect to ssh using port 22 after making the change. That's to make sure the change is actually taking effect.

Both iptables and sshd typically log to "/var/log/secure", so you can check for any interesting errors there. You can also check "/var/log/messages", since that's the catch-all system log.

Let us know (or contact support and let them know) what does and doesn't work from that list, and we can continue troubleshooting from there.

Fedora 16 some commands changed, this is one
iptables-save

When I created the user , when do I enter the password? I'm at the "SSH Copy" step, and it's asking for the new user's password. I was never prompted to create it so now I have no idea what it is?

In the article you said you will write a version for Windows users. Is this article available anywhere?

Sorry Frank, I'll try to clean the article up to make that link prominent. You can get info on using PuTTY to connect via SSH here:

http://www.rackspace.com/knowledge_center/article/rackspace-cloud-essentials-3-going-from-windows-to-linux-using-putty

Thanks, but that article doesn't touch on creating a Public/Private RSA key for SSH login from my Windows PC. I've gone through the steps in this article many times, but I always end up with the server refusing my key...

Ah, sorry about that. PuTTY does handle keys a bit differently. Try this article on using PuTTYGen:

http://www.rackspace.com/knowledge_center/article/ssh-puttygen

Add new comment