Please visit the new Mac-Forums Facebook page:
Join Us @ Mac-Forums Facebook Page




Results 1 to 7 of 7
  1. #1
    Change ownership of home directory
    In a shell script (running as a launch daemon, thus as root), I'm trying to transfer ownership of a home directory from a local user to a network user.

    Stripped of all the logging and debugging code, here's what the script does:

    ----
    # $sourcedir contains the old directory name (from the old, local user)
    # $userdir contains the new directory name (from the new, network, user)
    # $tgtgu contains the user:group specifier for the network user (like 3953253:20)

    # Remove any conflicting old directory
    rm -R $userdir || true

    # Rename the old directory to the new name
    mv -f "$sourcedir" "$userdir"

    # Change the ownership
    chown -R $tgtgu \"$userdir\"
    ----

    The 'chown' statement claims that '$userdir' does not exist at that point. Part of the debugging code confirms that the directory does, in fact, exist.

    Does anyone know why I would not be able to change the owner of the newly-renamed directory, or why chown would report that it does not exist?

  2. #2
    Change ownership of home directory
    pm-r's Avatar
    Member Since
    Oct 16, 2010
    Location
    Brentwood Bay, BC, Canada
    Posts
    12,323
    Rep Power
    20
    Change ownership of home directory

    Just a thought and without knocking any programmers that might be here, but I think you might get a better response at one of the Mac programmers sites or forums.




    - Patrick
    ======

  3. #3
    You may be right; my impression was that most of those were more concerned with Obj-C and Swift than shell scripting. I'll give it a try.

  4. #4
    Change ownership of home directory
    chscag's Avatar
    Member Since
    Jan 23, 2008
    Location
    Keller, Texas
    Posts
    58,919
    Your Mac's Specs
    2017 27" iMac, 10.5" iPad Pro, iPhone 7+, iPhone 8, Numerous iPods, Mojave
    Rep Power
    53
    Not helpful Patrick. We have several folks who are members and know programming and lets not forget one of our Staff Moderators is professional programmer.

  5. #5
    Change ownership of home directory
    pm-r's Avatar
    Member Since
    Oct 16, 2010
    Location
    Brentwood Bay, BC, Canada
    Posts
    12,323
    Rep Power
    20
    Quote Originally Posted by chscag View Post
    Not helpful Patrick. We have several folks who are members and know programming and lets not forget one of our Staff Moderators is professional programmer.

    Well Charles, I'm sure the OP will gladly welcome any useful programmer's comments or input if and when they might reply.

    Personally, I wasn't even sure what a 'shell script" was until I checked:
    https://developer.apple.com/library/...roduction.html

    And it seems to be a bit more than any HyperCard language I dare say, which was about the last time I did any "programming".




    - Patrick
    ======

  6. #6
    Change ownership of home directory
    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    12,122
    Rep Power
    24
    Assuming this is a BASH shell script, do you have appropriate checks around the various functions? That is, I would do the chown in the following way
    Code:
    if [ -e $userdir ]; then
        chown -R $tgtgu $userdir
    else
        echo "$userdir doesn't exist"
    fi
    This should ensure that the folder exists before you try to change ownership. I assume you have error checking in place but have provided a streamlined version of your commands for input.

    Also, I'm confused by your use of quotes and escaped quotes in the script since those are variables to commands and not being printed, so not sure why you are using those.
    --
    Regards
    ...Ashwin


  7. #7
    Change ownership of home directory

    Member Since
    Apr 16, 2016
    Location
    CT
    Posts
    1,096
    Your Mac's Specs
    MacBook Air Mid-2012 / iMac Retina 5K Late-2014
    Rep Power
    5
    Ownership of a file or directory is based on the UID of the user. All UID's are "local" to the machine, even for network users.

    So, the first thing you need to do is to understand how your network users will be representing themselves to the local machine. For example: When I share directories on my Linux machines, I use SAMBA. And, as part of SAMBA, I "map" usernames in the SAMBA software to usernames on the local machine. The local username has an associated UID with it, and THAT'S the UID that I have to ensure owns their home directory.

    Also, it's pretty important to ensure that UID's stay consistent across various machines on a network so that you always use the same UID no matter where you are. That way, if drives are moved around or sharing changes, the numbers remain the same and so does the ownership.

    When changing ownership, you use the CLI utility "chown". It supports a username as a parameter, but can also take the UID directly. Use either "chown rsmith" or "chown 1029" or whatever the name or UID is.

    You should also be looking to do a recursive ownership change all the way down the tree, so the -R parameter probably makes sense. If default group membership will be different, then you should look to whether or not changing that would be necessary.

    If you're purely transferring ownership of the directory structure from old id to new id, there is no need to copy or to delete. If the username is changing, consider updating the record for the user so that things like ~/<user> will work properly.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. change Home directory name
    By pack234 in forum macOS - Operating System
    Replies: 4
    Last Post: 11-10-2016, 06:07 PM
  2. Home Directory question
    By pfisher in forum macOS - Operating System
    Replies: 1
    Last Post: 08-01-2011, 11:17 AM
  3. MBP and MacBook Air SWAP - Change of ownership
    By outsideworld in forum Apple Notebooks
    Replies: 3
    Last Post: 07-03-2011, 05:09 PM
  4. How do I change ownership of my Mac?
    By andyb in forum macOS - Operating System
    Replies: 4
    Last Post: 12-04-2007, 11:27 AM
  5. Home directory and filevault
    By Joolz34 in forum Switcher Hangout
    Replies: 15
    Last Post: 08-31-2005, 05:43 PM

Posting Permissions

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