Is it possible to change ownership of a file without root access?

74,376

Solution 1

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: Without root/sudo permissions you can change the permissions of the file using chmod, and the group ownership (to any group which you are a member of, with chgrp), if you are the owner of that file, but you cannot change the user ownership (using chown), even though you are the owner of the file, without having root/sudo permissions. This is a feature and not a bug.

Solution 2

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?

Solution 3

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

Solution 4

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

Solution 5

There is a strong argument to changing the ownership, because in my business, we get files from different users all the time and if the permission is such that you cannot do anything to the file other that read it, then we run it issues.

So what I found out was that it is easy to run a 'compress <data_filename>' followed by an 'uncompress <data_filename.Z>' which will do the trick and the ownership of the original file automatically changes to the current owner.

Share:
74,376

Related videos on Youtube

Daniel
Author by

Daniel

Updated on September 18, 2022

Comments

  • Daniel
    Daniel almost 2 years

    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!

    • Admin
      Admin over 12 years
      You could put incriminating data in a file and change its ownership to frame someone :) I guess that's one argument against allowing what you suggest.
    • Admin
      Admin about 10 years
      Small addition: AFAIU you can change the owning group of a file that you own, provided that you're a member of the group you're changing to.
    • Admin
      Admin about 5 years
  • Mark Stosberg
    Mark Stosberg about 8 years
    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.
  • aish
    aish about 6 years
    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.
  • Holger Böhnke
    Holger Böhnke over 5 years
    @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.
  • ADJenks
    ADJenks almost 3 years
    I don't think this is a good idea. Particularly for larger files. You're wasting energy and time because compressing and decompressing the file is CPU intensive. As other users have said, you just need to copy the file, delete the old file, and optionally rename the copy back to the old name. Compressing and decompressing essentially does the same thing, but it performs two juggling acts in-between. If you want to do it in only two steps use the force flag when moving. Like so: cp thefile tempcopy && mv -f tempcopy thefile