LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-22-2016, 01:45 PM   #1
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Rep: Reputation: Disabled
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.

Last edited by fanoflq; 04-22-2016 at 02:05 PM.
 
Old 04-22-2016, 05:07 PM   #2
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
I am not sure what you are trying to achieve here but this statement of yours:
Quote:
Originally Posted by fanoflq View Post
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.

Last edited by joe_2000; 04-22-2016 at 05:09 PM.
 
Old 04-22-2016, 09:15 PM   #3
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
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.
 
Old 04-22-2016, 09:28 PM   #4
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-22-2016, 09:39 PM   #5
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
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?
 
Old 04-23-2016, 02:51 PM   #6
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
Quote:
Originally Posted by fanoflq View Post
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.
 
1 members found this post helpful.
Old 04-23-2016, 04:32 PM   #7
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by fanoflq View Post
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 View Post
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 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
VFS: Can't find ext4 filesystem / PC does not boot bartonlinux Linux - Newbie 2 06-15-2012 09:52 PM
boot error mount:unknown filesystem type 'ext4' vinvishwa Fedora 1 02-05-2010 03:10 PM
mounting an ext4 filesystem from ext3(or reiser): What do I need? svar Slackware 27 09-19-2009 02:19 PM
3 issues: Making swap, fixing partitions, changing ownership of mounted filesystem. Dunedain Linux - Newbie 2 01-27-2004 03:52 PM
Changing ownership of mounted filesystem jhansman Red Hat 4 09-11-2003 12:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration