Skip to main content
Solved

adding config to users ~/.bash_profile and ~/.zshrc files


Forum|alt.badge.img+8

Hello all

 

I am trying to put in some configs for our servers into the ~/.bash_profile and ~/.zshrc files of users. It will make connecting to our servers a bit easier for non technical folks with the use of aliases. I am writing a bash script that basically checks if the file exists, if it does it appends to the file, it the file doesn't exist it creates it and then adds the code block into the file.

 

However while searching my own mac i do not seem to have the ~/.bash_profile file . Has anyone does something similar using a script and are there anything things to look out for when doing

Best answer by mm2270

If your script is specifically using ~/.bash_profile and similar for the .zshrc file as the path, then it's not going to work. In scripts run from Jamf Pro you cannot use the ~ as part of a path, since that automatically resolves to the home path of whoever is calling the command. With a Jamf Pro run policy, that's going to be root, so ~/.bash_profile actually becomes /private/var/root/.bash_profile

To fix this, you have to 1) get the logged in user's name, then either run all the commands that are adding entries into their bash and zsh profile as them, or 2) run the script as root but point to the full path of those files, as in /Users/$loggedInUser/.bash_profile

The problem with the second method is you'll need to add in some extra lines to ensure the user owns those files. By default they'll be owned by root and the user account may not even be able to read from them, or make any changes to them. Use chown and chmod to adjust the ownership and permissions on the files if you choose to use the second method.

View original

mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • March 17, 2022

If your script is specifically using ~/.bash_profile and similar for the .zshrc file as the path, then it's not going to work. In scripts run from Jamf Pro you cannot use the ~ as part of a path, since that automatically resolves to the home path of whoever is calling the command. With a Jamf Pro run policy, that's going to be root, so ~/.bash_profile actually becomes /private/var/root/.bash_profile

To fix this, you have to 1) get the logged in user's name, then either run all the commands that are adding entries into their bash and zsh profile as them, or 2) run the script as root but point to the full path of those files, as in /Users/$loggedInUser/.bash_profile

The problem with the second method is you'll need to add in some extra lines to ensure the user owns those files. By default they'll be owned by root and the user account may not even be able to read from them, or make any changes to them. Use chown and chmod to adjust the ownership and permissions on the files if you choose to use the second method.


Forum|alt.badge.img+8
mm2270 wrote:

If your script is specifically using ~/.bash_profile and similar for the .zshrc file as the path, then it's not going to work. In scripts run from Jamf Pro you cannot use the ~ as part of a path, since that automatically resolves to the home path of whoever is calling the command. With a Jamf Pro run policy, that's going to be root, so ~/.bash_profile actually becomes /private/var/root/.bash_profile

To fix this, you have to 1) get the logged in user's name, then either run all the commands that are adding entries into their bash and zsh profile as them, or 2) run the script as root but point to the full path of those files, as in /Users/$loggedInUser/.bash_profile

The problem with the second method is you'll need to add in some extra lines to ensure the user owns those files. By default they'll be owned by root and the user account may not even be able to read from them, or make any changes to them. Use chown and chmod to adjust the ownership and permissions on the files if you choose to use the second method.


thanks for your reply once again, leading me down the correct path. I made it so that the script saves another script onto the machine in the /private/tmp folder and then chmods and chowns and runs the script as the current logged in user and then deletes the script.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings