Setting up an SSH server on the Raspberry Pi allows remote login and command execution from another machine. The display of the originating machine is used. This is handy since I do not have exclusive use of the TV display at home. Subsequently, I connect remotely from a Linux or Windows client (through the use of Putty) that is connected to the Raspberry Pi via an Ethernet connection. Furthermore, I can configure the Rapsberry Pi to share the Internet connection of the client machine as detailed here.
This post is structured as follows. Section 1 details how to configure the SSH server on the Raspberry Pi. Section 2 details how to connect to the Raspberry Pi from a Linux client. Section 3 describes how to use RSA public/private keypairs to authenticate user ssh sessions, instead of passwords.
1. Configuring the Raspberry Pi for SSH
To start with we first need to temporarily connect the Raspberry Pi to a suitable display device and check that it has the SSH server installed. Following this we will change the default password for user pi and assign the Raspberry Pi a static IP address.
Type the following in a terminal session:
sudo service ssh status
If an ssh server is started you should receive a response that service sshd is running. If it is not then type the following in a terminal session:
This will start a configuration utility provided with the Raspbian distribution. Select the menu option for ssh and then choose to enable the ssh server. Reboot the Raspberry Pi.
Assign the Raspberry Pi a static IP address for the Ethernet interface by following step 2 of this page.
Finally, change the default password assigned to user pi by typing the following in a terminal session on the Raspberry Pi:
You will be prompted for the current password which should be raspberry When prompted enter the new password that you wish to use.
Note that by default the SSH server on the Raspberry Pi is configured to permit root (administrator) logins and uses the default port number, 22. To increase security this can be altered by changing the config file /etc/ssh/sshd_config on the Raspberry Pi.
2. Testing it out from a Linux client
I tested this from an Ubuntu machine. Instructions for using a windows client are detailed here.
Open a terminal session on the Linux client and type the following:
You will need to substitute 192.168.0.2 with the static IP address assigned to the Raspberry Pi. This can be determined by typing
in a terminal session on the Raspberry Pi and inspecting the IP address detailed in the inet addr heading.
If the ssh request is accepted you should be prompted for a password for user pi. You should then be logged in. Type exit to quit the SSH session.
3. Generating public/private keypairs to authenticate a user
This section details how to generate public/private keypairs to authenticate a user from a Linux client machine. Instructions for using a windows client are detailed here.
In a terminal session on the Linux client enter the following:
mkdir ~/.ssh chmod 700 ~/.ssh ssh-keygen -t rsa
This will generate and store the public/private key pair in directory .ssh on the client. Accept the default key file location when prompted and enter a passphrase for the keys.
Now copy the public key over to the Raspberry Pi by typing the following command in a terminal session:
ssh-copy-id <userid>@<hostname or ip address>
For example, my Raspberry Pi is assigned the static IP address of 22.214.171.124.2. I connect to it by typing the following in a terminal session:
Enter the password for user pi. Following this the public key file will be copied to the following file .ssh/authorized_keys on the Raspberry Pi.
Now try to login by typing the following into a terminal session:
You should be prompted for your passphrase. Enter your passphrase and you should be logged into the Raspberry Pi.