您已经冒险购买了Raspberry Pi。恭喜!如果您的 Pi 没有连接键盘和显示器,则需要从网络上的另一台计算机远程连接到它。此外,根据您打算如何使用您的 Pi,它可能被放置在难以触及的地方,因此即使您现在连接了键盘和显示器,也可能并非总是如此。您需要一些方法来远程连接到您的 Pi。
在本文中,我们将向您展示如何在您的 Pi 中使用SSH和SFTP、执行命令和传输文件。我们假设您的Raspberry Pi与您用来连接它的计算机位于同一网络上。
什么是 SSH?
SSH代表安全外壳(Secure Shell)。它是一种标准网络协议,可为您提供一种在 Pi 和用于远程连接 Pi 的计算机之间来回发送文本的安全方式。您将使用SSH协议进行各种操作,例如输入密码以登录 Pi 和执行命令。
SSH被设计为Telnet的替代品,后者通过网络以纯文本形式发送命令。相反,SSH对这些命令进行加密。任何(Any)网络服务都可以使用SSH,它通过TCP端口 22 运行。
什么是 SFTP?
SFTP是安全文件传输协议(Secure File Transfer Protocol)的缩写。SFTP是一种在 Pi 和网络上的另一台计算机之间传输文件的快速便捷方式。SFTP比FTP更安全,因为就像SSH一样,您的所有数据在从一台计算机传输到另一台计算机时都会被加密。
您不希望所有数据都以纯文本形式传输,对吗?当然不是!保持(Stay)安全,孩子们。
如何通过 SSH 连接到树莓派
我们将从如何使用SSH从您网络上的Windows PC 使用PuTTY连接到您的Raspberry Pi开始。PuTTY是几个免费提供的SSH客户端之一。我们喜欢PuTTY,因为它是完全由志愿者团队开发和支持的开源软件。你可以使用任何你想要的SSH客户端。(SSH)请务必在开始之前下载并安装它。
- 确保(Make)在您的 Pi 上启用了SSH 。如果您从头开始使用Raspberry Pi 4,本入门指南(this getting started guide)包含有关如何通过在Raspberry Pi内的 SD 卡上创建文本文件来启用SSH的说明。
- 假设您确定您的 Pi 上启用了SSH,接下来您需要找到您的 Pi 的 IP 地址,以便使用PuTTY等(PuTTY)SSH客户端连接到它。如果您还不知道,可以按照本指南(follow this guide)详细说明确定 Pi 的 IP 地址的几种方法。
- 在您的 Windows PC 上启动PuTTY(或其他SSH客户端)并输入您的 Pi 的 IP 地址。(SSH)SSH端口是 22。大多数SSH客户端已经填写了端口号。
- 选择打开(Open)以连接到您的 Pi。
- 这将启动一个终端窗口,您将被要求登录。输入您的用户名,然后按Enter。(大多数Pis的默认用户名是pi。)
- 接下来,输入您的密码或将其复制到 PC 上的剪贴板,然后在终端窗口中单击鼠标右键将其粘贴。请注意,您的密码不会(not )在您键入时显示。按Enter。
您现在已通过SSH连接到您的 Pi,并且可以使用终端执行命令(execute commands)。如果您还没有更改 Pi 的默认密码,那么现在是这样做的好时机。由于启用了SSH,如果您的树莓派(Raspberry Pi)使用的是默认密码,那么很(very )容易有人闯入!
如何通过 SFTP 进入树莓派
上面(Above),我们向您介绍了如何使用SSH客户端安全地连接到网络上的 Pi 并执行命令。对于传输文件,我们将使用SFTP客户端。从技术上讲,可以使用PuTTY传输文件,但是很麻烦。拥有一个文件浏览器会更好,您可以在需要时使用它来批量移动文件。
FileZilla是一个流行的支持SFTP的(SFTP)FTP客户端。如果您可以使用SSH连接,那么您也可以使用SFTP连接。方便(Convenient),不是吗?
通过在您用于远程连接到 Pi 的计算机上下载并安装FileZilla或您选择的 SFTP 客户端进行准备。(FileZilla)WinSCP是另一个流行的SFTP客户端。请记住(Remember),您不需要SFTP服务器,只需要一个SFTP客户端。
专业提示:如果您安装FileZilla,请注意它何时要求您安装McAfee和WinZip。如果您不想在计算机上安装这些程序,请选择拒绝。(Decline)
- 启动FileZilla或其他SFTP客户端。
- 在标记为Host的字段中,输入您的 Pi 的 IP 地址。
- 输入您的用户名(Username )和密码(Password)。
- 选择快速连接(Quickconnect)。您的SFTP客户端可能会显示保存密码的选项,但我们不建议这样做,因为某些客户端将该密码保存在不安全的文本文件中。
- 连接后,您可以浏览 Pi 上的文件夹结构,并在 Pi 和网络上的其他计算机之间来回复制文件。FileZilla窗口的左侧显示本地文件,右侧显示您连接到的计算机上的文件 - 在本例中是您的 Pi。
例如,如果您想将文件从本地计算机复制到 Pi,您将导航到左侧的源文件夹和右侧的目标文件夹。FileZilla 的文件管理器允许您在本地和远程站点之间拖放文件。
寻找 Pi 项目
一旦您熟悉了执行命令和在 Pi 之间传输文件和从 Pi 传输文件,您将开始意识到您可以通过多少不同的方式让 Pi 工作(put your Pi to work)。不过(Don),不要屈服于分析瘫痪——只需选择一个项目并投入其中!这确实是最好的学习方式。
How to SSH or SFTP Into Your Raspberry Pi
You’ve taken the plunge and bought a Raspberry Pi. Congrаtulations! If you don’t have а kеyboard аnd monitor сonnected to your Pi, you’ll need to remotely connect to it frоm another cоmрuter on your network. Moreover, depending on how yоu plan to uѕe your Pi, it might be placed in a difficult-to-reach place, so even if you have a keyboard and monitor attached now, that mіght not alwаys be the case. You need some way to remotely connect to your Pi.
In this article, we’ll show you how to use SSH and SFTP into your Pi, execute commands, and transfer files. We’ll assume that your Raspberry Pi is on the same network as the computer you’re using to connect to it.
What Is SSH?
SSH stands for Secure Shell. It’s a standard network protocol that gives you a secure way to send text back and forth between your Pi and the computer you’re using to remotely connect to your Pi. You’ll use the SSH protocol for all kinds of things like entering your password to log into your Pi and executing commands.
SSH was designed as a replacement for Telnet, which sends commands in plain text over the network. In contrast, SSH encrypts those commands. Any network service can use SSH, which operates over TCP port 22.
What Is SFTP?
SFTP is short for Secure File Transfer Protocol. SFTP is a fast and convenient way to transfer files between your Pi and another computer on your network. SFTP is more secure than FTP because, just like with SSH, all your data is encrypted while it’s traveling from one computer to another.
You don’t want all your data to be transferred in plain text, do you? Certainly not! Stay safe, kids.
How to SSH Into a Raspberry Pi
We’ll start with how to use SSH to connect to your Raspberry Pi from a Windows PC on your network using PuTTY. PuTTY is one of several SSH clients that are freely available. We like PuTTY because it’s open source software that’s completely developed and supported by a team of volunteers. You can use whatever SSH client you want. Just be sure to download and install it before you begin.
- Make sure SSH is enabled on your Pi. If you’re starting from scratch with a Raspberry Pi 4, this getting started guide includes instructions on how to enable SSH by creating a text file on the SD card within your Raspberry Pi.
- Assuming you’re certain that SSH is enabled on your Pi, next you’ll need to find your Pi’s IP address in order to connect to it with an SSH client like PuTTY. If you don’t already know it, you can follow this guide which details several methods of determining your Pi’s IP address.
- Launch PuTTY (or another SSH client) on your Windows PC and enter your Pi’s IP address. The SSH port is 22. Most SSH clients will have the port number filled in already.
- Select Open to connect to your Pi.
- This will launch a terminal window where you’ll be asked to login. Enter your username and press Enter. (The default username for most Pis is pi.)
- Next, type your password or copy it to the clipboard on your PC and single-right-click in the terminal window to paste it. Note that your password will not be displayed as you type. Press Enter.
You are now connected to your Pi via SSH and can use the terminal to execute commands. If you haven’t changed your Pi’s default password yet, this is a great time to do that. Since SSH is enabled, if your Raspberry Pi is using the default password, it would be very easy for someone to break in!
How to SFTP Into a Raspberry Pi
Above, we walked you through how to use an SSH client to securely connect to a Pi on your network and execute commands. For transferring files, we’ll use an SFTP client. Technically, it is possible to transfer files with PuTTY, but it’s cumbersome. It’s much nicer to have a file browser you can use to move files in bulk when you need to.
FileZilla is a popular FTP client that supports SFTP. If you can connect with SSH, then you can connect with SFTP as well. Convenient, isn’t it?
Prepare by downloading and installing FileZilla or your SFTP client of choice on the computer you’re using to remotely connect to your Pi. WinSCP is another popular SFTP client. Remember, you don’t need an SFTP server, just an SFTP client.
Pro Tip: If you install FileZilla, watch out for when it asks you to install McAfee and WinZip. Select Decline if you don’t want to install those programs on your computer.
- Launch FileZilla or another SFTP client.
- In the field marked Host, enter your Pi’s IP address.
- Enter your Username and Password.
- Select Quickconnect. Your SFTP client might display an option to save your password, but we don’t recommend it because some clients save that password in an unsecure text file.
- Once connected, you can navigate through the folder structure on your Pi and copy files back and forth between the Pi and other computers on your network. The left side of the FileZilla window displays local files, and the right side displays files on the computer you connected to—in this case, your Pi.
If, for example, you wanted to copy a file from your local computer to your Pi, you’d navigate to the source folder on the left and the destination folder on the right. FileZilla’s file manager allows you to drag and drop files between local and remote sites.
Finding Pi Projects
Once you get comfortable with executing commands and transferring files to and from your Pi, you’ll start to realize just how many different ways you can put your Pi to work. Don’t give in to analysis paralysis, though—just pick a project and dive in! It really is the best way to learn.