Find the answer to your Linux question:
Results 1 to 6 of 6
I am running a shell script as the user "redhatuser01" and this script creates a files in the home directory of another user "redhatuser02" (/home/redhatuser02/sample.txt) but the ownership of this ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    8

    Changing Ownership of a File


    I am running a shell script as the user "redhatuser01" and this script creates a files in the home directory of another user "redhatuser02" (/home/redhatuser02/sample.txt) but the ownership of this file is currently "redhatuser01". How can i change the "ownership of this file to the user "redhatuser02"? (My constraint is that I cannot sudo as redhatuser2 and create the file).

  2. #2
    Administrator MikeTbob's Avatar
    Join Date
    Apr 2006
    Location
    Texas
    Posts
    7,864
    By using the chown, chgrp commands?
    Issue these commands for more help, in a terminal window.
    Code:
    man chown
    Code:
    man chgrp
    I do not respond to private messages asking for Linux help, Please keep it on the forums only.
    All new users please read this.** Forum FAQS. ** Adopt an unanswered post.

    I'd rather be lost at the lake than found at home.

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    The chown utility allows you to change the owner of a file. Run the command "man chown" for details.

    Note that as a general rule, you shouldn't be creating files in another user's home directory. It is fairly common for a user's home directory to be unwritable for other users in any case. You should treat a user's home directory as their own private domain.

  4. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,758

    Lightbulb

    Quote Originally Posted by Cabhan View Post
    Note that as a general rule, you shouldn't be creating files in another user's home directory. It is fairly common for a user's home directory to be unwritable for other users in any case. You should treat a user's home directory as their own private domain.
    Yes,I agree . If you want to access other users /home then run the script with root permission.

    BTW, Does it allow you create file under /home/user2 as user1? It should give you
    Permission denied
    message
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  5. #5
    Just Joined!
    Join Date
    Jul 2010
    Posts
    8
    Thanks for your suggestion.

    I had tried using chown to change the permission but got "Operation not permitted " error. Infact "redhatuser02" user's secondary group is "redhatuser01" and the file created there has got execute permission set to it. Am i missing here anything? or is it really possible ?

    (I understand that its not a good practice to create a file under some other user's home directory and th
    is is not part of a production code and i am trying to test a concept.)

  6. #6
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    Please read these:

    Code:
    man chown
    man chgrp
    man chmod
    man usermod
    man umask
    and google this: "unix filesystem standard" (unix filesystem standard - Google Search)

    you should under no circumstances allow one user to write to another users home directory. a common place where to put variable data that can be shared is /var.

    i.e. create a group "sharegroup", add both users to that "sharegroup" using usermod, create a directory under var with read/write permissions for the group "sharegroup" (g+rw) and let both users access those files. you could even make up a symbolic link to those files in their home directory or make a hardlink if the files are shared through ftp which do not follow symbolic links. note: you would also have to change the "umask" of both users in order to create files with the g+rw permissions by default. otherwise you would need a cronjob to do that job for you OR make sure the files that are being written are "chmod"-ed to g+rw after creation (if you have a script that is being run, it is just one more line).

    if you need more fancy things google this:

    ACL (access control list)

    this little thing allows you to set access permissions on a file/directory for multiple users/groups. it was invented cause certain situations like this here are quite complicated to handle only with the categories user, group and other.

    this said, you would be able to do everything without the need of a "sharegroup", but with the drawback that things are no longer obvious and easy understandable for others that ask themselves "why is this user writing to that folder and how does it work?".
    Last edited by Kloschüssel; 08-04-2010 at 06:26 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •