如何在家中使用 SSH 和 SFTP 协议
SSH SFTP

> 
> 通过 SSH 和 SFTP 协议，我们能够访问其他设备，有效而且安全的传输文件等等。
> 
> 
> 


![](/data/attachment/album/201810/18/205017sh5ghl6w7eaihdd1.png)


几年前，我决定配置另外一台电脑，以便我能在工作时访问它来传输我所需要的文件。要做到这一点，最基本的一步是要求你的网络提供商（ISP）提供一个固定的地址。


有一个不必要但很重要的步骤，就是保证你的这个可以访问的系统是安全的。在我的这种情况下，我计划只在工作场所访问它，所以我能够限定访问的 IP 地址。即使如此，你依然要尽多的采用安全措施。一旦你建立起来这个系统，全世界的人们马上就能尝试访问你的系统。这是非常令人惊奇及恐慌的。你能通过日志文件来发现这一点。我推测有探测机器人在尽其所能的搜索那些没有安全措施的系统。


在我设置好系统不久后，我觉得这种访问没什么大用，为此，我将它关闭了以便不再为它操心。尽管如此，只要架设了它，在家庭网络中使用 SSH 和 SFTP 还是有点用的。


当然，有一个必备条件，这个另外的电脑必须已经开机了，至于电脑是否登录与否无所谓的。你也需要知道其 IP 地址。有两个方法能够知道，一个是通过浏览器访问你的路由器，一般情况下你的地址格式类似于 192.168.1.254 这样。通过一些搜索，很容易找出当前是开机的并且接在 eth0 或者 wifi 上的系统。如何识别你所要找到的电脑可能是个挑战。


更容易找到这个电脑的方式是，打开 shell，输入 ：



```
ifconfig
```

命令会输出一些信息，你所需要的信息在 `inet` 后面，看起来和 192.168.1.234 类似。当你发现这个后，回到你要访问这台主机的客户端电脑，在命令行中输入 ：



```
ssh gregp@192.168.1.234
```

如果要让上面的命令能够正常执行，`gregp` 必须是该主机系统中正确的用户名。你会被询问其密码。如果你键入的密码和用户名都是正确的，你将通过 shell 环境连接上了这台电脑。我坦诚，对于 SSH 我并不是经常使用的。我偶尔使用它，我能够运行 `dnf` 来更新我所常使用电脑之外的其它电脑。通常，我用 SFTP ：



```
sftp grego@192.168.1.234
```

我更需要用简单的方法来把一个文件传输到另一个电脑。相对于闪存棒和额外的设备，它更加方便，耗时更少。


一旦连接建立成功，SFTP 有两个基本的命令，`get`，从主机接收文件 ；`put`，向主机发送文件。在连接之前，我经常在客户端移动到我想接收或者传输的文件夹下。在连接之后，你将处于一个顶层目录里，比如 `home/gregp`。一旦连接成功，你可以像在客户端一样的使用 `cd`，改变你在主机上的工作路径。你也许需要用 `ls` 来确认你的位置。


如果你想改变你的客户端的工作目录。用 `lcd` 命令（ 即 local change directory 的意思）。同样的，用 `lls` 来显示客户端工作目录的内容。


如果主机上没有你想要的目录名，你该怎么办？用 `mkdir` 在主机上创建一个新的目录。或者你可以将整个目录的文件全拷贝到主机 ：



```
put -r thisDir/
```

这将在主机上创建该目录并复制它的全部文件和子目录到主机上。这种传输是非常快速的，能达到硬件的上限。不像在互联网传输一样遇到网络瓶颈。要查看你能在 SFTP 会话中能够使用的命令列表：



```
man sftp
```

我也能够在我的电脑上的 Windows 虚拟机内用 SFTP，这是配置一个虚拟机而不是一个双系统的另外一个优势。这让我能够在系统的 Linux 部分移入或者移出文件。而我只需要在 Windows 中使用一个客户端就行。


