Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
01-22-2011, 01:30 PM
|
#1
|
Member
Registered: Aug 2008
Location: Chittagong
Distribution: Red hat 9, Fedora, Centos, RHEL5, Debian
Posts: 53
Rep:
|
Shell Script
Please add a script to add user whose shell will be /bin/false.
|
|
|
01-22-2011, 01:31 PM
|
#2
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,324
|
Quote:
Originally Posted by mosharaf_linux
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.
|
|
|
01-22-2011, 05:48 PM
|
#3
|
Senior Member
Registered: Jul 2007
Distribution: Gentoo
Posts: 2,125
|
@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'
|
|
|
01-22-2011, 10:46 PM
|
#4
|
Member
Registered: Aug 2008
Location: Chittagong
Distribution: Red hat 9, Fedora, Centos, RHEL5, Debian
Posts: 53
Original Poster
Rep:
|
@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.
|
|
|
01-23-2011, 05:32 AM
|
#5
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
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.
|
01-23-2011, 12:12 PM
|
#6
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,324
|
Quote:
Originally Posted by mosharaf_linux
@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.
|
|
|
01-24-2011, 09:08 AM
|
#7
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
TB0ne,
How did you manage:
Quote:
Originally Posted by TB0ne
[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
|
|
|
01-24-2011, 09:09 AM
|
#8
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,324
|
Quote:
Originally Posted by archtoad6
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.
|
|
|
01-24-2011, 09:41 AM
|
#9
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
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.
|
|
|
01-25-2011, 12:23 AM
|
#10
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415
|
For RHEL/Centos, this should work
Code:
echo $passwd | --stdin $username
where $passwd can be cleartext
|
|
|
All times are GMT -5. The time now is 04:20 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|