Quick Linux question

The friendliest place on the web for anyone with an interest in aquariums or fish keeping!
If you have answers, please help by responding to the unanswered posts.
Status
Not open for further replies.

deli_conker

Aquarium Advice Addict
Joined
Dec 4, 2003
Messages
1,728
Location
Central Ohio
Ok, so the boss went on vacation and gave me a to do list. We are in the middle of a migration that I, until now, haven't had a whole to do with directly. One of the things I need to do is copy a few generic files (.bashrc, .profile, etc) that have been tweaked , to all of the user's home directories. I can't just hard link them as some of the users will need to change some of the files, I need a seperate copy in each directory.

I can't get it to work with the cp command. Is there something else I can use in SuSE 9.3?
 
Do you have root access (shudder...)? You can't copy files into other people's accounts if you don't have the right access priviledges.
 
I agree, you have to be Root to have full access to other people's directories. When done, ensure that the users have, at least, read, execute privs on those files. HTH

8)
 
Yes I have root access.
Yes I can copy the files individually, I just don't want to spend all day typing...

Code:
cp /etc/skel/.* /home/user1
cp /etc/skel/.* /home/user2
etc.

I looked at the cp help file and haven't found anything that might help me. I even tried stupid things like...
Code:
cp /etc/skel/.* /home/*/

and

cp /etc/skel/.profile /home/*/.profile
to no avail...
 
Try this -

Code:
for i in /home/* ; do (for j in /etc/skel/.* ; do if test -f $j;then echo $j $i;echo `basename $i` $i/`basename $j`;fi;done);done

if it looks right to you, then change the first echo to cp, and the second echo to chown
 
That all kinda makes sense, but I'm fairly new to scripting in linux.

The i variable cycles through the home directory.
The j variable cycles through the /etc/skel directory.
I'm not sure what the "if test -f" does. It seems like it looks into the current /home/i/ for a file named /etc/skel/j and if false (the -f switch?) if copies the file and then changes the ownership to the username matching the directory it is copying it into? Next j, next i.

What does the "fi" do?

Sorry, like I said, fairly new to linux and want to double check all of this before I mess it all up...
 
deli_conker said:
I'm not sure what the "if test -f" does. It seems like it looks into the current /home/i/ for a file named /etc/skel/j and if false (the -f switch?) if copies the file and then changes the ownership to the username matching the directory it is copying it into? Next j, next i.

What does the "fi" do?

"test -f" checks to see if $j is a file. This is to avoid trying to copy "." and ".." from /etc/skel. If you have any directories in /etc/skel that you want copied, then this will be a problem. And as bzbee said, the fi is end if (if spelled backwards).

Sorry, like I said, fairly new to linux and want to double check all of this before I mess it all up...

Before you change the echo's it is totally safe and won't do a thing. Maybe this will be more clear as to what it is doing:

Code:
for i in /home/* ; do (for j in /etc/skel/.* ; do if test -f $j;then echo cp $j $i;echo chown `basename $i` $i/`basename $j`;fi;done);done

Still does nothing, but echos the actual commands it would do. Remove the echos to make it actually do the work.
 
deli_conker said:
I take it that done == next (as in old school basic next)?

Yeah - I guess "rof" didn't look cool enough. :)

Try 'help if' and 'help for' in bash to get an explanation of those commands ('info bash' if you've got a few hours to kill). 'man test' or 'info test' will explain the many functions it can do.
 
Thanks for the info. I just finished reading a short article about basename. It all makes sense. Thanks.

Now for part 2...

I need to set the passwords for all of these accounts as well. I am going to set a default password for each using...
Code:
passwd -e <username>
This will make it so that they will have to change their password after the first login. Can I use the same method in the cp script to expidite this? I am assuming that I can, but for some reason my boss didn't know how to do it (although in his defense, he was heading out to the Outer Banks for a week when he told me this).

I suppose I can always make an alias for "rof"... :lol:
 
That's probably a reasonable way to execute a command for each username.

Code:
for i in /home/* ; do echo passwd -e `basename $i` ;done

hmmm - when I do passwd -e to an account on my debian system I can't log in at all (using ssh). You might want to test its effect on your system before you hit all the user accoounts with it :)

"rof" alias is a neat idea - make one for "elihw" and "litnu" too ;) Actually done terminates any command with a "do", so there are a few others I think.
 
When I do the "passwd" command with the username after it, it asks me to type in the new password followed by a confirmation consisting of re-typing the new password.

I don't think the above code will work. Then again, I might just have to typeand retype the default password 100 or so times...
 
You have to enter password with passwd -e on Suse? You might want to try
Code:
for i in /home/*; do echo passwd -e $(basename "$i"); done
Hey Steve, does the user you're testing with ssh have shell access? Just a thought.
 
czcz said:
You have to enter password with passwd -e on Suse? You might want to try
Code:
for i in /home/*; do echo passwd -e $(basename "$i"); done
I still have to type and re-type the password for each user in the above example. Is there any way around that?
 
I just read this thread again. What else is on your to do list? I ask because you could have had /etc/skel/.* in each user's home, set same password, and -e flagged with one command: adduser. Try man adduser and see if it has other things you need -- consider starting users from scratch (seriously, maybe faster depending on whats been and needs to be done).
 
I had nothing to do with this part of the project (until my boss went on vacation). As far as users are concerned, I only have to copy the .* from /etc/skel to the already made users /home directories and give them a password (which is the same for all users).
 
Ok. I tried the script with the echoes in it. The cp works great, but the chown does not. The 'basename' part does not work correctly. The version I am using either does not support this natively or we do not have the necessary software to interpret this correctly.

I may just go ahead and type in all the user names and then copy and paste all other pertinent information in front of them (for chown and passwd). Not exactly time saving, but it has to be done soon...
 
Its not pretty (there's better ways to do this), but follows format repeated previously.
Code:
for i in /home/*; do echo usermod -p $(echo yourpass | openssl passwd -1 -stdin) $(basename "$i"); echo chown $(basename "$i") $i/.*; done
This pipes "yourpass" through openssl because its easy and usermod only likes encrypted passwords -- you need this echo when executing. Note basename syntax (which is also in code in previous post) or just man. So, if this is what you want, you would run
Code:
for i in /home/*; do usermod -p $(echo yourpass | openssl passwd -1 -stdin) $(basename "$i"); chown $(basename "$i") $i/.*; done
 
Status
Not open for further replies.
Back
Top Bottom