LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-22-2011, 01:30 PM   #1
mosharaf_linux
Member
 
Registered: Aug 2008
Location: Chittagong
Distribution: Red hat 9, Fedora, Centos, RHEL5, Debian
Posts: 53
Blog Entries: 1

Rep: Reputation: 0
Shell Script


Please add a script to add user whose shell will be /bin/false.
 
Old 01-22-2011, 01:31 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,324

Rep: Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089
Quote:
Originally Posted by mosharaf_linux View Post
Please add a script to add user whose shell will be /bin/false.
Write your own scripts...we're not here to do things for you.

If you want HELP, then post what you've written and some actual, useful, details, and we'll be glad to ASSIST you in working through them.
 
Old 01-22-2011, 05:48 PM   #3
Noway2
Senior Member
 
Registered: Jul 2007
Distribution: Gentoo
Posts: 2,125

Rep: Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781
@TBone, I agree that in English, the literal meaning of the post comes across as a bit rude but based upon the wording of some of their other posts, I think that there may be a little bit of language translation barrier. It is quite possible that s/he was trying to ask how one does this and it came across wrong.

@mosharaf_linux, when you create a user with the useradd function, use the -s option which sets the default shell. Set this to /bin/false. This will prevent the user account from being used to login. See the man documents for detailed information on the command, including all the option flags. You can get this information by typing 'man useradd'
 
Old 01-22-2011, 10:46 PM   #4
mosharaf_linux
Member
 
Registered: Aug 2008
Location: Chittagong
Distribution: Red hat 9, Fedora, Centos, RHEL5, Debian
Posts: 53

Original Poster
Blog Entries: 1

Rep: Reputation: 0
@Noway2, I know the usage of /bin/false. This is my script for add user in linux. But Now I want to add user by this script but shell will be /bin/false. So could you help me??


#!/bin/bash
# Script to add a user to Linux system
if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi

Last edited by mosharaf_linux; 01-22-2011 at 10:49 PM.
 
Old 01-23-2011, 05:32 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
mosharaf_linux,

I see you discovered that you accidentally clicked on the "Report" button instead of the "Quote" button, or perhaps the "Post Reply" button. In any case, you realized your mistake & fixed it; that is good.


Everyone,
What isn't so good, is that the mods (moderators) of this board have to follow up on each of these reports & this accidental one caused us some unnecessary work. It wasn't much extra work, so no real harm done. But do please be more careful in the future.

Happy LQ-ing, & thanks for your co-operation.

On the script
1. Reversing the the sense of the if would expose the error earlier in the written flow & make it read clearer. In other words,
if !x, few lines, else, many lines, fi
is better than
if x, many lines, else, few lines, fi
because the reader is not thinking about "what if not x?" while reading what is essentially the main line of the program.

2. Before you decided to use perl w/ all its overhead & the possibility that it may not be present, did you look at any CLI commands, like mcrypt? This might make your script both faster & more transportable.

Last edited by archtoad6; 01-24-2011 at 09:04 AM. Reason: add script comments, missing word
 
1 members found this post helpful.
Old 01-23-2011, 12:12 PM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,324

Rep: Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089
Quote:
Originally Posted by mosharaf_linux View Post
@Noway2, I know the usage of /bin/false. This is my script for add user in linux. But Now I want to add user by this script but shell will be /bin/false. So could you help me??
Well, if you know the usage, and Noway2 gave you the parameter, why are you having a problem? Add something like this to your script:
Code:
#!/bin/bash
if [ $(id -u) -eq 0 ]; then
	read -p "Enter username : " username
	read -s -p "Enter password : " password
        read -p "Enter shell (/bin/bash or /bin/false) : " shell 
	egrep "^$username" /etc/passwd >/dev/null
	if [ $? -eq 0 ]; then
		echo "$username exists!"
		exit 1
	else
		pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
		useradd -m -s $shell -p $pass $username
		[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
	fi
else
	echo "Only root may add a user to the system"
	exit 2
fi
Needs tweaking, but as Noway2 pointed out, all you've got to do is add the -s switch to useradd....as you said, you know the usage, so I'm confused as to why ask the question if you already knew the answer.

Last edited by TB0ne; 01-24-2011 at 09:09 AM.
 
Old 01-24-2011, 09:08 AM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
TB0ne,

How did you manage:
Quote:
Originally Posted by TB0ne View Post
[CODE]
...
[/QUOTE]
Oh well, I'm sure you know where the "Edit" button is.

Last edited by archtoad6; 01-24-2011 at 09:39 AM. Reason: typo
 
Old 01-24-2011, 09:09 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,324

Rep: Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089
Quote:
Originally Posted by archtoad6 View Post
TB0ne,

How did you manage: [/noparse]
Oh well, I'm sure to know where the "Edit" button is. [/QUOTE]

Sure, pick on a poorly edited post.
 
Old 01-24-2011, 09:41 AM   #9
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Oops, that should have been:
Quote:
I'm sure you know where the "Edit" button is. [correction underlined]
That's funny. Now let's get back on topic.


post 4200

Last edited by archtoad6; 01-24-2011 at 09:43 AM.
 
Old 01-25-2011, 12:23 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
For RHEL/Centos, this should work
Code:
echo $passwd | --stdin $username
where $passwd can be cleartext
 
  


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
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 02:45 AM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 01:21 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:20 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