Detailed tutorial on implementing SSH remote access to the intranet Raspberry Pi from the external network

How to connect to the Raspberry Pi at home through SSH remote access outside the LAN?

This article mainly explains the role of the Raspberry Pi + cpolar combination, which can achieve:

  • How to enable SSH in Raspberry Pi
  • How to connect to a Raspberry Pi device via SSH
  • How to remotely access your Raspberry Pi at home from anywhere

Of all the things you can do with a Raspberry Pi, using it as a server on your home network is very popular. The tiny footprint and low power consumption make it the perfect device for running lightweight servers.

In this case, one thing you should be able to do is run commands on the Raspberry Pi without having to plug in a monitor, keyboard, mouse, or move yourself to the Raspberry Pi every time Pi) location.

You can do this by logging into your Raspberry Pi via SSH (Secure Shell) from any other computer, laptop, desktop or even mobile phone. Let me tell you how.

How to connect to a Raspberry Pi via SSH

img

I'm assuming you're running Raspbian on the Pi and have successfully connected to the network via Ethernet or WiFi. It's important that your Raspberry Pi is connected to the network, otherwise you won't be able to SSH into it (sorry for making this too obvious).

Step 1. Enable SSH on Raspberry Pi

SSH is disabled by default on the Raspberry Pi, so you must enable it when you turn on the Pi after a fresh Raspbian installation.

First go to the Raspberry Pi configuration window via the navigation menu.

img

  • Raspberry Pi menu, Raspberry Pi configuration

Now, go to the Interfaces tab, enable SSH and reboot your Pi.

img

Enable SSH on Raspberry Pi

You can also enable SSH without going through the terminal. Just enter the command sudo raspi-configand go to advanced options to enable SSH.

Step 2. Find the IP address of the Raspberry Pi

In most cases, your Raspberry Pi will be assigned a local IP address, which will look like 192.168.xx or 10.xxx. You can use various Linux commands to find the IP address.

I'm using the old command here ifconfig, but you can use that too ip address.

ifconfig

img

Raspberry Pi network configuration

This command displays a list of all active network adapters and their configurations. The first entry (eth0) shows the IP address as 192.168.9.36, which is valid. I connect my Raspberry Pi to the network using ethernet, so it's under eth0. If you use WiFi check under the entry named "wlan0".

You can also find the IP address by other means, such as checking the list of network devices on your router/modem.

Step 3. SSH to your Raspberry Pi

Now that you have SSH enabled and your IP address found, you can continue to SSH to your Raspberry Pi from any other computer. You will also need your Raspberry Pi username and password.

The default username and password are:

  • Username: pi
  • Password: raspberry

If you changed your default password, please use the new password instead of the one above. Ideally, you must change the default password. In the past, malware has infected thousands of Raspberry Pis using default usernames and passwords.

Open a terminal on the computer you want to SSH to the Pi (on Mac and Linux) and type the following command. On Windows, you can use an SSH client like Putty .

Windows users install SSH client:

If you are using Windows, you need to install an SSH client such as PuTTY on your computer, a free SSH and telnet client for Windowswww.putty.org/ , or Baidu installation.

After the download is complete, open PuTTY, enter the IP address of the Raspberry Pi in the "Host Name" in the session, and then click "open" to connect to the Raspberry Pi.

img

The first time you will see a warning, click Acceptthe button.

img

Prompt to enter login user name: pi

Enter password (default: raspberry)

Now, enter your password and press enter.

img

Login via SSH successful.

Mac and Linux users:

Here, use the IP address you found in the previous step.

ssh [email protected]

NOTE: Make sure your Raspberry Pi is connected to the same network as the computer you are using to SSH into the Raspberry Pi.

img

SSH via terminal

The first time you'll see a warning, type yesand hit enter.

img

Enter password (default is 'raspberry')

Now, enter your password and press enter.

img

Login via SSH successful

After a successful login, you will see the Raspberry Pi's terminal. Now you can execute any command on your Raspberry Pi remotely (within the current network) through this terminal without having to physically access your Raspberry Pi.

Step 4. Access your Raspberry Pi at home from anywhere

There are limitations to accessing the Raspberry Pi only in the same LAN at home, and once the IP of the Raspberry Pi at home changes, what if one day we want to access the Raspberry Pi from the office location? Then we'll solve it now.

4.1 Install Cpolar

cpolar is an intranet penetration tool, which can expose your intranet site to the public network, so that everyone can visit your site. You can also map your ssh port to the port of the public network address in the way of TCP.

  • cpolar one-click installation script: (domestic users)
curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash
  • Or short link installation method: (Foreign users)
curl -sL https://git.io/cpolar | sudo bash
  • View cpolar version information
cpolar version

If it displays normally, the installation is successful, as shown in the figure:

img

4.2 cpolar performs token authentication

If you don’t have a cpolar account yet, please go to the cpolar official website to register and log in to the backend to obtain the authentication token.

cpolar authtoken xxxxxxxxxxxxxxxxxx

4.3 Configure the cpolar service to start automatically at boot

  • Configure cpolar to start automatically at boot
sudo systemctl enable cpolar
  • Daemon mode, start cpolar
sudo systemctl start cpolar
  • View cpolar daemon status
sudo systemctl status cpolar

img

As shown in the figure, the status is active, indicating that the startup status is successful.

cpolar will install two sample tunnels by default, one is the Website tunnel pointing to http port 8080, and the other is the ssh tunnel pointing to tcp port 22.

4.4 View the tunnel address mapped to the public network

We log in to the cpolar background->Status and check the public network address mapped by the ssh tunnel:

img

4.5 SSH public network remote access to Raspberry Pi

ssh [email protected] -p 20013

Since our local port 22 is mapped to port 20013 on the public network, the ssh command needs to add the -p parameter, followed by the public network tunnel port number.

img

Login successful! Now, you can access your Raspberry Pi at home, school, office, or anywhere on your mobile phone’s 4G terminal network!

4.6 Change the default password of Raspberry Pi (optional)

As mentioned above, the default password is unsafe, especially when you expose the Raspberry Pi to a public network address. It is recommended to change the password.

passwd

img

The system prompts you to enter the current password (default is 'raspberry') and then re-enter the new password.

Additionally, you can set up SSH keys so that you don't have to enter a password every time you log in via SSH, but that's a completely different topic.

Reprinted from cpolar pole cloud article: No public network IP, remote access to the Raspberry Pi at home from the public network SSH

Guess you like

Origin blog.csdn.net/weixin_46626339/article/details/132807703