How to Setup and Use SSH on a Raspberry PI

Businesswoman in hotel suite working on laptop.

Thomas Barwick/Iconica / Getty Images

SSH is a secure method of logging onto a remote computer. If your Pi is networked, then this can be a handy way of operating it from another computer or just copying files to or from it.

First, you have to install the SSH service. This is done by this command:

sudo apt-get install ssh

After a couple of minutes, this will be complete. You can start the daemon (Unix name for a service) with this command from the terminal:

sudo /etc/init.d/ssh start

This init.d is used to start other daemons. For example, if you have Apache, MySQL, Samba etc. You can also stop the service with stop or restart it with restart.

Have It Start at Bootup

To set it up so the ssh server starts every time the Pi boots up, run this command once:

sudo update-rc.d ssh defaults

You can check that it worked by forcing your Pi to reboot with the reboot command:

sudo reboot

Then after rebooting try to connect to it using Putty or WinSCP (details below).

Powering Down and Rebooting

It's possible to corrupt your SD card with power offs before it halts. The result: reinstall everything. Only power down once you have fully shut down your Pi. Given its low power usage and little heat given off, you could probably leave it running 24x7.

If you want to shut it down, use the shutdown command:

sudo shutdown -h now

Change -h to -r and it does the same as sudo reboot.

Putty and WinSCP

If you're accessing your Pi from the command line of a Windows/Linux or Mac PC then use Putty or the commercial (but free for private use) Tunnelier. Both are great for general browsing around your Pi's folders and copying files to or from a Windows PC. Download them from these URLs:

Your Pi needs to be connected to your network before you use Putty or WinSCP and you need to know its IP address. On my network, my Pi is on 192.168.1.69. You can find yours by typing in

/sbin/ifconfig

and on the 2nd line of the output, you'll see inet addr: followed by your IP address.

For Putty, it's easiest to download putty.exe or the zip file of all the exes and put them in a folder. When you run putty it pops up a configuration Window. Enter your IP address in the input field where it says Host Name (or IP address) and enter pi or any name there.

Now click the save button then the open button at the bottom. You'll have to login into your pi but now you can use it as if you were actually there.

This can be quite useful, as it's far easier to cut and paste long text strings in via a putty terminal.

Try running this command:

ps ax

That shows a list of processes running on your pi. These include ssh (the two sshd) and Samba (nmbd and smbd) and many others.

PID TTY STAT TIME COMMAND
858 ? Ss 0:00 /usr/sbin/sshd
866 ? Ss 0:00 /usr/sbin/nmbd -D
887 ? Ss 0:00 /usr/sbin/smbd -D
1092 ? Ss 0:00 sshd: pi [priv]

WinSCP

We find it most useful to set it up in two screen mode rather than in explorer mode but it's easily changed in the Preferences. Also in preferences under Integration/Applications change the path to the putty.exe so you can easily jump into putty.

When you connect to the pi, it starts at your home directory which is /home/pi. Click on the two .. to view the folder above and do it once more to get to the root. You can see all of the 20 Linux folders.

After you've used a terminal for a while you'll see a hidden file .bash_history (not that well hidden!). This is a text file of your command history with all the commands you've used before so copy it, edit out the stuff you don't want and keep the useful commands somewhere safe.

Format
mla apa chicago
Your Citation
Bolton, David. "How to Setup and Use SSH on a Raspberry PI." ThoughtCo, Apr. 5, 2023, thoughtco.com/setup-use-ssh-with-raspberry-pi-958618. Bolton, David. (2023, April 5). How to Setup and Use SSH on a Raspberry PI. Retrieved from https://www.thoughtco.com/setup-use-ssh-with-raspberry-pi-958618 Bolton, David. "How to Setup and Use SSH on a Raspberry PI." ThoughtCo. https://www.thoughtco.com/setup-use-ssh-with-raspberry-pi-958618 (accessed March 28, 2024).