From 8c32aaf3fbf64227462f51d6fc8b4906799725a3 Mon Sep 17 00:00:00 2001 From: Ray Lyon Date: Wed, 30 Sep 2020 17:51:13 -0400 Subject: [PATCH 1/3] nas-ubuntu boilerplate --- _posts/2020-10-01-nas-ubuntu.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 _posts/2020-10-01-nas-ubuntu.md diff --git a/_posts/2020-10-01-nas-ubuntu.md b/_posts/2020-10-01-nas-ubuntu.md new file mode 100644 index 0000000..7dccf5d --- /dev/null +++ b/_posts/2020-10-01-nas-ubuntu.md @@ -0,0 +1,9 @@ +--- +layout: single +title: "NAS in 10 Minutes with Ubuntu 20.04, Samba, and NFS" +date: 2020-10-01 22:45:00 +categories: [Linux Administration] +tags: linux ubuntu samba nas +comments: true +--- + From a1f18e5fc57b120ebad12217acddc77c2f1b2af5 Mon Sep 17 00:00:00 2001 From: Ray Lyon Date: Wed, 7 Oct 2020 18:56:34 -0400 Subject: [PATCH 2/3] final revision --- _posts/2020-10-01-nas-ubuntu.md | 9 -- _posts/2020-10-03-systemd-automount.md | 196 +++++++++++++++++++++++++ favicon.ico | Bin 0 -> 15406 bytes 3 files changed, 196 insertions(+), 9 deletions(-) delete mode 100644 _posts/2020-10-01-nas-ubuntu.md create mode 100644 _posts/2020-10-03-systemd-automount.md create mode 100644 favicon.ico diff --git a/_posts/2020-10-01-nas-ubuntu.md b/_posts/2020-10-01-nas-ubuntu.md deleted file mode 100644 index 7dccf5d..0000000 --- a/_posts/2020-10-01-nas-ubuntu.md +++ /dev/null @@ -1,9 +0,0 @@ ---- -layout: single -title: "NAS in 10 Minutes with Ubuntu 20.04, Samba, and NFS" -date: 2020-10-01 22:45:00 -categories: [Linux Administration] -tags: linux ubuntu samba nas -comments: true ---- - diff --git a/_posts/2020-10-03-systemd-automount.md b/_posts/2020-10-03-systemd-automount.md new file mode 100644 index 0000000..42bb0c7 --- /dev/null +++ b/_posts/2020-10-03-systemd-automount.md @@ -0,0 +1,196 @@ +--- +layout: single +title: "Painless On-Demand NAS Connections in Linux with Systemd Automount" +date: 2020-10-02 22:45:00 +categories: [Linux Administration] +tags: linux samba nas systemd ubuntu +comments: true +--- + +If you have a NAS, you probably want to be able to connect to it automatically each time you log into your Linux machine. Adding an entry to your fstab is great for desktops with persistent ethernet connections, but you quickly run into trouble using this method on a laptop that's frequently jumping on and off the network. Your distribution's file manager may have a way to do this, but wouldn't it be nice to have a consistent method accross all modern distributions? Systemd to the rescue! + +Instead of trying to mount the network shares at login or boot, we're going to mount them on-demand by combining Systemd mount and automount units. With this method, the shares won't appear until you navigate to the designated mount points in the terminal or file manager. + +*NOTE:* These instructions were written for **Ubuntu 20.04**, but the principles should apply to nearly all modern distributions. Adjust for your distro's package manager and install locations. + +### Systemd Unit Files + +If you're not familiar with Systemd unit files and how they work, I would highly recommend reading the [Freedesktop.org article](https://www.freedesktop.org/software/systemd/man/systemd.unit.html) on the subject. Unit files are effectively services that can be run on-demand or triggered by events or specific times. For our purposes, we're going to use both [mount](https://www.freedesktop.org/software/systemd/man/systemd.mount.html#) and [automount](https://www.freedesktop.org/software/systemd/man/systemd.automount.html#) unit files. + +### Create a mount point + +You'll need to create dedicated folders on your machine where the shares will be mounted. + +``` bash +$ sudo mkdir -p /mnt/smb/sambashare +$ sudo mkdir -p /mnt/nfs/nfsshare +``` + +### Create a credentials file + +If your Samba server uses authentication, you'll need to create a file with your login details that Systemd can use to connect. These should be saved in a safe location with restricted permissions. + +``` bash +$ sudo nano /etc/samba/smbcreds +``` + +``` +username=[USERNAME] +password=[PASSWORD] +``` + +``` bash +$ sudo chmod 600 /etc/samba/smbcreds +``` + +### Install the client packages + +#### Samba + +``` bash +$ sudo apt install samba cifs-utils +``` + +#### NFS + +``` bash +$ sudo apt install nfs-common +``` + +### Create the systemd unit files + +To make this work, we need (2) unit files for each connection: the **mount** unit and the **automount** unit. These use a specific naming convention that follows the path of the mount point. For example, if our share is at `/mnt/smb/sambashare` the mount file should be named `mnt-smb-sambashare.mount`. The same goes for the automount file. **If you don't name the units this way they will not function.** + +The below instructions assume your samba share is located at `//example.server/sambafiles`. + +``` bash +$ sudo nano /etc/systemd/system/mnt-smb-sambashare.mount +``` + +``` +[Unit] +Description=samba mount for sambafiles +Requires=systemd-networkd.service +After=network-online.target +Wants=network-online.target + +[Mount] +What=//example.server/sambafiles +Where=/mnt/smb/sambashare +Options=vers=2.1,credentials=/etc/samba/smbcreds,iocharset=utf8,rw,x-systemd.automount,uid=1000 +Type=cifs +TimeoutSec=30 + +[Install] +WantedBy=multi-user.target +``` + +A few notes on the above file: +* `vers=2.1` - adjust this based on the version of samba running on your server +* `uid=1000` - adjust this based on your local user ID to avoid permissions problems. This is usually 1000 on a desktop system. + +\ +Next we need to create the automount file in the same location. + +``` bash +$ sudo nano /etc/systemd/system/mnt-smb-sambashare.automount +``` + +``` +[Unit] +Description=samba automount for yourfiles +Requires=network-online.target + +[Automount] +Where=/mnt/smb/sambashare +TimeoutIdleSec=0 + +[Install] +WantedBy=multi-user.target +``` + +#### NFS + +The below instructions assume your NFS share is located at `example.server:/srv/nfsfiles`. + +``` bash +$ sudo nano /etc/systemd/system/mnt-nfs-nfssahre.mount +``` + +``` +[Unit] +Description = nfs mount for nfsfiles + +[Mount] +What=example.server:/srv/nfsfiles +Where=/mnt/nfs/nfsshare +Type=nfs +Options=defaults + +[Install] +WantedBy=multi-user.target +``` + +\ +Same as before, we need to create the automount file in the same location. + +``` bash +$ sudo nano /etc/systemd/system/mnt-smb-nfsshare.automount +``` + +``` +[Unit] +Description=nfs automount for nfsfiles +Requires=network-online.target + +[Automount] +Where=/mnt/nfs/nfsshare +TimeoutIdleSec=0 + +[Install] +WantedBy=multi-user.target +``` + +\ +NFS mounts by nature are a bit more straightforward than samba. In an all-Linux environment they will lead to fewer headaches and I recommend them highly. + +### Put them to work! + +At this point your unit files are ready to go but systemd doesn't know about them. Run the following commands to test your mount: + +``` +$ sudo systemctl daemon-reload +$ sudo systemctl start mnt-nfs-nfsshare.mount +$ sudo systemctl start mnt-smb-smbshare.mount +``` + +\ +If all went well, you should see your file shares at their designated mount points. To verify, check the status of your service and look for any errors. + +``` +$ sudo systemctl status mnt-nfs-nfsshare.mount +● mnt-smb-sambashare.mount - samba mount for sambafiles + Loaded: loaded (/etc/systemd/system/mnt-smb-sambashare.mount; static; vendor preset: enabled) + Active: active (mounted) since Wed 2020-10-07 18:05:34 EDT; 1min 1s ago + Where: /mnt/smb/sambashare + What: //example.server/sambafiles + Process: 13005 ExecMount=/bin/mount //example.server/sambashare /mnt/smb/sambashare -t cifs -o vers=2.1,credentials=/etc/samba/smbcreds,iocharset=utf8,rw,x-systemd.automount,uid=1000 (code=exited, status=0/SUCCESS) + Tasks: 0 (limit: 4915) + CGroup: /system.slice/mnt-smb-sambashare.mount + +Oct 07 18:05:34 raylyon-ThinkPad-T450s systemd[1]: Mounting samba mount for sambafiles... +Oct 07 18:05:34 raylyon-ThinkPad-T450s systemd[1]: Mounted samba mount for sambafiles. +``` + +\ +Next, enable your automount files to start at boot. This will allow your shares to mount on-demand. + +``` +$ sudo systemctl enable mnt-nfs-nfsshare.automount +$ sudo systemctl enable mnt-smb-smbshare.automount +``` + +\ +That's it! To test, reboot your system, open the mountpoint in terminal or the file manager, and your share will mount before your eyes. If you have any questions or critiques on the above instructions, please shoot me an [email](mailto:ray@raylyon.net) or open a [Github issue](https://github.com/skoobasteeve/skoobasteeve.github.io.2/issues). + +Thanks for reading and happy hacking! \ No newline at end of file diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..0bb4fe4ec34b3556e7ad65ad5e67e4f8ff61bdd0 GIT binary patch literal 15406 zcmeHOe@sQiea0Btb>8?AI)E zX-&&0Sg?uYucFa1Y+;4YH65wTITX<#e`2l8)|U0{`+d**eBK}L-uHM9a651Na^5}X zeed`4{hrS`_uO;7aNHf-o!sQf9F{R$)mL!S>n^bq096!emmB~$l+E~vT{+0Dt;fWNQaZn z9z!|aQ~>2U4wb&#k25n<;hCpOgGzdG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weG z8XgFH4R`MJFW$37TAwK;a9w-i=uaU3|C@{h`O<~C5BF2<1AUyl#e(O+BcY7ttTYK~ zmM-+3^V3;>G=$FHDZfa@O`O5FcZ%`x5BQM#{(AE_1nX+b?T~*v(MpOf#a*<<-ICuV s`$0Y=@m9A>uy$ZR!R1iLbhP{>+!)uhl3=Ok`G@Y6@v@RdAW{+dA698Y<^TWy literal 0 HcmV?d00001 From e7324ee2557e4e845dfc5a6d4d9aac4f7937b105 Mon Sep 17 00:00:00 2001 From: Ray Lyon Date: Wed, 7 Oct 2020 19:02:24 -0400 Subject: [PATCH 3/3] publish date updated --- ...-03-systemd-automount.md => 2020-10-07-systemd-automount.md} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename _posts/{2020-10-03-systemd-automount.md => 2020-10-07-systemd-automount.md} (99%) diff --git a/_posts/2020-10-03-systemd-automount.md b/_posts/2020-10-07-systemd-automount.md similarity index 99% rename from _posts/2020-10-03-systemd-automount.md rename to _posts/2020-10-07-systemd-automount.md index 42bb0c7..6a5076e 100644 --- a/_posts/2020-10-03-systemd-automount.md +++ b/_posts/2020-10-07-systemd-automount.md @@ -1,7 +1,7 @@ --- layout: single title: "Painless On-Demand NAS Connections in Linux with Systemd Automount" -date: 2020-10-02 22:45:00 +date: 2020-10-07 19:00:00 categories: [Linux Administration] tags: linux samba nas systemd ubuntu comments: true