SSH: Finding users home directories.
Hello there;
I maintain server with 89 account of websites created by my company. I always login to sFTP using root.
When I upload files using root account, they aren't writable with account owner so I wrote this .sh script to help me fix permissions for files for each account:
My problem started when I used second harddisk and mounted it as /home2/ so the .sh script isn't working for all accounts since some accounts are created in home2 instead of home. I hope someone can guide me to get a variable for correct user public_html directory instead of hard coding it which in some cases isn't correct. Best Regards
# Get current users lists.
cd /var/cpanel/users
# Loop users.
for user in *
do
# Exclude system, root, nobody users.
if [ "$user" != "system" ] && [ "$user" != "root" ] && [ "$user" != "nobody" ]
then
echo "chown -R $user:$user /home/$user/public_html"
# Update permission to all files in public_html for that user.
chown -R $user:$user /home/$user/public_html
fi
done
My problem started when I used second harddisk and mounted it as /home2/ so the .sh script isn't working for all accounts since some accounts are created in home2 instead of home. I hope someone can guide me to get a variable for correct user public_html directory instead of hard coding it which in some cases isn't correct. Best Regards
-
I really have reservations about what you are doing and how you are doing it. Blindly changing the ownership of files and directory probably isn't a good idea. But to answer your specific question. To get the home directory of a specific user: [font="courier new">homedir=$(getent passwd ${user} | cut -d : -f 6) Then replace instances of [font="courier new">/home/$user with [font="courier new">${homedir} 0 -
I really have reservations about what you are doing and how you are doing it. Blindly changing the ownership of files and directory probably isn't a good idea. But to answer your specific question. To get the home directory of a specific user: homedir=$(getent passwd ${user} | cut -d : -f 6) Then replace instances of /home/$user with ${homedir}
Works like Charm. Thank you very much. I really appreciate your concern but I'm not changing ownership of all files. I'm just changing ownership for files inside public_html directory which are owned by this user by default. Also since I'm the coder for these 89 websites, I understand the risks. Greetings0 -
Thanks for the input on that @sparek-3 - I do agree that blindly changing ownership of files and directories is not recommended practice. @fokakmeny glad to see you were able to get a resolution. 0
Please sign in to leave a comment.
Comments
3 comments