简体   繁体   中英

Linux run bash script as root but no permission to chmod inside script

I have a bash script that I run like:

sudo ./AddNewUser.sh user7 qwerty

Inside the script there is (amongst many other lines)

chown $1 $homeBaseDir/$1/upload

I just keep getting back an error: chown: changing ownership of '/media/pi/CORSAIR/user7/upload': Operation not permitted

Why isn't it permitted if the script is running as root? The directory mentioned is there and the user exists.

/media/pi/CORSAIR/user7/upload seems like a mounted directory

Use the 'mount' command in order to see if you've mounted this directory with Read-Write permissions.

Alternatively you can use 'lsattr' to check if there are any file-system attributes that deny you permissions for some reason.

a} Check the permissions of everything involved.

If you're trying to do something on a mounted drive, mount and its' parameters are definitely the first thing I would check, but have a good look around with "ls -l" with the relevant files and note the security settings.

b} I'm actually on the less paranoid side of things where security is concerned, but the idea of automating things as root still really makes me uncomfortable.

In my experience, the reason to avoid using root isn't because I'm worried about getting broken into remotely by some bored 14 year old from Vladivostok. It's because I know how easy it is to write "rm -rf /usr" when I'm tired or distracted, because I've already done it.

The ideal way to use sudo, isn't just to do everything as root; but instead to create different users (an "adduser" account in this case) with the specific permissions you need for a given task, and then use sudo to switch to them.

UNIX was mocked by pre-iOS Apple users for years about how the security model was a blunt instrument. With sudo, however, we can create a more delicate and subtle setup than any other operating system you can think of; it's just that you have to do it yourself, and it takes work. It doesn't come out of the box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM