Operating System - HP-UX
1826332 Members
3498 Online
109692 Solutions
New Discussion

Re: Re-creating /home directories

 
Bob Ferro
Regular Advisor

Re-creating /home directories

Is there any easy way to re-create /home directories and all of the . (dot) files in them? I know the data can be restored from a back up or tar'd and rcp from another server but I was wondering if there is a command that will accomplish this without deleting and readding the users.

Thanks
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: Re-creating /home directories

Hi Bob:

Depending on your objectives, you could re-populate the home directories with "virgin" copies of the files in '/usr/newconfig/etc/skel'.

You could do something like:

# for DIR in `ls /home`
> do
> cp /usr/newconfig/etc/skel/.profile /home/${DIR}
> chown ${DIR} /home/${DIR}/.profile
> done

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Re-creating /home directories

You are going to be far better off by restoring from backup because any user customization of the .profile's, .kshrc's, etc. plus any files in the users' directories will be lost.

If it ain't broke, I can fix that.
Court Campbell
Honored Contributor

Re: Re-creating /home directories

# for user in user1 user2 user3
>do
>cp -rR /etc/skel /home/$user
>chown $user:$(id -g $user) /home/$user/.*
>done

I am not sure if you need to recreate all the dirs, but you could get a list of user accounts on a file and cat the file like so

# for user in `cat userlist`
>do
>cp -rR /etc/skel /home/$user
>chown $user:$(id -g $user) /home/$user/.*
>done
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Dennis Handly
Acclaimed Contributor

Re: Re-creating /home directories

You could write a script that scans /etc/passwd and does a mkdir -m mode for each user. Then a chown user:group.

Did you not want to create the directory and . files if they are there.

Then copy the 4 . files from /usr/newconfig/etc/skel/ there.
Bill Hassell
Honored Contributor

Re: Re-creating /home directories

Not sure what you are asking. A user is given access (login and password) by placing the name in the passwd file. This is independent of the HOME directory. If you have a passwd file from another system, you can certainly append that list to the end of you existing passwd file, then spend the few hours fixing duplications or other conflicts. However, if you are running a Trusted system, then copying the passwd file is useless.

Now if you already have the users added to /etc/passwd, you can write a script that extracts the HOME directory from field 6, then creates that directory, copies everything from /etc/skel and changes the ownership to that user's login name.

IFS=":"
cut -f1,4,6 -d: /etc/passwd | while read MYUSER MYGROUP MYHOME
do
mkdir $MYHOME
cp -r /etc/skel/* /etc/skel/.??* $MYHOME
chown -R $MYUSER\:$MYGROUP $MYHOME
done


Bill Hassell, sysadmin
Bob Ferro
Regular Advisor

Re: Re-creating /home directories

The problem that we have was caused by migrating from 1 server to another. The original server did not follow any strict guidelines. There were a lot of users that should of been deleted before migration and a lot of new users added to the new server. The Oracle DBAs have most of the control of the server and all the users and directories and over time things got kind of messy. These are very large servers with hundreds and hundreds of users. Plus, instead of copying the /etc/group and /etc/passwd files from the old to the new, the existing ones on the new server were updated. Around here, a lot of servers go from production to test and back to production. We have a condition where we have many users in the /etc/passwd file without a corresponding /home directory. We don't know if these user accounts are needed but they should have all of their . (DOT) files. It is better to have the files there and no one logs on then to have no files and when someone logs on we have to get calls all times of the day or weekends. Some users might not log on for a long period of time.
A. Clay Stephenson
Acclaimed Contributor

Re: Re-creating /home directories

Ok, you are now in a situation of pay me now or pay me later (and pay me more later each time we migrate). You really need to sit down and think about how to really address your user management. You might strongly consider some form of centralized user/group management such as LDAP and you might even consider automounted home directories using maps as well so that no matter what machine a user logs in on, he gets the same home directory. Moreover your users' .profiles should probably be modified so that similar groups of users SOURCE files to set environment variables rather than setting these in each and every .profile. This gives you a way to change the environments of many users by changing one sourced file. It also is a nice way to handle cron'ed and batch jobs because both the cron'ed scripts and the users .profile could source the same file. The idea of using sourced files inside .profiles even addresses production and test machines because eventhough a user's home directory might be NFS automounted, it could still source environment specific files on the local machine.

Do this right and moving from box to box will become trivially easy.
If it ain't broke, I can fix that.