1

I have the following problem:

I have my network drive in my home network that I would like to mount via sshfs. Being in my local network I don't have to care that much about encryption and could use the arcfour cipher for example. My ssh port internally is A. For technical reasons I can connect from the outside network via ssh on the port B which is not the same as A. I am also not able to connect from the outside to that port while I am in my home network. Now, mounting from the outside, I would of course rely on other encryption.

I would like to build a systemd-service which handles that mount situation reasonably well, using arcfour on port A iff I am in network with ID X and connecting via port B with another cipher in all other cases. I'm still somewhat new to writing my own services and could not find the right condition that would work here.

Could someone help me out with this?

0

I'd suggest, you run two actions: one upon login (for mounting), the other one upon logout (unmounting). A sample service file:

[Unit]    
Description=mount with sshfs

[Service]
Type=simple
ExecStart=/path/to/login_script.sh
#This makes the service stay active while logged in and
#  makes sure ExecStop is only executed at logout
RemainAfterExit=yes
ExecStop=/path/to/logout_script.sh

[Install]
#opens with first login and ends with last logout - multiple ssh sessions are OK
WantedBy=default.target

The write scripts for login and logout. I'll just make a check by SSID to see if we're at home or not. See what works out for you.

#!/bin/bash
#login script
if [[ "$( nmlci | grep 'home_network_name' )" != "" ] ; then
   #we're at home
   sshfs <mount with home options>
else
   #we're out
   sshfs <mount with outside options>
fi

and after logging out, you want to unmount

#!/bin/bash
#logout script
fusermount -u /path/to/mount/point

To activate the service put it in e.g. ~/.cofig/systemd/user/autosshfs.service and run systemctl --user enable autosshfs.service. It should work on next login.

| improve this answer | |
  • Hey Fiximan! That sounds like a robust start. Is it possible to also cover the situation that I leave the network while logged on? Say I decide to leave home with my laptop and resume my session later in a different network. – Daniel Bischof Oct 28 '19 at 18:00
  • Well, you can just run an infinite while loop in the login script with a nested if-statement: while ((1)) ; do if [ <mounted> ] ; then if [ <check network matching mount staus?> ] ; then <yes: do nothing> ; else <no: change mounting> ; fi ; else <mount the right way> ; fi ; sleep 60 ; done – Fiximan Oct 28 '19 at 18:16
  • So I still have to figure out how to really build a stable login script - simply sshfs-ing if the service is already potentially connected leads to somewhat nasty side effects which freeze my shell (so not even "ls" terminates for quite a while). However, your suggestion helped me a lot so I'll mark it as answered - thanks! I just want to add that it seems to be 'cleaner' to use a systemd.timer instead of building that script repetition. – Daniel Bischof Oct 30 '19 at 21:30
  • Yes, a timer sounds more sound. You can always scan for sshfs-mounts before doing any operation. – Fiximan Oct 30 '19 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.