1

给手机设置受限的SFTP服务器

 2 years ago
source link: https://www.lainme.com/doku.php/blog/2022/04/%E7%BB%99%E6%89%8B%E6%9C%BA%E8%AE%BE%E7%BD%AE%E5%8F%97%E9%99%90%E7%9A%84sftp%E6%9C%8D%E5%8A%A1%E5%99%A8
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

给手机设置受限的SFTP服务器

准备允许自己的手机通过SFTP读写服务器上的一些文件,但无论是密码登陆还是密钥登陆都不能让我很放心,所以做了一些额外的配置并且要求

  • 手机只能登陆SFTP不能登陆SSH,且只能读写指定的目录
  • 不能影响任何现有的功能,比如我桌面端的登陆

先给手机建立一个专门的目录,目录及上级目录都要是root所有(为chroot做准备)

sudo mkdir /home/mobile

然后将所有允许读写的目录都mount bind过来。比如目录/home/[用户名]/Documents

sudo mkdir /home/mobile/Documents
sudo chown [用户名]:[用户名] /home/mobile/Documents
sudo mount --bind /home/[用户名]/Documents /home/mobile/Documents

然后设置SSH服务器如下(只显示关键配置)

Port 22
Port 2333

Subsystem sftp internal-sftp

Match LocalPort 2333
        AllowGroups [用户名]
        AllowUsers [用户名]
	X11Forwarding no
	AllowTcpForwarding no	
        AuthorizedKeysFile /home/[用户名]/.ssh/authorized_keys_sftp
	ChrootDirectory /home/mobile/
	ForceCommand internal-sftp

这里除了原本的22端口额外设置了一个2333端口,并且在连接这个端口的时候用authorized_keys_sftp来验证,chroot到/home/mobile目录,并且只允许SFTP。

然后重启SSH服务器

sudo systemctl restart sshd

在防火墙上放行2333端口,并将一组密钥对的公钥放到/home/[用户名]/.ssh/authorized_keys_sftp,私钥放到手机连接SFTP的APP上就可以连接了。密钥也可以设置Passprase。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK