Python Forum
[Errno 1] Operation not permitted
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Errno 1] Operation not permitted
#1
Hello, I'm trying to run the following program, but I'm getting an error. My OS is Linux Ubuntu 18.04 LTS and the IDE is IDLE.
#!/usr/bin/python3.8
import os, sys
# Open a file
path = "/home/user/Desktop/Programming/Python/var/www/html/foo.txt"
fd = os.open(path, os.O_RDWR|os.O_CREAT)
# Close opened file
os.close( fd )
# Now change the file ownership
# Set a file owner id
os.lchown( path, 500, -1)
# Set a file group ID
os.lchown( path, -1, 500)
print("Changed ownership successfully!!")

# "/home/user/Desktop/Programming/Python/var/www/html/foo.txt"
The error is:
Error:
Traceback (most recent call last): File "/home/user/Desktop/Programming/Python/Os lchown method.py", line 10, in <module> os.lchown( path, 500, -1) PermissionError: [Errno 1] Operation not permitted: '/home/user/Desktop/Programming/Python/var/www/html/foo.txt'
Reply
#2
Python is asking the OS to complete the lchown(), but the OS said the operation failed. Usually, this means you don't have permission to do what you're trying to do.

If you are not already uid 500, and you are not root, then I would expect the lchown() call to fail as you've shown.
Reply
#3
(Jun-02-2020, 08:49 PM)bowlofred Wrote: Python is asking the OS to complete the lchown(), but the OS said the operation failed. Usually, this means you don't have permission to do what you're trying to do.

If you are not already uid 500, and you are not root, then I would expect the lchown() call to fail as you've shown.
How can I get permission to run the program as root?
Reply
#4
Easiest is to invoke the program with sudo.

$ sudo python3 myscript.py
Reply
#5
(Jun-04-2020, 03:41 PM)bowlofred Wrote: Easiest is to invoke the program with sudo.

$ sudo python3 myscript.py

user@Lenovo-ideapad-110-17IKB:~$ sudo python3 /home/user/Desktop/Programming/Python/Os lchown method.py
[sudo] password for user: 
python3: can't open file '/home/user/Desktop/Programming/Python/Os': [Errno 2] No such file or directory
user@Lenovo-ideapad-110-17IKB:~$ sudo python3 /home/user/Desktop/Programming/Python/Os lchown method.py
python3: can't open file '/home/user/Desktop/Programming/Python/Os': [Errno 2] No such file or directory
user@Lenovo-ideapad-110-17IKB:~$ 
Reply
#6
There are spaces in the filename. So you should quote the path:
sudo python3 "/home/user/Desktop/Programming/Python/Os lchown method.py"
(One hint when on Linux: Never use spaces in a filename. (It complicates your programs.))
Reply
#7
Not sure how much this really helps your current problem but seeing you are using python 3.8 you might consider switching to the use of from pathlib import Path as it enables you to do cool things like for example

from pathlib import Path

our_file = Path("postgres.cast")
with our_file.open() as fd:
    print(our_file.owner())
    print(our_file.group())
    for line in fd:
        print(line)
and has the benefit of automatically closing the file, as well as other useful methods. In addition it is multiplatform, it understand paths from various operating systems. Not that this solves your current problem, just a pointer to something you might want to look into python shutil with path
Reply
#8
(Jun-05-2020, 02:33 PM)ibreeden Wrote: There are spaces in the filename. So you should quote the path:
sudo python3 "/home/user/Desktop/Programming/Python/Os lchown method.py"
(One hint when on Linux: Never use spaces in a filename. (It complicates your programs.))
user@Lenovo-ideapad-110-17IKB:~$ sudo python3 "/home/user/Desktop/Programming/Python/Os tcsetgrp method.py"
[sudo] password for user: 
Current working dir :/home/user
the process group associated is: 
20025
Traceback (most recent call last):
  File "/home/user/Desktop/Programming/Python/Os tcsetgrp method.py", line 12, in <module>
    os.tcsetpgrp(fd,2672)
PermissionError: [Errno 1] Operation not permitted
user@Lenovo-ideapad-110-17IKB:~$ su
Password: 
root@Lenovo-ideapad-110-17IKB:/home/user# sudo python3 "/home/user/Desktop/Programming/Python/Os tcsetgrp method.py"
Current working dir :/home/user
the process group associated is: 
20065
Traceback (most recent call last):
  File "/home/user/Desktop/Programming/Python/Os tcsetgrp method.py", line 12, in <module>
    os.tcsetpgrp(fd,2672)
PermissionError: [Errno 1] Operation not permitted
root@Lenovo-ideapad-110-17IKB:/home/user# exit
exit
user@Lenovo-ideapad-110-17IKB:~$ 
Reply
#9
Good! One step further, you can start your program as root.
But still you are not happy because the result is not what you want.
chown is an old Unix program to change the owner of a file you own. Only root was allowed to change all ownerships. Later it appeared smart guys could misuse this so the use of chown was blocked for normal users. Later more security rules were added like:
  1. File attributes (check with lsattr <file>)
  2. SELinux (check with ls -Z <file>)
SELinux is usually used to make Apache-httpd secure. As I see the path to your file is "/home/user/Desktop/Programming/Python/var/www/html/foo.txt" I guess it has something to do with Apache-httpd.

So do you encounter the same problem when you choose a file outside the "var/www" directory?
Of course there are ways around the problem by relaxing the security measures, but is that the best solution?

So consider: why do you want to change the ownership of the file? Is it necessary?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020