你能够使用 SSH 或 SFTP 访问通过网线或者 WIFI 连接到你路由器的任何设备。这里，我使用了一个叫做 [SSHDroid](https://play.google.com/store/apps/details?id=berserker.android.apps.sshdroid) 的应用，能够在被动模式下运行 SSH。换句话来说，你能够用你的电脑访问作为主机的 Android 设备。近来我还发现了另外一个应用，[Admin Hands](https://play.google.com/store/apps/details?id=com.arpaplus.adminhands&hl=en_US)，不管你的客户端是平板还是手机，都能使用 SSH 或者 SFTP 操作。这个应用对于备份和手机分享照片是极好的。




---


via: <https://opensource.com/article/18/10/ssh-sftp-home-network>


作者：[Geg Pittman](https://opensource.com/users/greg-p) 选题：[lujun9972](https://github.com/lujun9972) 译者：[singledo](https://github.com/singledo) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Years ago, I decided to set up an extra computer (I always have extra computers) so that I could access it from work to transfer files I might need. To do this, the basic first step is to have your ISP assign a fixed IP address.

The not-so-basic but much more important next step is to set up your accessible system safely. In this particular case, I was planning to access it only from work, so I could restrict access to that IP address. Even so, you want to use all possible security features. What is amazing—and scary—is that as soon as you set this up, people from all over the world will *immediately* attempt to access your system. You can discover this by checking the logs. I presume there are bots constantly searching for open doors wherever they can find them.

Not long after I set up my computer, I decided my access was more a toy than a need, so I turned it off and gave myself one less thing to worry about. Nonetheless, there is another use for SSH and SFTP inside your home network, and it is more or less already set up for you.

One requirement, of course, is that the other computer in your home must be turned on, although it doesn’t matter whether someone is logged on or not. You also need to know its IP address. There are two ways to find this out. One is to get access to the router, which you can do through a browser. Typically, its address is something like **192.168.1.254**. With some searching, it should be easy enough to find out what is currently on and hooked up to the system by eth0 or WiFi. What can be challenging is recognizing the computer you’re interested in.

I find it easier to go to the computer in question, bring up a shell, and type:

`ifconfig`


This spits out a lot of information, but the bit you want is right after `inet`

and might look something like **192.168.1.234**. After you find that, go back to the client computer you want to access this host, and on the command line, type:

`ssh gregp@192.168.1.234`


For this to work, **gregp** must be a valid user on that system. You will then be asked for his password, and if you enter it correctly, you will be connected to that other computer in a shell environment. I confess that I don’t use SSH in this way very often. I have used it at times so I can run `dnf`

to upgrade some other computer than the one I’m sitting at. Usually, I use SFTP:

`sftp gregp@192.168.1.234`


because I have a greater need for an easy method of transferring files from one computer to another. It’s certainly more convenient and less time-consuming than using a USB stick or an external drive.


Once you’re connected, the two basic commands for SFTP are `get`

, to receive files from the host; and `put`

, to send files to the host. I usually migrate to the directory on my client where I either want to save files I will get from the host or send to the host before I connect. When you connect, you will be in the top-level directory—in this example, **home/gregp**. Once connected, you can then use `cd`

just as you would in your client, except now you’re changing your working directory on the host. You may need to use `ls`

to make sure you know where you are.

If you need to change the working directory on your client, use the command `lcd`

(as in **local change directory**). Similarly, use `lls`

to show the working directory contents on your client system.

What if the host doesn’t have a directory with the name you would like? Use `mkdir`

to make a new directory on it. Or you might copy a whole directory of files to the host with this:

`put -r ThisDir/`


which creates the directory and then copies all of its files and subdirectories to the host. These transfers are extremely fast, as fast as your hardware allows, and have none of the bottlenecks you might encounter on the internet. To see a list of commands you can use in an SFTP session, check:

`man sftp`


I have also been able to put SFTP to use on a Windows VM on my computer, yet another advantage of setting up a VM rather than a dual-boot system. This lets me move files to or from the Linux part of the system. So far I have only done this using a client in Windows.

You can also use SSH and SFTP to access any devices connected to your router by wire or WiFi. For a while, I used an app called [SSHDroid](https://play.google.com/store/apps/details?id=berserker.android.apps.sshdroid), which runs SSH in a passive mode. In other words, you use your computer to access the Android device that is the host. Recently I found another app, [Admin Hands](https://play.google.com/store/apps/details?id=com.arpaplus.adminhands&hl=en_US), where the tablet or phone is the client and can be used for either SSH or SFTP operations. This app is great for backing up or sharing photos from your phone.

## 7 Comments