Jump to content

macros using symbolic links to redirect home folders to network drive


Recommended Posts

Posted

Hi all,

I've written a small script which removes the logged on users documents\downloads\movies\music\pictures folders and creates a symbolic link to their networked home drive equivalent. This works fine until I run it as a user which will not have the required sudo rights to remove system folders. Is there anything I can add to the script to force it to run as a user with sudo privilege or am i going about it the wrong way? Has anyone else managed to do this and get it working?

Thanks

Posted
I've written a small script which removes the logged on users documents\downloads\movies\music\pictures folders and creates a symbolic link to their networked home drive equivalent.

 

Just in case something goes wrong, you might want to have that script move said folders into a hidden / temp folder somewhere on the harddrive, just so you've got somewhere you can restore them from if needed.

 

Is there anything I can add to the script to force it to run as a user with sudo privilege or am i going about it the wrong way?

 

Not that I could figure out:

 

http://www.edugeek.net/forums/mac/173154-mac-os-network-login-script.html

 

The answer seemed to be to create a launchd Global Daemon that runs as root that starts at boot time, then have a launchd Global Agent run at each user login to signal that Global Daemon in some way to go and sort folders out. My startup script did the signalling bit by writing files into a folder which the root daemon then checked.

Posted (edited)

You shouldn’t need to run your script as a sudo admin user as the currently logged in user already has the permissions to remove there own Documents directory etc and put a symlink in its place. But that depends on how exactly you are running the script to begin with.

 

Don’t know what system folders you are talking about but you shouldn’t have issues with the folders you have listed.

 

Should note that replacing anything in the users folder with symbolic links to a mounted network drive is considered bad practice and Apple strongly recommend against doing it.

Edited by ovek
Posted
Thanks for your replies :) I know its not recommended but we've ended up with so many users having trouble accessing their documents where they have been stored on the local user account instead of their home network drive. I want a user to be able to open their documents folder and it goes to their network drive. At the moment i use sudo rm -rf /Users/$USER/Documents etc to remove the local folder and then sudo ln -s /Volumes/$USER/Documents /Users/$USER to create the link. It works but the user first has to open their network home to make the connection before the script runs. Its a crap way of doing it but does anyone know how to do it without spending on expensive management software for just 30 clients....
Posted
You shouldn’t need to run your script as a sudo admin user as the currently logged in user already has the permissions to remove there own Documents directory etc and put a symlink in its place. But that depends on how exactly you are running the script to begin with.

 

Do you have any more details? I tried having a login script running as the current user remove the folders, but got permission errors - is there some particular way I needed to go about things?

 

Do you know why Apple regard replacing folders with symbolic links to mounted drives bad practice?

Posted
Do you have any more details? I tried having a login script running as the current user remove the folders, but got permission errors - is there some particular way I needed to go about things?

 

Do you know why Apple regard replacing folders with symbolic links to mounted drives bad practice?

 

I don't have access to my mac at the moment so I will come back to you.

 

As for why Apple frowns upon redirecting user folders, who knows they also don't like you modifying the default user template but you have no other alternative in a multi-user environment even if you have a MDM in place.

 

If you run a full mac environment like me you quickly learn not to script macOS to much. Don't get me wrong macOS is a great flexible/scriptable OS that it doesn't get enough credit for. But Apple can be unpredictable with the OS updates and you might find things that might have worked for years not working in the new OS or even point releases, either with bugs or just undocumented changes.

  • Thanks 1
Posted
Thanks for your replies :) I know its not recommended but we've ended up with so many users having trouble accessing their documents where they have been stored on the local user account instead of their home network drive. I want a user to be able to open their documents folder and it goes to their network drive. At the moment i use sudo rm -rf /Users/$USER/Documents etc to remove the local folder and then sudo ln -s /Volumes/$USER/Documents /Users/$USER to create the link. It works but the user first has to open their network home to make the connection before the script runs. Its a crap way of doing it but does anyone know how to do it without spending on expensive management software for just 30 clients....

 

Try and add the following to your script to auto mount your network drive.

 

open "smb://server/share"

  • Thanks 1
Posted
Sorry to hijack your thread, but I'm running into the same issue with folder redirects to AD home locations. Ever since 10.13.5 dropped over the weekend, our Ad joined Mac's won't automount their AD home locations to /Volumes/Users anymore. thus our script we had in place to redirect the Desktop and Documents folders no longer work. I'd appreciate any assistance that anyone can give!
Posted (edited)

Ok hold on tight I got a little bored at work so wrote the entire script and all the other bits that you need.

 

I have included a install script that will copy the script in to /Library/Application Support/login/ and fixes the permissions and sets up the launch agent in the user template folder (so new users get modified) and loop through all current users present on the mac.

 

Some simple steps below should get you up and running...

 

Go download all three files from here: https://github.com/Ovek99/macOS-Login-Alias-Script, use the "clone/download button" to get a zip. Extract that zip in to a folder on your macs local admin desktop and you should have com.user.loginscript.plist, installscript.sh and login.sh.

 

Next open the installscript.sh file and add your local admin user to the SKIPUSER variable. If you don't want the script to open a network connection on load or delete various folders (like the Desktop) open login.sh and comment out (#) the bits that you don't need and save it.

 

Open up terminal and run the script (e.g. sudo bash ~/Desktop/script/installscript.sh).

 

This is fully working on 10.13.4 for me but as with all random scripts this needs testing & more testing before deployment, I'm sure you will need to modify parts to better suite your environment anyway.

 

 

EDIT: I should note that the login.sh script could be capable so doing more than aliasing user folders and you could use it to se default settings for applications etc but thats beyond the scope of today. ;)

Edited by ovek
  • Thanks 3
Posted

Just had a quick look through and I have a feeling our home drive location will cause a problem. We have a homes$ windows share inside which is staff and student folder, which contains users. So for example, server\homes$\staff\username or server\homes$\students\2018\username is there a way to get that path into the script as I dont know how to construct that part of it. I saw a script long ago which had the command to get the path to store as a variable but cant find it for looking anymore.

Thanks

Posted
You are a legend! Thank you for this @ovek! When I get some time to myself I will get this all set up and let you know how it goes. Thanks for taking your time to help!

 

No problem, hope it helps and helps other people in the future ;)

 

Just had a quick look through and I have a feeling our home drive location will cause a problem. We have a homes$ windows share inside which is staff and student folder, which contains users. So for example, server\homes$\staff\username or server\homes$\students\2018\username is there a way to get that path into the script as I dont know how to construct that part of it. I saw a script long ago which had the command to get the path to store as a variable but cant find it for looking anymore.

Thanks

 

found it :)

http://www.edugeek.net/forums/mac/63495-mac-os-x-mapped-home-drives.html

It uses grep to save the info to a variable then I can just add it to your script and we should be away

Sweet :)

 

Yeah should work fine, or as an alternative try. Same thing just a different Active Directory Attribute.

 

dscl /Active\ Directory/All\ Domains -read /Users/$CURRENTUSER OriginalHomeDirectory | awk '{ print $2 }' | sed -e 's/<[^>]*>//g'\

 

:)

  • 4 months later...
Posted

Hi Ovek, sorry its been a while. Ive managed to get it mostly working, but I get a > prompt when using the line of code dscl /Active\ Directory/All\ Domains -read /Users/$CURRENTUSER OriginalHomeDirectory | awk '{ print $2 }' | sed -e 's/<[^>]*>//g'\

I can get the correct string returned using just dscl /Active\ Directory/DOMAINNAME/All\ Domains -read /Users/$CURRENTUSER SMBHome | grep \\\\\\\\.* -o but I just need to turn the \ to the accepted / . I believe thats what the awk and the sed commands do but I'm getting the prompt every time I call them. Cant work out which one is causing it though.

I hope that makes sense.

James

 

Posted (edited)
Hi Ovek, sorry its been a while. Ive managed to get it mostly working, but I get a > prompt when using the line of code dscl /Active\ Directory/All\ Domains -read /Users/$CURRENTUSER OriginalHomeDirectory | awk '{ print $2 }' | sed -e 's/<[^>]*>//g'\

I can get the correct string returned using just dscl /Active\ Directory/DOMAINNAME/All\ Domains -read /Users/$CURRENTUSER SMBHome | grep \\\\\\\\.* -o but I just need to turn the \ to the accepted / . I believe thats what the awk and the sed commands do but I'm getting the prompt every time I call them. Cant work out which one is causing it though.

I hope that makes sense.

James

 

 

The trailing \ at the very end of the command was causing the prompt it shouldn't have been there, also the "OriginalHomeDirectory" active directory attribute isn't used anymore depending on what Windows Server version you use... my bad sorry about that!

 

Here is the code that should work for you:

dscl /Active\ Directory/DOMAIN/All\ Domains -read /Users/$CURRENTUSER SMBHome | awk '{ print $2 }' | tr '\' '/'

 

"tr" at the end is the command that will do the character swapping.

 

Good Luck :)

Edited by ovek
Posted

I hate to hijack this thread but i'm having trouble with redirecting mac desktop/documents/music etc to their relative windows share location.

 

Our students have different locations to staff on our file server, for example:

 

\\VM1\Staff\STAFFUSERNAME\UserArea <-Documents

\\VM1\Staff\STAFFUSERNAME\Desktop <-Desktop

 

Students have local profiles but with read only desktops to a generic share.

We use Profile Manager for management.

 

Can anyone provide a walkthrough guide to implement the script?

Thanks!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...