Friday, 29 March 2013

ssh Connection refused on UBUNTU

I tried to ssh to localhost on my UBUNTU PC but the connection was refused:

UBUNTU > ssh localhost
ssh: connect to host localhost port 22: Connection refused
UBUNTU >

This was because ssh had not been set up on my machine. I set it up like this:

UBUNTU > sudo apt-get install openssh-server openssh-client
Reading package lists... Done
Building dependency tree    
Reading state information... Done
openssh-client is already the newest version.
Suggested packages:
  rssh molly-guard openssh-blacklist openssh-blacklist-extra monkeysphere
The following NEW packages will be installed
  openssh-server ssh-import-id
0 upgraded, 2 newly installed, 0 to remove and 576 not upgraded.
Need to get 348 kB of archives.
After this operation, 891 kB of additional disk space will be used.
Get:1 http://gb.archive.ubuntu.com/ubuntu/ precise/main openssh-server i386 1:5.9p1-5ubuntu1 [342 kB]
Get:2 http://gb.archive.ubuntu.com/ubuntu/ precise/main ssh-import-id all 2.10-0ubuntu1 [6.598 B]
Fetched 348 kB in 1s (288 kB/s)  
Preconfiguring packages ...
Selecting previously unselected package openssh-server.
(Reading database ... 153823 files and directories currently installed.)
Unpacking openssh-server (from .../openssh-server_1%3a5.9p1-5ubuntu1_i386.deb) ...
Selecting previously unselected package ssh-import-id.
Unpacking ssh-import-id (from .../ssh-import-id_2.10-0ubuntu1_all.deb) ...
Processing triggers for ureadahead ...
ureadahead will be reprofiled on next reboot
me time ...
CreaProcessing triggers for ufw ...
Processing triggers for man-db ...
Setting up openssh-server (1:5.9p1-5ubuntu1) ...
Creating SSH2 RSA key; this may take some time ...
Creating SSH2 DSA key; this may take soting SSH2 ECDSA key; this may take some time ...
ssh start/running, process 3144
Setting up ssh-import-id (2.10-0ubuntu1) ...
UBUNTU >


Then I was able to connect as follows:

UBUNTU > ssh localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is 56:30:a1:d5:23:9b:3f:a4:3b:30:6c:2c:07:c5:8e:3e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
andrew@localhost's password:
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)

 * Documentation:  https://help.ubuntu.com/

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

No mail.
UBUNTU >

/etc/services

The /etc/services file specifies which port a program must use when it connects to a UNIX or Linux machine. I tested this example using UBUNTU on my stand-alone PC so the connections are made to localhost. According to /etc/services, you must connect via port 21 if you wish to use ftp: 

UBUNTU > cat /etc/services|grep ftp
ftp-data        20/tcp
ftp             21/tcp
tftp            69/udp
sftp            115/tcp
ftps-data       989/tcp             # FTP over SSL (data)
ftps            990/tcp
venus-se        2431/udp            # udp sftp side effect
codasrv-se      2433/udp            # udp sftp side effect
gsiftp          2811/tcp
gsiftp          2811/udp
frox            2121/tcp            # frox: caching ftp proxy
zope-ftp        8021/tcp            # zope management by ftp
UBUNTU > ftp localhost 21
Connected to localhost.
220 (vsFTPd 2.3.5)
Name (localhost:andrew):
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
257 "/home/andrew"
ftp>

… but if you try using a different port, you are rejected:
 
UBUNTU > ftp localhost 42
ftp: connect: Connection refused
ftp>