LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-20-2014, 05:34 PM   #1
eduardlh
LQ Newbie
 
Registered: Aug 2014
Posts: 2

Rep: Reputation: Disabled
Exclamation switching user


Hi all,
I've been trying to make a small bash script to create a user and then to switch to the new user. But, I cannot giving the password when the script executes the command

I'm trying this:

Code:
#!/bin/bash 
user=test
pass=xxxx
dir=/var/www/mydir
group=adm
bash=/bin/bash

sudo useradd -d "$dir" -s "$bash" -g "$group" -m $user
echo "$user:$pass" |sudo chpasswd 
echo -e "El usuario ha sido creado con exito...\n"
echo -e "$pass\n" | su - $user > /dev/null 2>&1 && echo -e "\nSwitching user completed... You're $user"
# ??????
I need switch user inside de bash script, but I don't know how... I hope you can help me...

Thanks in advance.

Last edited by eduardlh; 08-20-2014 at 05:40 PM.
 
Old 08-20-2014, 07:47 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
sudo su - $user
 
Old 08-20-2014, 09:04 PM   #3
eduardlh
LQ Newbie
 
Registered: Aug 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks for answer ...
I try that but... I running script as root. When I run sudo su - user or su - user asks me the user's password and there is where I have the problem. How can I make the password take it to a variable and not wait keyboard input?

Thanks for your help!
 
Old 08-20-2014, 09:46 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
If you are root, then you should be able to fully configure the user's credentials and to reach right in to the user's home directory and set that up, too. Just be sure to chown everything to the user's owner-id and group-id so that they "belong to him." (Unless you want to exercise your godly prerogatives to put something there that doesn't "belong to him.")
 
Old 08-20-2014, 10:38 PM   #5
Smokey_justme
Member
 
Registered: Oct 2009
Distribution: Slackware
Posts: 534

Rep: Reputation: 203Reputation: 203Reputation: 203
Eduard..

1) You can't redirect 'su - ' to /dev/null.. this won't work..
2) The script won't continue to execute after the user is switched.. So the portion [/code]echo -e "\nSwitching user completed... You're $user"[/code]
will be executed after you logout
3) 'su - ' will only accept passwords from terminal devices.. You can't pipe the password to it.. However, if the script is runned as root it should not ask you for the password
 
  


Reply



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
[SOLVED] switching to another user via terminal z99 Linux - Newbie 5 11-21-2012 04:17 AM
GUI user switching pixellany Linux - Software 0 04-19-2009 10:41 PM
Fast User Switching Arrowx7 Linux - Software 3 08-03-2005 03:32 AM
XP's user switching in redhat ?? Zzapped Linux - Newbie 2 11-08-2003 12:49 PM
Switching between root and user PapaNoHair Slackware 14 07-28-2003 12:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:37 PM.

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