Skip to content

Instantly share code, notes, and snippets.

@pjobson
Last active April 21, 2024 21:37
Show Gist options
  • Star 56 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save pjobson/3811b73740a3a09597511c18be845a6c to your computer and use it in GitHub Desktop.
Save pjobson/3811b73740a3a09597511c18be845a6c to your computer and use it in GitHub Desktop.
Plex Media Permissions for Linux Noobies

Plex Media Permissions for Linux Noobies

There is no problem with being a noobie and I do not use the term to sligtht or disparage anyone.

This is a way to setup your permissions for running Plex in Linux. Different folks may use different methods.

The permissions concepts provided here apply to OSX, but the users and groups are controlled and modified differently, so much of this will not work properly. I think the command is dscl, but that could be out of date.

There are many ways to setup your permissions scheme in Linux, this methodology describes a way to do it, not everyone will like it, but it works for me, so whatever.

This is meant to be a super quick guide, please do some research on your own to help you with these terms, you're also welcome to reply with fix requests and/or suggestions.

DON'T!

  • Set your user and group for your media files to root.
  • Set your permissions to 777.

Permissions and Ownership Commands

  1. chmod - change file modes or Access Control Lists
  2. chown - change file owner and group
  3. chgrp - change group ownership

If you want more information on these or most commands in Linux, use man:

man chmod
man chown
man chgrp

Permissions & Access Control Brief

This is a super simplified description of Linux user and permission sets, I implore you to read up on this subject yourself.

After installing Plex, it will create a plex user and a plex group for itself. This user and group will need to have at least read access to your media files for them to show up in your library, you can give it write access if you want to be able to delete stuff from the GUI.

There are several types of access controls we're going to be using with Plex.

User

Basically a user is either a login account or a system account. User accounts can login to the computer, system accounts do stuff on the computer but generally can't login.

To view your user do:

whoami

To view all users do:

cat /etc/passwd

Group

Basically a group is a set of one or more users, groups can allow multiple users to have permissions to a file or directory.

To view your groups do:

groups

To view all groups do:

cat /etc/group

World / Everyone / Other

The world or everyone may have certain permissions to certain files, you rarely should give this level write permissions to anything.

Permissions

Permissions are the levels of access users, groups, or world has, they are broken up into three sets:

  • r - Read - The ability to read a file or a directory.
  • w - Write - The ability to create or delete a file in a directory or write to a file.
  • x - eXecute - The ability to execute a command in a file or directory.

You can see the permissions of something by doing:

ls -la

Here's an example from my server with the description of what the stuff is.

drwxr-xr-x    2 plex plex 20480 Sep 20 12:53 A
^^            ^ ^    ^    ^     ^            ^
||            | |    |    |     |            |
||            | |    |    |     |            +-> name of the path or file
||            | |    |    |     +--------------> modification date
||            | |    |    +--------------------> file size or size of the contents
||            | |    +-------------------------> owners group
||            | +------------------------------> owners name
||            +--------------------------------> number of links
|+---------------------------------------------> permissions of the path or file
+----------------------------------------------> directory / file / link / other

The permissions are setup in 3 groups:

rwxr-xr-x
^  ^  ^
|  |  |
|  |  +-> World Permissions - Read / eXecute
|  +----> Group Permissions - Read / eXecute
+-------> Owner Permissions - Read / Write / eXecute

So in the example above, we can tell the user is plex, the group is plex, and the permissions are rwxr-xr-x. This means:

  • User plex can read, write, or execute stuff in the directory.
  • Group plex can read or execute stuff in the directory.
  • World can read or execute stuff in the directory.

Plex Media Permissions

I keep all of my plex media in a path on my server, the path is a mount of a hardware RAID5. I don't recommend any particular file system or RAID setup, I do recommend using what you want to learn; for example my new system uses various drives in using btrfs.

My drives are mounted to the /dvr/mediastore path, mounting is outside of the scope of this document, you can search for How to mount a device in Linux? for more information.

I'll be referring to my personal directory structure from here on out, you will need to substitute your structure to get this stuff to work correctly.

In my setup I have Music, Movies, TV Shows as such:

/dvr/mediastore/Music
/dvr/mediastore/Movies
/dvr/mediastore/TV

The permission sets are:

  • /dvr - Owner: root, Group: root, Permissions: 755
  • /dvr/mediastore - Owner: plex, Group: plex, Permissions: 775

Everything in mediastore is owned by plex and has the group plex, all the files are set to xxx

Directory Structure

In movies I have a set of folders, my TV and Music are setup similarly.

