LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-02-2009, 12:16 PM   #1
paladin21
LQ Newbie
 
Registered: Feb 2009
Posts: 2

Rep: Reputation: 0
Mass Creating Home Directories?


Hi all,

At my job, we lost a disk in the student programming server. The disk that went down was the one with all of the student home directories in it. Due to the backup policy on the server, the last backup doesn't include home directories for several hundred freshmen students/new programmers. What I need is a way to quickly put in place a home directory for each user so that the new guys can at least log in. Is there some sort of utility/script I can use for this? Thanks for your help!
 
Old 02-02-2009, 12:52 PM   #2
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

do you only have to create the directories or the users too? If you have a e. g. passwd file, you can use a loop like this:
Code:
getent passwd | cut -f1 -d: | while read user; do
  test -d /home/$user && continue
  mkdir /home/$user && chown $user:users /home/$user
done
It's only a sample - you have to check your system's prerequisites. Especially look for this:
- the home base directory
- the users, which are students (grep for a range of user id's)?
- need to copy /etc/skel into the user directories?

HTH
Jan
 
Old 02-02-2009, 01:13 PM   #3
paladin21
LQ Newbie
 
Registered: Feb 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for the help Jan. The only thing on the disk that died was the actual /home mount, so I have full access to the rest of the system. Also, the only user on the box that isn't a student is root. Home directories default to /home/user. Could I modify your example like this:

getent passwd | cut -f1 -d: | while read user; do
test -d /home/$user && continue
mkdir /home/$user && chown $user:users /home/$user
cp /etc/skel /home/$user
done

To get them a fully-functioning blank directory (at least for the new guys). I'm admittedly over my head on the shell scripting, and it's not something I can afford to leave down for a couple of days while I play with it, so thanks a ton for the help. I got hired for this job as a Windows Admin, and then found out one of my main servers was Linux, so I'm learning that side ASAP.

Last edited by paladin21; 02-02-2009 at 01:14 PM.
 
Old 02-02-2009, 07:59 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,405

Rep: Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783
Good guides to Linux at the cli and shell scripting:

http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 02-03-2009, 01:04 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by paladin21 View Post
Could I modify your example like this:
Code:
getent passwd | cut -f1 -d: | while read user; do
test -d /home/$user && continue
mkdir /home/$user && chown $user:users /home/$user
cp /etc/skel /home/$user
done
You want to do the cp before you chown...
Code:
getent passwd | cut -f1 -d: | while read user; do
  test -d /home/${user} && continue 
  mkdir /home/${user} && cp /etc/skel/.* /home/${user} && chown -R ${user}:users /home/${user}
done

Last edited by Tinkster; 02-03-2009 at 01:08 AM.
 
Old 02-03-2009, 02:02 PM   #6
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

Quote:
Originally Posted by Tinkster View Post
You want to do the cp before you chown...
Code:
getent passwd | cut -f1 -d: | while read user; do
  test -d /home/${user} && continue 
  mkdir /home/${user} && cp /etc/skel/.* /home/${user} && chown -R ${user}:users /home/${user}
done
right - and additionally skip the mkdir part if the user is root:

Code:
getent passwd | cut -f1 -d: | while read user; do
  test -d /home/${user} -o "$user" = "root" && continue 
  mkdir /home/${user} && cp /etc/skel/.* /home/${user} && chown -R ${user}:users /home/${user}
done
Jan
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
creating home directories on different machine.. shishirkotkar Linux - Newbie 3 04-25-2008 01:21 PM
Automatically creating user home directories oggy Slackware 1 11-18-2007 06:10 AM
Useradd not creating home directory when creating newuser meneedham Linux - Newbie 4 10-05-2007 01:11 PM
Apache Root/Home Directory and setting up FTP for home directories? Mankind75 Linux - Newbie 6 07-23-2006 03:37 PM
Creating home directories with samba and PAM mkhomedir ferrantepunto Linux - Software 2 01-21-2004 10:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration