View on GitHub

OpenWRT sftp-bittorrent guide

Guide to create an OpenWRT SFTP & BitTorrent server

Download this project as a .zip file Download this project as a tar.gz file

OpenWRT SFTP & BitTorrent server

This guide is intended to walk a user through building a router-based multi-user SFTP & BitTorrent (using Transmission) server with OpenWRT. You should have a working router running OpenWRT (and LuCI) before starting. Upon completion, you will have a router with seedbox capabilities. SFTP users (that you manage) will be able to download files from shared directories without having router shell access.

Don't need BitTorrent transfers? Skip the Transmission section and transfer local data to the SFTP server via Samba.

NOTE!

  • This guide assumes you are familiar with command-line terminals and VIM or another command-line text editor
  • Text within brackets such as [username] are variables to be replaced at your discretion
  • All commands are to be run as the root user (use sudo -i after disabling root SSH access)

Prerequisites

Hardware

Software

Via SSH, update OPKG (Open PacKaGe Management).

opkg update

Setup

Replace Dropbear with OpenSSH

Dropbear doesn't work with openssh-sftp-server, so we will replace it with OpenSSH to create a multi-user SFTP server.

opkg install openssh-server openssh-sftp-server
/etc/init.d/sshd enable
/etc/init.d/sshd start
/etc/init.d/dropbear disable
/etc/init.d/dropbear stop

Users and groups

Add OpenWRT users

opkg install shadow-useradd shadow-groupadd shadow-userdel shadow-groupdel shadow-groupmod shadow-usermod
useradd [username]
mkdir /home
mkdir /home/[username]
passwd [username]

Configure shell access for new users

usermod -s /bin/ash [admin_username]
usermod -s /bin/false [sftp_username]

Create groups to manage access rights

groupadd [admin_group_name]
groupadd [sftp_group_name]

Assign groups to users

usermod -G [admin_group_name] [admin_username]
usermod -G [sftp_group_name] [sftp_username]

Secure SSH access

opkg install sudo
visudo
Defaults targetpw  # Ask for the password of the target user
ALL ALL=(ALL) ALL  # WARNING: only use this together with 'Defaults targetpw'
vim /etc/ssh/sshd_config
Port [your_desired_port]
PermitRootLogin no
Subsystem sftp internal-sftp
Match Group [sftp_group_name]
        ForceCommand internal-sftp
        ChrootDirectory %h
        AllowTcpForwarding no
        PermitTunnel no
        X11Forwarding no
        AllowAgentForwarding no
/etc/init.d/sshd restart

At this point you can test SSH and SFTP access for the administrator and other users. Root SSH access is now disabled (use sudo -i as the administrative user to gain sudo privileges) Later on we'll be adding mount points so there's actually a point for SFTP-only users to access the router.

Storage

Connect hardware

Connect your EXT4 USB HDD to the router and if applicable, connect your SWAP formatted USB flash drive. The EXT4 HDD will be used as a network drive to store BitTorrent data in addition to Transmission configuration files.

Install necessary packages

opkg install kmod-fs-ext4
opkg install block-mount

Configure fstab

block detect > /etc/config/fstab
vim /etc/config/fstab
option enabled '1'
option options 'rw'
option target '/mnt/[network_drive]'

Grant users directory access

mkdir /home/[username]/[shared_directory]
vim /etc/rc.local
# Mounts network drive for SFTP users (remember to create /home/[username]/[shared_directory] first)
mount --bind /mnt/[network_drive]/[shared_directory]/ /home/[username]/[shared_directory]/

At this point users can (locally) login and access directories on the network drive you gave them access to. Permissions will be set in the next section.

Permissions

We need to ensure permissions are set correctly on the network drive so that the SFTP group cannot write anything to the drive. We should also give the administrator group write access.

chown -R root:root /mnt/[network_drive]
chmod -R 755 /mnt/[network_drive]

At this point, the SFTP and administrator groups only have read access to the shared directories.

chgrp -R [admin_group_name] /mnt/[network_drive]
chmod -R 775 /mnt/[network_drive]
chmod -R g+s /mnt/[network_drive]

At this point permissions have been correctly configured. After configuring DDNS, your users will be able to safely access your shared directories.

DDNS

There are many different DDNS options available. Follow the guide here to choose and configure a DDNS client that best meets your needs.

NOTE: Add a firewall exception for SSH/SFTP remote access

Example of firewall exception in /etc/config/firewall

At this point your users will be able to access your shared directories remotely by using your DDNS address and SSH/SFTP port.

Samba (optional)

If you're on a Windows-based system, you'll want to access your network drive via Windows File Explorer.

opkg install luci-app-samba
vim /etc/samba/smb.conf.template
force create mode = 0775
force directory mode = 0775
invalid users = root
smbpasswd -a [username]
NOTE: The password you set here is different than the SSH/SFTP password set for the user.

At this point you can mount your network drive locally in Windows via the "Map Network Drive" feature.

BitTorrent

Configure Transmission client

opkg install luci-app-transmission transmission-web
config file directory = /mnt/[network_drive]/transmission
download directory = /mnt/[network_drive]/[specific_directory]
umask = 2

Make sure you configure a RPC username and password for Transmission. Once secured, you can disable the RPC whitelist to enable remote Transmission access (you'll still need to configure a firewall exception for Transmission web interface access).

New content permissions

Files downloaded by Transmission won't mirror the existing file/directory permissions, and Transmission settings don't allow the level of control necessary. To work around this, we create a cron job to update permissions for any new content.

# Runs every 10 minutes to apply permissions and setgid over /mnt/[network_drive]/ (to account for data created by luci-app-transmission)
*/10 * * * * /bin/chmod -R 2775 /mnt/[network_drive]

Wrap-up

And that's all folks. You can view Transmission web interface documentation here. The number of BitTorrent transfers possible will depend on your specific router and OpenWRT release stability.

I'll add additional information (and potentially some media of the finished product) in the future. Questions or found a bug? Email me at me@matthewtraughber.com or create an issue or pull request.