LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Changing ownership after mounting ext4 filesystem at boot time (https://www.linuxquestions.org/questions/linux-newbie-8/changing-ownership-after-mounting-ext4-filesystem-at-boot-time-4175578118/)

fanoflq 04-22-2016 01:45 PM

Changing ownership after mounting ext4 filesystem at boot time
 
Changing ownership after mounting filesystem at boot time.

I have the following

Code:

/dev/sda5
mkfs.ext4 /dev/sda5

In /etc/fstab, I have this line:

Code:

/dev/sda5 /mnt/tempdir ext4 defaults,noexec 0 0

If I do a remount or a reboot, /mnt/tempdir is owned by root:
Code:

sudo mount -o remount /mnt/tempdir
How do I change its ownership to user at boot time without prompting user?

Obviously this is possible since each user's directory is owned by the user at boot time.


NOTEs:
For filesystem type ext4, adding uid=<user_id> to fstab is not allowed.
Code:

/dev/sda5 /mnt/tempdir ext4 defaults,noexec,uid=1002  0 0
I tried adding chown in .bashrc, but did not work due to need for sudo prefix.

joe_2000 04-22-2016 05:07 PM

I am not sure what you are trying to achieve here but this statement of yours:
Quote:

Originally Posted by fanoflq (Post 5535123)
Obviously this is possible since each user's directory is owned by the user at boot time.

leads me to believe you are drawing conclusions based on false assumptions. The fact that each user's directory is owned by the user is not something that is being set up at boot time. It's being set that way at install time, or more precisely, at the time the user and his home directory is created. The ownership is stored in the filesystem in a persistent way that "survives" reboots.

In other words, my guess is that your problem might already be addressed by a simple
Code:

chown -R your_user:your_user /mnt/tempdir
(as root) right after you ran mkfs.ext4 and mounted the device.

I might be missing something here, though.

On a separate note, your other issue (running commands at startup with root privileges) can be solved in several ways:

- You can put a @reboot entry in to root's crontab
- You can add hooks to your init process (the way how to do this depends on your init)
- You can put the stuff into a script and make it sudo-executable with NOPASSWD permissions in the sudoers file. Make sure it is writable only by root for security reasons and then run it prefixed with sudo in your autostart config for your x session...

There are probably many pther ways. The .bashrc approach you suggested has the downside of being run every time you open a shell - seems like a bit of an overkill.

But again, for your specific issue I don't think any of this is needed. Just set the right permissions/ownership once and the result should be persistent across reboots.

Shadow_7 04-22-2016 09:15 PM

You can add noauto so it doesn't mount automagically and therefor as root at boot. Or uid as suggested above. Also umask if you want everyone to have all permissions. But in general, the user who mounts the drive has the permissions for the drive. Outside of the actual permissions of the contents of the drive. I tend to create a directory under the / of the device with the users permissions. That way it doesn't matter so much that root mounted it. But that's probably not the preferred way with multiple machines, multiple users, enterprise environments and the likes.

fanoflq 04-22-2016 09:28 PM

Quote:

But that's probably not the preferred way with multiple machines, multiple users, enterprise environments and the likes.
How would this be done?
I am experimenting on a CentOS7 virtual machine.

fanoflq 04-22-2016 09:39 PM

Quote:

- You can put a @reboot entry in to root's crontab
Why do you want a reboot command in crontab?


Quote:

- You can add hooks to your init process (the way how to do this depends on your init)
I do not know how yet.
Am using CentOS7 virtual machine for learning.
Systemd init process is new to me...
I suspect there should be a config or script file that is called after all the filesystems from /etc/fstab for the user account has been mounted....


Quote:

- You can put the stuff into a script and make it sudo-executable with NOPASSWD permissions in the sudoers file. Make sure it is writable only by root for security reasons and then run it prefixed with sudo in your autostart config for your x session.
How to you specify NOPASSWD for a specific directory?
even if you can specify NOPASSWD, do you still have to use the sudo prefix in command line?

Shadow_7 04-23-2016 02:51 PM

Quote:

Originally Posted by fanoflq (Post 5535323)
How would this be done?
I am experimenting on a CentOS7 virtual machine.

# mount /dev/sda3 /mnt/partition3
# mkdir /mnt/partition3/someuser
# chown someuser:someuser /mnt/partition3/someuser
# umount /dev/sda3

And next time it's mounted, that directory with that users permissions (someuser in this case) has permissions to it's self owned directory. Granted this does add an extra name on the path since that user does not have access to the root directory of the mounted filesystem. Baring filesystems that have no permissions anyway. Also note that the permissions is based on user id (1000+), so if you move between machines and the same user is there but does NOT have the same id, then that's an issue. Which is where it starts to fail in business circles.

joe_2000 04-23-2016 04:32 PM

Quote:

Originally Posted by fanoflq (Post 5535325)
Why do you want a reboot command in crontab?

It's not a reboot command. It's a command that runs at boot. The @reboot keyword is a special case in crontabs which replaces the time configuration and just makes the command run at boot.

Quote:

Originally Posted by fanoflq (Post 5535325)
I do not know how yet.
Am using CentOS7 virtual machine for learning.
Systemd init process is new to me...
I suspect there should be a config or script file that is called after all the filesystems from /etc/fstab for the user account has been mounted....

I'm not good a systemd (try to avoid it as much as I can) but you'll probably have to create a service file and somehow declare the dependency of mounted file systems.
I'd have to look this up myself for systemd, but it certainly can be done.


Quote:

Originally Posted by fanoflq (Post 5535325)
How to you specify NOPASSWD for a specific directory?
even if you can specify NOPASSWD, do you still have to use the sudo prefix in command line?

Run
Code:

visudo
as root.
Then add an entry
Code:

ALL ALL = NOPASSWD: /path/to/your/script
Next time you run
Code:

sudo /path/to/your/script
it's not going to ask you for a password. This has nothing to do with a directory, the logic to which directory or whatever this applies has to be in your script. Make sure the script is only writable by root! It can be executed with root privileges without password, so you don't want anybody but root to be able to modify it.

But as said before, for your specific problem none of this should be needed.


All times are GMT -5. The time now is 04:47 AM.