SOLVED SSH: Finding users home directories.

fokakmeny

Member
Jan 15, 2012
5
0
51
cPanel Access Level
Root Administrator
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:
Code:
# 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
 

sparek-3

Well-Known Member
Aug 10, 2002
2,059
236
368
cPanel Access Level
Root Administrator
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}
 

fokakmeny

Member
Jan 15, 2012
5
0
51
cPanel Access Level
Root Administrator
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.
Greetings
 

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,298
1,279
313
Houston
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.
 
Thread starter Similar threads Forum Replies Date
J Miscellaneous 4