Linux - ServerThis 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.
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.
@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'
@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 09:49 PM.
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 08:04 AM.
Reason: add script comments, missing word
@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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.