📜 ⬆️ ⬇️

Network hard drive using SSHFS and Raspberry Pi



Hello!

I want to share with you my experience of creating a network hard drive on the Raspberry Pi. My article is perfect for those who use Linux as the main OS, because here I am actively using SSHFS.
')
All interested in asking under the cat.

Step 0. Preparation


We need:


Step 1. Installation


First you need to install on Raspberry Pi Raspbian or Arch Linux ARM.
In the article, I will use Raspbian.
  1. Go to the Raspberry Pi website in the download section and download the Raspbian image
  2. Unpack the archive
  3. Open the terminal and go to the folder with the image
  4. We insert the memory card into the computer
  5. Enter df -h in the terminal and look at the output. The output should contain / dev / mmcblk0p1 or / dev / sdd1. In my case, this is / dev / mmcblk0p1. p1 (or 1, if your card is displayed as / dev / sdd1) is the partition number, if there are several sections on the card, the output will still be / dev / mmcblk0p2 or / dev / sdd2. The number of entries in the output depends on how many sections you have.
  6. So, having learned how your card is displayed in the system, we proceed to the image recording.
    1. In the terminal, run umount / dev / mmcblk0p1 (or umount / dev / sdd1). If you have multiple partitions, you need to unmount everything.
    2. We will record the image on the card using the dcfldd program. The dcfldd program works in the same way as dd, but periodically reports how much has already been recorded. Enter the following command in the terminal:

      $ sudo dcfldd bs=4M if=___.img of=___ 

      In my case, the command looks like this:

       $ sudo dcfldd bs=4M if=2014-12-24-wheezy-raspbian.img of=/dev/mmcblk0 

      Please note that on the way to your map it is not necessary to specify the section number (even if it is one). The image is recorded on the entire card.
      bs - block size - the size of one block that is written to the card. If the recording does not work with 4M installed, then 1M must be supplied, but this will increase the recording time.
    3. After the image is written, run the command:

       $ sync 

      This will ensure that all data has been written to the card and clear the buffer.
  7. Everything. The card can be removed and inserted into the Raspberry Pi.


Step 2. First start


  1. Insert the card into the Raspberry Pi;
  2. We connect to the Raspberry Pi internet, monitor and keyboard;
  3. Turn on the Raspberry Pi;
  4. After the system has booted, you should see the Raspberry Pi Software Configuration Tool window.
    1. Select the item Expand Filesystem and press Enter. This will make sure that the entire volume of the card is available OS
    2. Next, go to step 2 - Change User Password and enter a new password;
    3. Go to step 3 - Enable Boot to ... and select the Console Text console;
    4. Go to step 4 - Internationalization Options and then go to the item Change Locale and look in the list en_RU.UTF-8 UTF-8, mark with a space and press Enter. Then the system will ask which locale to use by default. Select Russian and press Enter;
    5. Go back to the Internationalization Options and then go to the item Change Timezone. Choose Europe and look for your city in the list. Then press Enter;
    6. Go to step 7 - Overclock (we will accelerate our baby a little) and select the Medium level;
    7. Go to step 8 - Advanced Options and go to step A2 - Hostname. Enter the name under which we want to see our baby in the local network and press Enter;
    8. Go back to Advanced Options and go to A4 - SSH and turn on SSH;
    9. That's where our adventures in the Raspberry Pi Software Configuration Tool are over. Click the right arrow and Finish.
  5. If you need to get into this program again, you need to enter:

     $ sudo raspi-config 
  6. Now you need to restart the system. In the console, enter the command:

     $ sudo reboot 
  7. After the system reboots go to the account. The username is the password that you entered earlier;
  8. Now you need to upgrade your OS. To do this, enter:
     $ sudo rpi-update 
    and after that
     $ sudo apt-get update 
    and
     $ sudo apt-get upgrade 



Step 3. Configure SSH


  1. First of all, it is necessary to generate keys (if they are not yet generated). To do this on the main OS, run:

     $ ssh-keygen -t rsa -C "your_email@example.com" 
  2. Then the keys must be copied to your Raspberry Pi.
    1. For this you need to know under what ip your baby is online. This can be found by going to the router, for example, or using the command:
       $ ifconfig 

      I have an ip 192.168.0.102.
    2. Now we execute the command on the main OS:

       $ ssh-copy-id pi@192.168.0.102 

      You will be asked to enter consent to continue the connection. You must enter yes. Then you will be asked to enter the password from the account on the Raspberry Pi. We enter and it.
  3. Now you can try to connect to your Raspberry Pi via ssh. To do this, on the main machine, type:

     $ ssh pi@192.168.0.102 
  4. If you see a greeting message, then, hooray, you are logged in.
  5. Now you can disable the monitor and keyboard from the Raspberry Pi. All further actions we will be through SSH


Step 4. Brushing Raspberry Pi


  1. Firstly, you need to set a password for root. by default it is absent. To do this, run:
     $ sudo passwd root 
    And enter the new password for root
  2. Install the vim editor:
     $ sudo apt-get install vim 
    You can also use the nano editor, which is already installed. But I prefer vim.
  3. Now I want to change my username. To do this, exit using the exit command. Login again via ssh, but under the root.
     $ ssh root@192.168.0.102 
    and execute the command:
     $ usermod -l _ -d /home/_ -m pi 
    I use inn0kenty as a new name, i.e. in my case, the command looks like this:
     $ usermod -l inn0kenty -d /home/inn0kenty -m pi 



Step 5. Static ip


DHCP works for me, so every time the Raspberry Pi connects to the home network, it will receive a new ip address, which of course does not suit me. In order to make ip static you need to run:
  1. Enter in the terminal
     $ sudo vim /etc/network/interfaces 
    . In the opened file we change
     iface eth0 inet dhcp 
    on
     iface eth0 inet static address 192.168.0.98 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.1 
    Indicating the parameters of your network. Netmask and broadcast can be found with the command
     $ sudo ifconfig 

    and gateway using
     $ sudo route -nee 
  2. Then reboot the Raspberry Pi with the sudo reboot command
  3. Now in order to connect to the Raspberry Pi by shh you must enter
     $ ssh inn0kenty@192.168.0.98 


Step 6. We brush SSH


In this step, I will show how to simplify ssh connection and make it more secure.
  1. First of all, open the ssh configuration file with the command:
     $ sudo vim /etc/ssh/sshd_config 
    And set it to prohibit ssh login as root and disable password authentication:
     PasswordAuthentication no PermitRootLogin no 
    And also change the port
     Port 22226 () 
  2. Then restart the Raspberry Pi
     $ sudo reboot 
  3. Now you need to specify port 22226 to connect to the Raspberry Pi
     $ ssh -p 22226 inn0kenty@192.168.0.98 
    But such a long team can be simplified and minimized. To do this, on your primary OS, edit the config file. In your home directory, run:
     $ vim .ssh/config 
    and paste the following lines there:
     Host 192.168.0.98 Hostname 192.168.0.98 User inn0kenty Port 22226 

    After that, to connect to your Raspberry Pi you only need to dial
     $ ssh 192.168.0.98 



Step 7. Connect to the Raspberry Pi from the outside.


In this section, I want to tell how to connect via ssh to the Raspberry Pi, not only from the local network, but also from the outside.
If you have an external ip static, then, cheers, you can not particularly bathe. It is enough to make redirection from external ip to internal.
But if you, like me, have an external ip dynamic, then dynamic dns comes to the rescue.
Personally, I use noip.com. it is free, although it requires periodically to go to the site and confirm that you still use the address they gave you.
So, register there and go to Add a Host.
  1. In the hostname, enter the address you want to use to connect from the outside and click on the Add Host button without changing anything else
  2. Now it is necessary that noip know when your ip changes. There are two ways. First, if your router supports the Dynamic Domain Name System (DDNS) function, it is enough to enter account data and domain name in the router settings. After that, the router will inform noip about changing the ip address. Second, you need to put the program on your main computer. In noip, this is the Dynamic DNS Update Client. You can take it from here.
  3. Next step: it is necessary to redirect from the external ip to your local one.
    To do this, in my router, you need to go to Redirect -> Virtual Servers and add the following entry there:
    Service port: 22226
    Inner port: 22226
    IP Address: 192.168.0.98
    Protocol: all
  4. Now you need to add data from the outside to the ssh configuration file on your main OS.
    Open the config file
     $ vim .ssh/config 
    And add there
     Host _ Hostname _ User inn0kenty Port 22226 
    We save.
  5. Everything. Now you can connect to the Raspberry Pi not only from home


Step 8. Connecting HDD


  1. First you need to create a folder on your Raspberry Pi where your HDD will be mounted
     $ mkdir hdd 
  2. Now connect the hard drive to the Raspberry Pi and enter the command
     $ sudo blkid 
    The output should be something like this:
     /dev/mmcblk0p1: SEC_TYPE="msdos" LABEL="boot" UUID="936C-7154" TYPE="vfat" /dev/mmcblk0p2: UUID="c1398422-7a7c-4863-8a8f-45a1db26b4f2" TYPE="ext4" /dev/sda1: LABEL="Seagate Expansion Drive" UUID="7CD8E7DCD8E792A6" TYPE="ntfs" 
    The last line is just what we need. Remember UUID.
  3. Now you need to install file system support
     $ sudo apt-get install ntfs-3g 
  4. Open the fstab file
     $ sudo vim /etc/fstab 
  5. Add this line there:
     UUID="*" ** ntfs-3g rw,force,exec,users 0 0  * -   UUID (  7CD8E7DCD8E792A6)  ** -       (  /home/inn0kenty/hdd) 
  6. Save and reboot the Raspberry Pi
  7. After rebooting, all files and folders from the hard disk should appear in the hdd folder.


Step 9. SSHFS


  1. Install sshfs on your main OS
     $ sudo apt-get install sshfs 
  2. Create a folder in your home directory where Raspberry Pi will be mounted.
     $ mkdir pi 
  3. Mount the Raspberry Pi in this folder:
     $ sshfs 192.168.0.98:/home/inn0kenty/hdd pi 
    Everything. All our files from the hard disk were in the pi folder on your main machine.
  4. In order to unmount the Raspberry Pi, enter the command
     $ sudo umount pi 
    or
     $ fusermount -u pi 
  5. In order to do the same from the outside it is enough to change the command a little.
     $ sshfs _:/home/inn0kenty/hdd pi 
  6. These commands can be written in fstab on your main machine, so that the mount is performed every time you turn on the computer, but for me this is not a suitable option. The main computer I have is a laptop and the Internet when turned on is not always there. Therefore, I suggest using a small script that will easily mount your Raspberry Pi with one short command.
     #!/bin/bash ABSOLUTE_FILENAME=`readlink -e "$0"` #  #      DIRECTORY=`dirname "$ABSOLUTE_FILENAME"` if [ "x$1" = "x-nh" ] || [ "x$1" = "x-nothome" ] ; then sshfs -o nonempty _:/home/inn0kenty/hdd $DIRECTORY # -o nonempty  sshfs       #    else sshfs -o nonempty 192.168.0.98:/home/inn0kenty/hdd $DIRECTORY fi 
    1. Save this script in the file mount.sh and make it executable
       $ chmod +x mount.sh 
    2. And put it in the pi folder
       $ mv mount.sh pi/ 
    3. Then add an alias to run the script with one command. Open bashrc (if not, create one).
       $ vim .bashrc 
      And add the line there
       alias pimount='/home/inn0kenty/pi/mount.sh' 
      Of course substituting your data.
      Save and restart bash.
      Now you can mount your Raspberry Pi with one command
       $ pimount 
      And if you specify the -nh or -nothome key, then your Raspberry Pi will be mounted via the domain name i.e. You can use your Raspberry Pi to the full even without being in the same local network with it.
       $ pimount -nh 
      or
       $ pimount -nothome 


Step 10. Deluge


