Calibrating permissions

For interesting topics. But remember this is a Linux Forum. Do not post offensive topics that are meant to cause trouble with other members or are derogatory towards people of different genders, race, color, minors (this includes nudity and sex), politics or religion. Let's try to keep peace among the community and for visitors.

No spam on this or any other forums please! If you post advertisements on these forums, your account may be deleted.

Do not copy and paste entire or even up to half of someone else's words or articles into posts. Post only a few sentences or a paragraph and make sure to include a link back to original words or article. Otherwise it's copyright infringement.

You can talk about other distros here, but no MX bashing. You can email the developers of MX if you just want to say you dislike or hate MX.
Message
Author
User avatar
DukeComposed
Posts: 1451
Joined: Thu Mar 16, 2023 1:57 pm

Re: Calibrating permissions

#11 Post by DukeComposed »

Jakob77 wrote: Wed Sep 20, 2023 4:08 pm Thank you very much.
I have to look more into the code before I ask more about it.
If I mess things up and create new issues when I with Thunar remove permission to run as a program on all files in a folder, then I am afraid I have already messed up before I asked here.
"chmod 0644 filename" will change the permissions of filename to readable. This is normally the "right" permissions for a regular file to have. There are exceptions.

"chmod 0755 dirname" will change the permissions of dirname to readable + executable. This is normally the "right" permissions for a regular directory to have. There are exceptions.

"find dirname -type f" will return a list of all files in and beneath dirname.

"find dirname -type d" will return a list of all directories in and beneath dirname.

This should be enough knowledge to get you started on figuring out how to find and change permissions on almost anything.

Extra credit: find supports an "-exec" argument that can used to run a command on the files/directories it returns. It also supports a "-perm" argument I mentioned earlier, and "\!" can be used to negate the permissions check, i.e., "find dir -type d \! -perm 0755" should give a list of all directories and subdirectories that do not have mode 0755.
Jakob77 wrote: Wed Sep 20, 2023 4:08 pm Can the reason for the problem be that the external SSD solid USB drive has been formatted by Windows 10 ?
Absolutely correct. Windows filesystems, including FAT32, exFAT, and NTFS don't handle UNIX-style file modes in the same way and so using different operating systems to move files around can be troublesome. The "mtree" program can useful here, since it can check the contents of a file tree, write down the permissions of those files and directories, and save them to disk as a "spec file". It can also then repair those permissions later on if you provide the spec file.

Charlie Brown

Re: Calibrating permissions

#12 Post by Charlie Brown »

Jakob77 wrote: Wed Sep 20, 2023 4:08 pm... been formatted by Windows 10 ...
I wish you told that at the very beginning. The above are according to ext4 etc.

Jakob77
Posts: 658
Joined: Thu Feb 09, 2023 3:09 am

Re: Calibrating permissions

#13 Post by Jakob77 »

I don't hope it is a serious problem because it is growing.
I restored a whole computer with data from the external drive, and all the files from there has permission to run as programs.


It was my idea to just find a command or write a script that could fix it for all files and sub folders with files.
However, it might not be that easy to make if we can't separate files and folders, and before it is made it is important to know how well it can do the job.
To do that it is at least important to know what files we are dealing with. If I for instance run it in my ~/bin folder to fix files and sub folders there, then I will afterwards have to add permissions again to my scripts in bin.
The files in the bin folder I have put there myself so I know about them. I could be wrong but I see no problem in that.

If we look at the whole home folder in a MX full basic install the same way how many files are there with permission to run as programs. If it is only a couple we can maybe run a cure for whole home, and then afterwards switch the couple back in place again.
Yes, I walk like a cat around a hot plate and share some more thoughts.




DukeComposed

Thank you very much for more tips.
It will be a struggle to get my head around it but I am working on it.



Charlie Brown

Yes, but I had no idea that could be the problem.
I have never seen it before, and I have never thought much about it. I always thought I in general should be able to trust the system when a copy was done without errors.

Do you use gpt and ext4 on all drives with no exceptions then.?

It looks like fat32 is the default setting in the MX format tool, and I don't see any warnings in the menu or help.
I find it scary that it looks like it is taken for granted, that it is common knowledge.

Charlie Brown

Re: Calibrating permissions

#14 Post by Charlie Brown »

Jakob77 wrote: Thu Sep 21, 2023 7:26 am... Do you use gpt and ext4 on all drives with no exceptions then.?..
No, I just mean ext4 (and such Linux file systems) is better for Linux-related things, especially permissions/ownerships (as already mentioned above, Microsoft filesystems already won't keep/care them).. and , say ntfs just for sharing files bw. both systems (make easy to access from both OSs ) ...

User avatar
thomasl
Posts: 484
Joined: Sun Feb 04, 2018 9:26 am

Re: Calibrating permissions

#15 Post by thomasl »

This is a script (called chmodr) I've used for a long time to change permissions for whole trees (as you've seen copying big directory trees from NTFS or FAT volumes can produce "wrong" permissions):

Code: Select all

#!/bin/bash
sudo find ./ -type d | while read DIR ; do sudo chmod $1 "${DIR}" ; done
sudo find ./ -type f | while read DIR ; do sudo chmod $2 "${DIR}" ; done
Start in the directory which you want to change, give the permissions for dirs and files and it'll do its job recursively:

Code: Select all

chmodr 755 644
Nowadays I'm doing two things WRT this. First I mount all NTFS or FAT volumes with the "right" permissions to start with. Secondly, I replaced the find command with a utility called fd (https://github.com/sharkdp/fd for details) which is faster, easier to use and at least for certain tasks more powerful. YMMV.
Frugal installs on Lenovo ThinkPad L14 Ryzen 5 4650U/24GB * HP Pavilion Ryzen 3 3300U/16GB * Toshiba R950 i5-3340M/12GB
I have a reservation... What do you mean it's not in the COMPUTER!

Jakob77
Posts: 658
Joined: Thu Feb 09, 2023 3:09 am

Re: Calibrating permissions

#16 Post by Jakob77 »

A lot of things are scary today. I also have a USB backup disk I dare not look at. lol




I am very grateful for more code, I just don't understand it well enough to use it yet.



As a peasant in scripting I would like a little feedback on this simple idea for a script to calibrate a folder for instance ~/Music and its sub folders (all files are just music files) :





1) Somehow establish a copy of the whole tree with folders but without files.


2) Then use chmod with 0755 on all the folders in the tree with no files


3) And use chmod with 0644 on the other tree with all the files.


4) Afterwards copy/combine in a way so the folders in the empty tree overwrites the folders in the tree with all the files.





Could that be a simple and okay way to go.?

I don't have the code for 1 or 4 yet.

Jakob77
Posts: 658
Joined: Thu Feb 09, 2023 3:09 am

Re: Calibrating permissions

#17 Post by Jakob77 »

Charlie Brown wrote: Tue Sep 19, 2023 5:52 pm
Jakob77 wrote: Tue Sep 19, 2023 4:47 pm...
And it has some different sub folders ... Do you think this looks like a correct syntax ...

Code: Select all

sudo chmod -x -R ~/Music/*.*
Yes, but no :D

If we do it in bulk, then, those which do not already have x (executable) may become problematic, new issues may occur.
I have to ask problematic how, what issues.?
Could it perhaps help to make them all +x first and then -x all.?
To me it looks like a big advantage if it is possible to reduce the edit to only the one privilege we want to change.

User avatar
thomasl
Posts: 484
Joined: Sun Feb 04, 2018 9:26 am

Re: Calibrating permissions

#18 Post by thomasl »

Jakob77 wrote: Thu Sep 21, 2023 10:38 amA lot of things are scary today. I also have a USB backup disk I dare not look at. lol

I am very grateful for more code, I just don't understand it well enough to use it yet.
It's certainly better to be careful than to rush headlong into disaster :eek: However in the long run it'd pay if you tried to understand the basics as these things will pop up again and again.

For instance, the short (and not so hard to understand) script I showed above can be used for many similar purposes by simply changing the "chmod" part... but you'd have to have some idea what you're doing as recursive scripts and commands can be as powerful as they can be destructive... if misused.

So it always pays to double-check whether such a script (or command) works as intended, eg with one or more test runs on a set of dummy files/directories. Or you could prefix the actual command ("chmod ...") with an echo statement like this:

Code: Select all

#!/bin/bash
find ./ -type d | while read DIR ; do echo chmod $1 "${DIR}" ; done
find ./ -type f | while read DIR ; do echo chmod $2 "${DIR}" ; done
This will simply list the actions to be taken but not perform them.

Additionally, as long as you don't want to modify system directories the sudo bits are not necessary (as shown in the modified script).
Frugal installs on Lenovo ThinkPad L14 Ryzen 5 4650U/24GB * HP Pavilion Ryzen 3 3300U/16GB * Toshiba R950 i5-3340M/12GB
I have a reservation... What do you mean it's not in the COMPUTER!

User avatar
fehlix
Developer
Posts: 12760
Joined: Wed Apr 11, 2018 5:09 pm

Re: Calibrating permissions

#19 Post by fehlix »

Jakob77 wrote: Thu Sep 21, 2023 10:38 am A lot of things are scary today. I also have a USB backup disk I dare not look at. lol

I am very grateful for more code, I just don't understand it well enough to use it yet.

As a peasant in scripting I would like a little feedback on this simple idea for a script to calibrate a folder for instance ~/Music and its sub folders (all files are just music files) :

1) Somehow establish a copy of the whole tree with folders but without files.

2) Then use chmod with 0755 on all the folders in the tree with no files


3) And use chmod with 0644 on the other tree with all the files.


4) Afterwards copy/combine in a way so the folders in the empty tree overwrites the folders in the tree with all the files.

Could that be a simple and okay way to go.?

I don't have the code for 1 or 4 yet.
The above may solve your wrong file permission issue now for the files which have copied with wrong permissions.

But that's wrong strategy to get proper file permissions for files copied back from external NTFS/exFAT USB-DATA .
Instead set file permission during mount for the external Data-USB, which do have file systems types vfat[FAT32], exFAT or NTFS. Permissons for all files and directories will be set at once, when mounted e.g. with Thunar,
by placing this udisks2-conf file /etc/udisks2/mount_options.conf:

Code: Select all

# This file contains custom mount options for udisks 2.x
# placed at /etc/udisks2/mount_options.conf
# Refer to http://storaged.org/doc/udisks2-api/latest/mount_options.html
[defaults]
vfat_defaults=uid=$UID,gid=$GID,dmask=0002,fmask=0113,shortname=mixed,utf8=1,showexec,flush
vfat_allow=uid=$UID,gid=$GID,flush,utf8,shortname,umask,dmask,fmask,codepage,iocharset,usefree,showexec
# common options for both the native kernel driver and exfat-fuse
exfat_defaults=uid=$UID,gid=$GID,dmask=0002,fmask=0113,iocharset=utf8,errors=remount-ro
exfat_allow=uid=$UID,gid=$GID,dmask,errors,fmask,iocharset,namecase,umask
# ntfs3, ntfs-3g and the legacy ntfs kernel driver options
ntfs_defaults=uid=$UID,gid=$GID,dmask=0002,fmask=0113,windows_names
ntfs_allow=uid=$UID,gid=$GID,umask,dmask,fmask,locale,norecover,ignore_case,windows_names,compression,nocompression,big_writes,nls,nohidden,sys_immutable,sparse,showmeta,prealloc

Jakob77
Posts: 658
Joined: Thu Feb 09, 2023 3:09 am

Re: Calibrating permissions

#20 Post by Jakob77 »

thomasl

Thank you. I need to start my test computer soon.



fehlix

Thank you.
Yes, prevention is a much better strategy than cure.
I hope to be able to do the prevention just by choosing the best way to format:
viewtopic.php?t=77258
If more is needed I suppose you send it out in an automatic update.?

The script in quest is meant for repair when prevention has failed.

Locked

Return to “General”