/dvr/mediastore/Movies/#
/dvr/mediastore/Movies/A
/dvr/mediastore/Movies/B
/dvr/mediastore/Movies/C
### you can figure it out ###
/dvr/mediastore/Movies/X
/dvr/mediastore/Movies/Y
/dvr/mediastore/Movies/Z

In each folder I keep movies which have titles starting with the listed character, with the exception of movies which start with The which I use the first letter of the second word, thus The Matrix (1999) is stored in M. Movies which start with numbers are all stored in # as they are uncommon.

Set Up

Let's setup from scratch.

  • Download and Install Plex - https://www.plex.tv/media-server-downloads/

  • Add your user to the plex group, I'm guesssing your name is probably not pjobson.

      sudo usermod -a -G plex pjobson
    
  • Setup your drives and mount them if they're not.

  • Create some directories.

      sudo mkdir -p /dvr/mediastore/Movies/#
      sudo mkdir -p /dvr/mediastore/Movies/A
      sudo mkdir -p /dvr/mediastore/Movies/B
      sudo mkdir -p /dvr/mediastore/Movies/C
      sudo mkdir -p /dvr/mediastore/Movies/D
      sudo mkdir -p /dvr/mediastore/Movies/E
      # ...etc...
      # same convention for for TV & Music
    
  • Setup the OWNERSHIP of the paths.

      sudo chown -R plex.plex /dvr/mediastore/Movies
      sudo chown -R plex.plex /dvr/mediastore/TV
      sudo chown -R plex.plex /dvr/mediastore/Music
    
  • Setup the permissions of the paths.

      sudo find /dvr/mediastore/Movies -type d -exec chmod 775 {} \;
      sudo find /dvr/mediastore/TV -type d -exec chmod 775 {} \;
      sudo find /dvr/mediastore/Music -type d -exec chmod 775 {} \;
    
  • Go to the plex webgui http://localhost:32400 and/or http://127.0.0.1:32400.

  • Setup your Plex and tell it where the libraries are.

  • ???

  • PROFIT!

Seriously that is it. With this setup you can add media with either your plex user or your personal user account.

Maintence

When copying files and directories around be sure to keep your permissions up to date.

Directories should be 775 or rwxrwxrx- and files should be 664 or rw-rw-r--.

I have my process automated, but what it would look like if it were manual would be:

Copy the movie recursively to the correct path:

cp -r "Movie I Totally Own (1999)" /dvr/mediastore/Movies/M/

Change the permissions of the directory of the movie to rwxrwxrw-

chmod 775 "/dvr/mediastore/Movies/M/Movie I Totally Own (1999)"

Change the permissions of the files in the movie's path to rw-rw-r--

chmod 664 "/dvr/mediastore/Movies/M/Movie I Totally Own (1999)/*"

Change the ownership of the movie and all contents using recursively to plex and the group to plex.

sudo chown -R plex.plex "/dvr/mediastore/Movies/M/Movie I Totally Own (1999)"

I automate this process by creating a cron job which runs every fifteen minutes and checks the permissions and modifies them as needed.

@just-trey
Copy link

You may want to save yourself the chron job and look at this guide here https://forums.plex.tv/t/automating-linux-permissions-using-inheritance-helps-dvr/198011 you can enable inheritance using chmod g+s

@pjobson
Copy link
Author

pjobson commented May 13, 2021

Thanks @just-trey ! I'll read that later.

@EddieRayValentine
Copy link

Thanks so much for posting this. Very helpful to me. Followed the instructions to the T and it worked great.

@brennanjk
Copy link

I just installed a brand new Mint OS yesterday; the OS is installed on a 256 SSD, and I have a separate 4 TB hard drive I'd like to use to load up music and movies for a Plex server. I have setup the 4 TB drive; it's mounted and I can go into the drive and create folders.
I've setup my Plex server, and everything went fine until I tried to point my Libraries to this drive. I was hoping I had found the answer here when I found your post, but after applying all the suggested changes it still shows both my movies and music libraries as empty even though I've loaded up a handful of mp3s and movies into that drive.
I do notice when I go to 'Add Folders' I am able to select that root HDD drive, but it doesn't actually show me any of the folders within it, so I can only select the actual drive itself not a specific folder where the media is found. I manually typed in the full file path, but no luck with that either. Any help would be appreciated.

@pete-nice
Copy link

Here's my notes form last time I went through this. It was a nightmare to get it working. I ended up setting up a cron job to run every 10 minutes because even after I got it working the media in the folders wasn't showing up unless I ran the permission commands again. I really don't know much about linux permissions, so I won't be able to really give you any additional info if my notes below don't help you.

find /media/pete/USB8TB/Movies -type d -exec chmod 755 {} ;
find /media/pete/USB8TB/Movies -type f -exec chmod 644 {} ;

may 2021 notes:

sudo chown -R plex.plex /media/pete/USB8TB/Movies
sudo find /media/pete/USB8TB/Movies -type d -exec chmod 775 {} ;

sudo crontab -e (used to fix permissions on new files)

@pjobson
Copy link
Author

pjobson commented Feb 3, 2022

@brennanjk

That's weird. Can you input the path directly in? Go to the add folder thing and input the direct path there? I'm guessing it is a permissions issue, one directory above, do ls -la and verify that plex is the owner/group.

@pjobson
Copy link
Author

pjobson commented Feb 3, 2022

@brennanjk GREAT REPLY! I barely use the GUI in linux at all, it is interesting seeing that stuff for the first time.

I suspect it is due to the mounting of the drive, so we'll go down that path first.

To solve the unmounted HDD issue, you'll need to add it to your /etc/fstab.

First thing is you want to make a permanent mount path. I use /dvr because I was planning on setting up a dvr at some point, but got distracted and never changed it. You can use something like /mediadrive or really whatever you want, wherever you want it on your file system.

You'll need to edit the file with root so do sudo nano /etc/fstab. Where I have /your/mount/point make that the path you created above. Consider backing up your fstab first, just in case you mess it up cp /etc/fstab ~/fstab.backup.

UUID=cb1401bb-1709-4fe1-a856-a943ed8e2d71 /your/mount/point               ext4     defaults        0       0

I copied the UUID from the pic above, but you can double check it with sudo blkid | grep UUID, the device in the pic above is /dev/sda, so look for that.

Now you can test the fstab with: sudo mount -a. If this throws no errors, you're good to go.

You should be able to browse the media in whatever mount path you created. Reboot and it should come up automatically.

@brennanjk
Copy link

Thank you for the detailed instructions. I created a folder in the media path called 'mediadrive,' and I've mounted the drive there. I rebooted and it came up automatically now. Still not seeing the content just yet in Plex; would I need to add any additional permission for Plex to view that path at this stage?

@brennanjk
Copy link

Correction; I was able to get in and point the library's to the appropriate folders now. My music folder worked! For some reason the Movies folder is still coming up empty though... maybe the format of the videos isn't compatible with Plex or something?

@pjobson
Copy link
Author

pjobson commented Feb 4, 2022

@brennanjk

Plex supports multiple video formats, unless you've got something weird, it should play.

What are the permissions on the Movie folder?

@brennanjk
Copy link

Here's the screen shot; the Movies folder definitely ended up with slightly different permissions for some reason:

Screenshot 2022-02-04 084517

@pjobson
Copy link
Author

pjobson commented Feb 4, 2022

@brennanjk

Yeah, the permissions on that path are weird, from the path you are in /media/mediadrive/Plex Server do:

sudo find . -type d -exec chmod -v 775 {} \;
sudo find . -type f -exec chmod -v 664 {} \;
sudo chmod -v g+s Movies
sudo chmod -v g+s Music

This will change the permissions of all directories and files to reflect what I have at the top of the post. Then it'll force everything in there to keep the same group (inheritance).

@pjobson
Copy link
Author

pjobson commented Feb 4, 2022

@brennanjk

Your Plex Server directory should be 775 also.

sudo chmod 775 "/media/mediadrive/Plex Server"
sudo chmod g+s "/media/mediadrive/Plex Server"

@brennanjk
Copy link

Alright, I finally have both folders working properly now! I notice if I add a folder (which I'll be doing a lot of as I copy more content into that drive) I have to run one of the commands you sent me on the root folder to reapply those permissions, so maybe I can setup some kind of cron job to do this automatically like someone mentioned above...
Either way, I'm in a much better spot now than I was a few days ago. Thanks for your assistance, I really appreciate it!

@supmir
Copy link

supmir commented Jan 30, 2023

I just wanna add that instead of using cronjobs, you can add the file creator to the plex group.

@baghashams
Copy link

Thanks so much for this guide, it helped me get Plex working. However, now I can't do anything in my media folders from the GUI, such as create new folders, etc. Also, my Deluge can't write to the downloads folder, which is in the same group of folders.

How do I regain access to my media folder?

@pjobson
Copy link
Author

pjobson commented Feb 7, 2023

@baghashams

I unfortunately don't really know much about doing stuff from the GUI. You might have to logout/login to refresh your user's groups in the GUI, though I'm not 100% sure.

Per deluge, I think it has its own user and group, make sure the downloads path where it should download to has the correct permissions. You can cat your passwd and group files and find deluge's username, like:

cat /etc/passwd |grep deluge
cat /etc/group |grep deluge

Then look at the user/group/permissions of the deluge downloads directory:

ls -la /path/to/deluge/downloads

It should show something like:

drw-r--r--  1 deluge deluge  597 Nov 27  2021 downloads

Different date stamp of course. I put my downloads in an entirely different path. I don't use deluge anymore because the latest update F'd my permissions altogether. My pathing for deluge was:

/dvr/deluge/torrents/active
/dvr/deluge/torrents/seeding
/dvr/deluge/torrents/complete

The /dvr/deluge path and sub paths were all owned/grouped to the deluge user, then I put my regular login user into the deluge group, so I could copy stuff around.

@stevefxp1
Copy link

Stupid question but can this be applied to a NAS PMS, since the NAS is essentially a Linux box?

@pjobson
Copy link
Author

pjobson commented Mar 10, 2023

@stevefxp1

It isn't stupid, quite reasonable. You can setup whatever permissions you want on your NAS, if it uses a standard *nix permission set then I don't see why not. I haven't experimented with NAS stuff in a long time, so I don't have a good/complete answer for you.

@Clowser323
Copy link

Clowser323 commented May 3, 2023

Hi, I was following this guide and I find myself in a similar spot to where @brennanjk was.

I am trying to add files from a hard drive, I added it to the fstab and that doesn't cause any problems. The permissions look like this (keep in mind as of now I am only interested in adding the Audiobook library from this folder):
image

But when I go to plex and try to add that path to the library that is what it looks like:
image

And doesn't show any of the child directories. I also changed the ownership of the media directory to plex:plex after taking the screenshot.
Thanks in advance for any help!!

@Quoddity
Copy link

Quoddity commented Aug 9, 2023

You put a lot of work into explaining this, thanks for the effort!

My current system is a test case for when I switch a different PC to linux.

For me, Plex is able to access and play the files just fine, but I no longer have permission to see the folders/files.
My USER is part of the plex group.
I need to elevate my user to root (sudo) to view the files/folders.
How do I fix this?

@Quoddity
Copy link

Quoddity commented Aug 9, 2023

I am wondering how I would give certain programs/scripts to read and write to these folders?
The two I am currently wondering about are yt-dlp and syncthing.
For yt-dlp, I suppose i could run it with root in cron, but i'm a bit iffy on that.

@pjobson
Copy link
Author

pjobson commented Aug 10, 2023

@Quoddity

Your default user should have sudo privileges. You can check by doing something like sudo ls, this should prompt you for your password then run the ls command as root. If it gives you a permissions error, then you'll need to give your user higher privs, kind of outside of the scope of this document. Google: sudoers

I am wondering how I would give certain programs/scripts to read and write to these folders?

If your user is running the scripts, it'll just work with the permissions. I have syncthing setup as its own user, but if you have it running as your user it'll have access to stuff within your permission set.

@redkurn
Copy link

redkurn commented Sep 12, 2023

thanks for this, been struggling to get my plex server permissions to be correct since i set it up.

anyway you could share how to set up a cron job?
i've never used cron before, didn't really need it for a long time.

@pjobson
Copy link
Author

pjobson commented Sep 12, 2023

@redkurn

Any tutorial will explain cron jobs better than I can. Generally I use this tool to generate the proper crontab line, there may even be a bit of tutorial there... https://crontab-generator.org/

@redkurn
Copy link

redkurn commented Sep 12, 2023 via email

@pjobson
Copy link
Author

pjobson commented Sep 13, 2023

@redkurn what permissions does your script have? If you run it without using cron, does it run properly or fail?

@redkurn
Copy link

redkurn commented Sep 14, 2023

@pjobson
-rwxr-xr-x 1 plex plex 737 Sep 12 13:15 setperm.sh

I set it to run every minute and nothing changed, running manually i see

x@debian:/mnt/Media$ ./setperm.sh
chown: changing ownership of '/mnt/Media/test': Operation not permitted
chown: changing ownership of '/mnt/Media/test/test.txt': Operation not permitted

owner of the folder is plex, file is me.

@pjobson
Copy link
Author

pjobson commented Sep 14, 2023

Oh you need to run it as root, because you need elevated privs to modify all of the permissions. So do...

sudo su -
crontab -e

Then add the full path to the script wherever you have it.

@redkurn
Copy link

redkurn commented Sep 17, 2023

@pjobson your script up at the top works perfectly, wish i'd noticed git hiding it sooner, could have saved myself 6 hours of frustration.

i've noticed running crontab on debian as */30 **** /path does not work, it has to be an asterisk for the minute. not sure why.
added another job and added sleep 30 per a guide i found and that worked so my cron looks like

* * * * * /path/to/script
* * * * * sleep 30; /path/to/script

and it works, i'm sure i could have it on the same line, but easier to read like this.

thanks for the help, the script and the guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment