19

If a User A owns file.txt, can User A change the ownership of the file to User B without root access? When i run a chown B file.txt as user A, I get a Operation not permitted error. It seems to me that since User A owns the file, they ought to be able to change the ownership, but I don't see a way to do it. Thanks for the help!

7

If the User A owns file.txt, he cannot change the ownership of the file.txt without root access/sudo permission. This is a feature and not a bug. And one of the many reasons why the elders chose to put this feature in, has been explained in a comment to your question by roadmr

Bottom-line: You can change the permissions of the file using chmod if you are the owner of that file without root/sudo permissions but you cannot change the ownership, either user or group (using either chown or chgrp ), of a file even though you are the owner of the file without root/sudo permissions. This is a feature and not a bug.

  • 2
    Part of this wrong. Non-root users can change the group other groups they are a member of. Try touch t; ls -l t; chgrp lpadmin t; ls -l t (Assuming you are in the lpadmin group). See also my answer and linked question. – Mark Stosberg Jun 1 '16 at 17:47
  • Elders got it wrong. If im user A and im also user B, then i should be able to change ownership from A to B, if im logged in as A and i also have the password of B or vice versa. – aishu May 21 '18 at 4:32
  • @aishu You can in principle do that by copying the file as B then deleting it as A. Not very practical for big files though. – Holger Böhnke Dec 13 '18 at 17:05
4

No, you cannot change the owner a file without access, but if you own the file, you can change the permissions of the file with chmod and may change the group with chgrp to another group you a member of.

Related Question: chown is allowed to non root user?

2

if you have access to user B, you could just copy the file while logged in as B. If you also have access to user A, you could then log in and delete the original file. And finally rename the copied file, to the original name (again as B), leaving you with essentially the same file, owned by a different user.

Obviously is not the SAME file, but if you only cared about the contents of the file, this does the trick

1

You can change the ownership of a file or folder without sudo, so long as you have read/write permissions on the thing, and you can only change the owner to YOU, not to an arbitrary user. The trick is to simply copy whatever it is, delete the original then move your copy into its place. This, unfortunately, involves making a full-on copy of everything, but what do you do.

e.g. chuser.sh:

#!/bin/bash

TMP="some_temporary_filename_this_is_dumb"

RECURSIVE=""
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
    -r|--recursive)
    RECURSIVE="-r"
    shift # past argument
    ;;
    *)    # unknown option
    POSITIONAL+=("$1") # save it in an array for later
    shift # past argument
    ;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters

cp -d --preserve=all $RECURSIVE $1 $TMP || exit 1
rm $RECURSIVE $1
mv $TMP $1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.