Mounting SSH Folders In Ubuntu

Mounting SSH Folders In Ubuntu is a guide that walks through the process describing some of the methods available. It is part of a larger whole setting up and maintaining an Opensimulator server. However, it works equally well as a stand-alone article.

Full Guide

Forword

Security

Most guides online that cover this process add the mount to the system fstab, thus enabling it at system start. Unfortunately, that requires mounting the folder with the root account instead of a user account. Typically it then uses a setting that allows all users of that system access to the mount folder. If and only if the computer has a single user, is that a safe option.

Specific Purpose

Setting fstab to only give a specific user access is possible and may be desirable in many situations. However, this article is part of a larger whole with different purposes. Rather than using fstab, this article creates a systemD service to launch a single BASH script at startup. This bash script will then mount any server folders required. The approach allows the user rather than root to easily mount and un-mount folders after that. Additionally, in a later article, another program programmatically adjusts which folders to mount.

Getting Started – Mounting SSH Folders In Ubuntu

Like other articles in this series, it uses the following server details.

  • Ip Address: 192.168.1.232
  • User Name: opensim
  • Key pair authentication

Values above need substituting for the corresponding ones on the server.

Firstly check that sshfs is available using apt in a terminal.

sudo apt install sshfs

Secondly, create a folder to mount the folder from the remote machine. Again, this can be anywhere accessible to the user account. To keep things simple, this example creates a directory inside the user’s home directory called “ServersMount”. Additionally, it creates another folder inside the new one for each server. The second level is again part of the larger goal and not necessary to mount a single server folder.

mkdir is the command to make a directory. The second part of the command is the folder name. In each section there are two examples; the first is a generic one with values inside double asterisks which need substituting. Secondly, there is a precise instance that uses the parameters above.

mkdir **path to top level folder**
mkdir **path to top level folder**/**Server Folder Name**
mkdir ~/ServersMount
mkdir ~/ServersMount/192.168.1.232

Thirdly check the mount is by typing directly into the terminal, remembering to substitute the port, user name and IP address.

Below are the generic version and the one with values in this example. In the generic version, each value inside double asterisks needs replacing. The instance with exact values uses “~” to specify the current user’s home folder. E.g. if the current user is “opensim”, then “/home/opensim” is the same as “~/”.

  • sshfs is the command to mount a folder using ssh
  • -p sets the port number
  • -o specifiys that options will be set
  • The options tell sshfs to user default permisions, an identity file instead of password authentication and the location of the file.
  • default permisions all all file activity availible to the user on the remote machine.
  • the final two entries are the remote user, remote path and local path.
sshfs -p **PortNumber** -o default_permissions,IdentityFile=**PathToRsaKeyFile** **userName**@**IpAddress/Url**:/**Remote Folder** /**local folder**/
sshfs -p 2789 -o default_permissions,IdentityFile=~/.ssh/id_rsa [email protected]:/home/opensim/ ~/ServersMount/192.168.1.232

Assuming it’s working, it’s time to create a Bash script and SystemD service, but first, unmount the folder again.

umount ~/**Mounted folder**
umount ~/SerersMount/192.168.1.232

Creating a Bash Shell Script – Mounting SSH Folders In Ubuntu

Use a text editor to create a new file. The name and location are not important. In this example, the location is the user’s home folder, and the file’s name is “MountServers.sh”. The editor in the sample is “nano”; however, it could be any graphical editor such as “Gedit” in ubuntu.

nano ~/MountServers.sh

Add the following; again, two examples are given; one is generic where everything inside double asterisk needs replacing and then the precise version.

#!/bin/bash
sshfs -p **PortNumber** -o default_permissions,IdentityFile=**PathToRsaKeyFile** **userName**@**IpAddress/Url**:**Remote Folder** **local folder**
#!/bin/bash
sshfs -p 2789 -o default_permissions,IdentityFile=~/.ssh/id_rsa [email protected]:$HOME/opensim/ $HOME/ServersMount/192.168.1.232

In the script above, there are a few things worthy of note.

  • “#!/bin/bash” is a way of specifying this is a Bash shell script. It is needed so the the operating system knows how to read the file.
  • “~” is substituted with “$HOME”. They both mean the same thing, however they both have to be used in their specific contexts. Inside a BASH shel script the users home folder is specified with $HOME.

Save and close the text editor. In nano, this is done with

CTRL+O
CTRL+x

Next, it is necessary to make the script executable; otherwise, it will not run. Below is the generic and precise way to do this. Chmod is the command that changes file permission, and “+x” tells chmod to add “execute” permission to the file. Finally, the last part of the command is the location and file name.

chmod +x **PathToFile**/**FileName**
chmod +x ~/MountServers.sh

What about unmounting?

Unmounting in a terminal is simple; again, it follows a generic and specific example.

umount **folder path**
umount ~/SerersMount/192.168.1.232

Duplicate the process above for creating the bash script for mounting to make a new one for unmounting. Exactly like above, the examples below have a generic version and precise version.

#!/bin/bash
umount **folder path**
#!/bin/bash
umount ~/SerersMount/192.168.1.232

Creating A SystemD service.

Creating a systemD service requires root privileges. However, similar to creating the BASH scripts above, it can be done with any text editor that can run as root or sudo. The example uses the command line editor nano.

sudo nano /etc/systemd/system/MountServers.service

Add text from the examples below, substituting where necessary. Unlike everything up to this point, creating a systemD service requires stating specific details about the local user rather than the remote user. Similar to the above, there are two examples, one generic and one specific. However, in the particular example, it is necessary to understand this is for the local user. Within the particular instance, “sara” is the user name (the author’s name).

[Unit]
Description= Mount SSH Folders 
After=syslog.target network.target ufw.service   
[Service]
Type=simple
User=**User Name**
Group=**Group Name**
WorkingDirectory=**Path To Folder**
ExecStart=**Path To File**/**File Name**
ExecStop=**Path To File**/**File Name**
RemainAfterExit=yes
KillMode=none
Environment=USER=**user name**  HOME=/home/**user name**
[Install]
WantedBy=multi-user.target
[Unit]
Description= Mount SSH Folders 
After=syslog.target network.target ufw.service   
[Service]
Type=simple
User=sara
Group=sara
WorkingDirectory=/home/sara
ExecStart=/home/sara/MountServers.sh
ExecStop=/home/sara/UnMountServers.sh
RemainAfterExit=yes
KillMode=none
Environment=USER=sara  HOME=/home/sara
[Install]
WantedBy=multi-user.target

Save the file and exit the editor. Nano users do this with.

CTRL+O
CTRL+x

Finally, start the service and enable it, so it launches at system startup.

sudo systemctl start MountServers.service
sudo systemctl enable MountServers.service

Related Post

One thought on “Mounting SSH Folders In Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *