<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: oswllt</title>
    <description>The latest articles on DEV Community by oswllt (@oswllt).</description>
    <link>https://dev.to/oswllt</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1051224%2F50f10743-c942-4af1-bb18-e9d5fa72ab05.png</url>
      <title>DEV Community: oswllt</title>
      <link>https://dev.to/oswllt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oswllt"/>
    <language>en</language>
    <item>
      <title>Proxmox: Bind SMB Mount To Unprivileged LXC Container - The Easy Way</title>
      <dc:creator>oswllt</dc:creator>
      <pubDate>Tue, 10 Oct 2023 21:34:20 +0000</pubDate>
      <link>https://dev.to/oswllt/proxmox-bind-smb-mount-to-unprivileged-lxc-container-the-easy-way-3l4k</link>
      <guid>https://dev.to/oswllt/proxmox-bind-smb-mount-to-unprivileged-lxc-container-the-easy-way-3l4k</guid>
      <description>&lt;p&gt;Once you start running your own LXC containers inside a Proxmox, you might encounter a use case when you need a writable SMB/CIFS share mounted inside your unprivileged container.&lt;/p&gt;

&lt;p&gt;Example use-cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Plex server reading data from NAS&lt;/li&gt;
&lt;li&gt;Photoprism reading multimedia files from NAS&lt;/li&gt;
&lt;li&gt;and many more&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are two possible ways of binding the shares:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Secured way via &lt;code&gt;/etc/fstab&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;The Unsecured Way - A Privileged LXC Container&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The Secured way via &lt;code&gt;/etc/fstab&lt;/code&gt; file
&lt;/h2&gt;

&lt;p&gt;If you struggle with the &lt;code&gt;nobody&lt;/code&gt; file/dir owner/group in the container, then you come to the right place.&lt;/p&gt;

&lt;p&gt;This approach does not use the Proxmox dialog for adding SMB/CIFS volumes. The reason is that there is a lack of advanced control when setting the &lt;code&gt;uid&lt;/code&gt; and &lt;code&gt;gid&lt;/code&gt; for the mounted directory.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;/etc/fstab&lt;/code&gt; provides greater control when mounting the SMB/CIFS shares.&lt;/p&gt;

&lt;p&gt;The security benefit here is that the LXC container's &lt;code&gt;root&lt;/code&gt; user is not mapped directly to the host machine's &lt;code&gt;root&lt;/code&gt; user. Therefore the attack vector of escaping the Docker container should not do much harm to the host machine.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Securely store the SMB/CIFS credentials

&lt;ul&gt;
&lt;li&gt;create a credentials file
```bash
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;$ nano ~/.smbcredentials&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  - define username and password
  ```bash


username=me
password=secret


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;set R/W access for the owner only
```bash
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;$ chmod 700 ~/.smbcredentials&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
2\. Edit the `/etc/fstab` with the SMB/CIFS share mounted with the `uid` and `gid` of the container's `root` user, which is by default `100000`/`100000`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;//x.x.x.x/media /mnt/nas/media cifs credentials=/root/.smbcredentials,uid=100000,gid=100000 0 0&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  - the part `uid=100000,gid=100000` is essential for proper permission mapping to the containers' `root` user

3\. Bind the new mount into the LXC container
  - via command
```bash


$ pct set &amp;lt;container_id&amp;gt; -mp0 /mnt/nas/media,mp=/mnt/nas/media


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;or manually add the following line to the file &lt;code&gt;etc/pve/lxc/&amp;lt;container_id&amp;gt;.conf&lt;/code&gt;
```
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;mp0: /mnt/nas/media,mp=/mnt/nas/media&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
Now the mount should be mounted successfully with the same `uid` and `gid` as in the host machine.


## The Unsecured Way - A Privileged LXC Container

&amp;gt; **Attention!**
&amp;gt;
&amp;gt; This approach has one big disadvantage. It reduces the security of your host machine.
&amp;gt; 
&amp;gt; If your LXC container is under attack which escapes the Docker container, then the attacker has root access to your host machine.

1. Simply uncheck the "Unprivileged container" checkbox when creating a new LXC container
![Create a privileged LXC container](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xp2t7fcsl34kqyv5jsfp.png)
  - The privileged container `root` user has `uid` `0` inside the LXC container and he is mapped to the `root` user with `uid` `0` on the host machine.

1. Create an SMB share inside the Proxmox console
![Create an SMB share as disabled to bypass an error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1q084xs1c9vghhdhx2h4.png)
  - You can encounter `create storage failed: storage 'nas2' is not online (500)` error. 
  - In that case, create the share as disabled (uncheck the "Enable" checkbox). 
  - You can enable it once the share is created via the Edit dialog.

1. Bind the new mount into the LXC container
  - via command
```bash


$ pct set &amp;lt;container_id&amp;gt; -mp0 /mnt/pve/nas,mp=/mnt/nas


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;or manually add the following line to the file &lt;code&gt;etc/pve/lxc/&amp;lt;container_id&amp;gt;.conf&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  mp0: /mnt/pve/nas,mp=/mnt/nas


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now the mount should be mounted successfully with the same &lt;code&gt;uid&lt;/code&gt; and &lt;code&gt;gid&lt;/code&gt; as in the host machine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;There are several ways of achieving the goal. Using a privileged container is a not the safest approach. Using the &lt;code&gt;/etc/fstab&lt;/code&gt; method is much safer while providing greater control on the mount than what the Proxmox console provides.&lt;/p&gt;

&lt;p&gt;There is one more alternative - custom user &lt;code&gt;uid&lt;/code&gt;/&lt;code&gt;gid&lt;/code&gt; mapping. This approach maps container users and the host machine users with different &lt;code&gt;uid&lt;/code&gt;/&lt;code&gt;gid&lt;/code&gt;. However, the configuration is quite tricky to get right. More can be found in the official guide &lt;a href="https://pve.proxmox.com/wiki/Unprivileged_LXC_containers" rel="noopener noreferrer"&gt;Unprivileged LXC Containers&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>proxmox</category>
      <category>containers</category>
      <category>smb</category>
      <category>plex</category>
    </item>
  </channel>
</rss>