The last step - installing torrent rocking. I prefer the deluge.
  1. Enter in turn the commands:
     $ sudo apt-get install deluged deluge-webui 
  2. Then we perform
     $ sudo vim /etc/default/deluge-daemon 
    and enter there the following:
     # Configuration for /etc/init.d/deluge-daemon # The init.d script will only run if this variable non-empty. DELUGED_USER="__" # !!!CHANGE THIS!!!! # Should we run at startup? RUN_AT_STARTUP="YES" 
    Then save and close
  3. And we perform:
     $ sudo vim /etc/init.d/deluge-daemon 
    and enter there the following:
    Big script
     #!/bin/sh ### BEGIN INIT INFO # Provides: deluge-daemon # Required-Start: $local_fs $remote_fs # Required-Stop: $local_fs $remote_fs # Should-Start: $network # Should-Stop: $network # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Daemonized version of deluge and webui. # Description: Starts the deluge daemon with the user specified in # /etc/default/deluge-daemon. ### END INIT INFO # Author: Adolfo R. Brandes # Updated by: Jean-Philippe "Orax" Roemer PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="Deluge Daemon" NAME1="deluged" NAME2="deluge" DAEMON1=/usr/bin/deluged DAEMON1_ARGS="-d" # Consult `man deluged` for more options DAEMON2=/usr/bin/deluge-web DAEMON2_ARGS="" # Consult `man deluge-web` for more options PIDFILE1=/var/run/$NAME1.pid PIDFILE2=/var/run/$NAME2.pid UMASK=022 # Change this to 0 if running deluged as its own user PKGNAME=deluge-daemon SCRIPTNAME=/etc/init.d/$PKGNAME # Exit if the package is not installed [ -x "$DAEMON1" -a -x "$DAEMON2" ] || exit 0 # Read configuration variable file if it is present [ -r /etc/default/$PKGNAME ] && . /etc/default/$PKGNAME # Load the VERBOSE setting and other rcS variables [ -f /etc/default/rcS ] && . /etc/default/rcS # Define LSB log_* functions. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present. . /lib/lsb/init-functions if [ -z "$RUN_AT_STARTUP" -o "$RUN_AT_STARTUP" != "YES" ] then log_warning_msg "Not starting $PKGNAME, edit /etc/default/$PKGNAME to start it." exit 0 fi if [ -z "$DELUGED_USER" ] then log_warning_msg "Not starting $PKGNAME, DELUGED_USER not set in /etc/default/$PKGNAME." exit 0 fi # # Function to verify if a pid is alive # is_alive() { pid=`cat $1` > /dev/null 2>&1 kill -0 $pid > /dev/null 2>&1 return $? } # # Function that starts the daemon/service # do_start() { # Return # 0 if daemon has been started # 1 if daemon was already running # 2 if daemon could not be started is_alive $PIDFILE1 RETVAL1="$?" if [ $RETVAL1 != 0 ]; then rm -f $PIDFILE1 start-stop-daemon --start --background --quiet --pidfile $PIDFILE1 --make-pidfile \ --exec $DAEMON1 --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK -- $DAEMON1_ARGS RETVAL1="$?" else is_alive $PIDFILE2 RETVAL2="$?" [ "$RETVAL2" = "0" -a "$RETVAL1" = "0" ] && return 1 fi is_alive $PIDFILE2 RETVAL2="$?" if [ $RETVAL2 != 0 ]; then sleep 2 rm -f $PIDFILE2 start-stop-daemon --start --background --quiet --pidfile $PIDFILE2 --make-pidfile \ --exec $DAEMON2 --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK -- $DAEMON2_ARGS RETVAL2="$?" fi [ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] || return 2 } # # Function that stops the daemon/service # do_stop() { # Return # 0 if daemon has been stopped # 1 if daemon was already stopped # 2 if daemon could not be stopped # other if a failure occurred start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $DELUGED_USER --pidfile $PIDFILE2 RETVAL2="$?" start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $DELUGED_USER --pidfile $PIDFILE1 RETVAL1="$?" [ "$RETVAL1" = "2" -o "$RETVAL2" = "2" ] && return 2 rm -f $PIDFILE1 $PIDFILE2 [ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] && return 0 || return 1 } case "$1" in start) [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME1" do_start case "$?" in 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; esac ;; stop) [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME1" do_stop case "$?" in 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; esac ;; restart|force-reload) log_daemon_msg "Restarting $DESC" "$NAME1" do_stop case "$?" in 0|1) do_start case "$?" in 0) log_end_msg 0 ;; 1) log_end_msg 1 ;; # Old process is still running *) log_end_msg 1 ;; # Failed to start esac ;; *) # Failed to stop log_end_msg 1 ;; esac ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 exit 3 ;; esac : 

  4. We grant the right to execute this script:
     $ sudo chmod 755 /etc/init.d/deluge-daemon 
  5. Add it to autoload:
     $ sudo update-rc.d deluge-daemon defaults 
  6. And run:
     $ sudo invoke-rc.d deluge-daemon start 
  7. Then we reboot.
    Now if you go to 192.168.0.98:8112, then you can get into the web ui torrent rocking.
  8. Open access from the outside for our web ui. To do this, go back to our router in the Forwarding -> Virtual Servers and add another entry:
    Service port: 80
    Inner port: 8112
    IP Address: 192.168.0.98
    Protocol: all

    After saving, it will be possible to access the web ui by simply typing your domain name in the address bar of the browser.


Conclusion


I hope that my article at least someone will be useful. Thank you for your time and attention.

Source: https://habr.com/ru/post/247783/


All Articles