Connecting to the Raspberry Pi Server Via SSH
SSH, which stands for Secure Shell, is a network protocol that enables secure system administration over a network. Since the Raspberry Pi is connected to a wireless network, we can use SSH to control it from another device connected to the same network.
Notes
To be clear, this article assumes that both the Raspberry Pi and the computer used to log into it are on the same local network! I might write an article in the future about connecting over the internet from a remote network, but that is beyond the scope of this particular article.
It is also worth mentioning that the instructions in this article are design to allow a computer on which Debian GNU/Linux is install to log into the Pi remotely, but they have not been tested with other operating systems. However, they should work with most other GNU/Linux distributions, and they may even be somewhat applicable to BSD, Mac OS, and Windows.
Determining the Network ID
Before SSH can be used to log into the Pi, the Pi's local IP address must be determined. Before we can do that, we must first determine the ID of the network to which the Pi is connected. Assuming that the computer that we will use to log into the Pi is also connected to the same network, we can find the network ID with the ip
, hostname
, or ifconfig
command. This article will only show how to do so with the ip
command, but you may use whichever command you prefer.
The network ID is contained within the local IP addresses of all devices connected to the network, so it can be determined by finding the IP address of any of those devices. We will find the IP address of the computer which we are going to use to log into the Raspberry Pi. We will do that via the command ip a
. Some example output from this command is shown below, and I have made the IP address a darker shade of green so that it is easier to see. To be clear, the command is ip a
not ip -a
! I found that to be confusing, sort of like differentiating between the words "Pi" and "IP" when typing this article.
user@debian:~$
ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp2s0: mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
3: wlp3s0: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:00:00:00:00:a1 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.4/24 brd 192.168.1.255 scope global dynamic noprefixroute wlp3s0
valid_lft 000000sec preferred_lft 000000sec
inet6 0000::0000:0000:0000:0000/00 scope link noprefixroute
valid_lft forever preferred_lft forever
Once you have determined the IP address of the computer which we will use to connect to the Pi (192.168.1.4/24 in this case), we can move onto the next step. However, since we only really need the network ID, the host ID of the computer itself really does not matter. Therefore, 192.168.1.0/24, 192.168.1.5/24, and 192.168.1.27/24 would all work.
Determining the Raspberry Pi's IP Address
Now, we must scan the network in order to determine the local IP address of the Raspberry Pi. Sadly, in order to do this, we must install a network scanning program. In this article, I will use Nmap, which is a powerful, free, cross-platform network scanning tool. I think that it is unfortunate that I must install a whole 26.3 MB program just to find the IP address of one device. I could uninstall it, but then I will probably need it again.
Assuming that you have now installed Nmap (via sudo apt-get install nmap
on Debian and its derivatives), it can now be used to find the IP address of the Pi. In my case, the command was nmap -sn 192.168.1.4/24
because 192.168.1.4/24 was the IP address found in the prior step. You will need to use the IP address that you found for your network.
user@debian:~$
nmap -sn 192.168.1.4/24
Starting Nmap 7.70 ( https://nmap.org ) at 2020-11-15 19:43 UTC
Nmap scan report for router.home (192.168.1.1)
Host is up (0.0023s latency).
Nmap scan report for computer.home (192.168.1.2)
Host is up (0.00027s latency).
Nmap scan report for stoneTablet.home (192.168.1.3)
Host is up (7.9523h latency).
Nmap scan report for debian.home (192.168.1.4)
Host is up (0.00023s latency).
Nmap scan report for raspberrypi.home (192.168.1.5)
Host is up (0.055s latency).
Nmap done: 256 IP addresses (5 hosts up) scanned in 2.65 seconds
As seen above this paragraph, Nmap returns the names and IP addresses of all of the devices on the network. Included among these is the Raspberry Pi, which I have made a darker shade of green so that you can see it more easily. Nmap shows that its IP address is 192.168.1.5.
Logging Into the Pi Via SSH
Now that we know the Raspberry Pi's IP address—192.168.1.5 in this case—we are ready to access it via SSH. Assuming that SSH is already installed (which its on most systems), this can be done by issuing the ssh command followed by the name of the account that you would like to log in with at the IP address of the Raspberry Pi. As example is shown below this paragraph.
user@debian:~$
ssh pi@192.168.1.4
pi@192.168.1.4's password:
Once the command is issued, the user will be prompted for a password. The default account on the Raspberry Pi is "pi", and its password is "raspberry". Once the password is submitted, the user will be logged into the Pi and presented with a shell prompt as seen below.
pi@raspberrypi
:
~$
It should be noted that the IP address of the Raspberry Pi may change if lots of additional devices are added to the network. However, it should not change very often.
We can now control the Raspberry Pi remotely via SSH. In the next article, we will use SSH to add a user to the Pi and configure various other items.
Previous | Published December 2, 2020 | Next |
---|