How do you allow a user to log in using "su - user" but prevent the user from login in using SSH?

I tried to set the shell to /bin/false but the when I try to su it doesn't work.

Are there several ways to only allo logins by su?

Is SSH's AllowUser the way to go? (how would I do this if it's the way to go)

link|improve this question

75% accept rate
feedback

8 Answers

up vote 21 down vote accepted

You can use AllowUser / AllowGroups if you have only a few users/groups that are allowed to login via ssh or DenyUser / DenyGroups if you have only a few users/groups that are not allowed to login. Note that this onyl restricts login via ssh, other ways of login (console, ftp, ...) are still possible.

If you have set the login shell to /bin/false you can use su -s /bin/bash user (replace /bin/bash with the shell of your choice)

link|improve this answer
thanks a lot to all. I didn't expect to get 2+ upvotes on my question :) I like the "su -s ..." construct a lot and the console/ftp is a good point. I was really after something like "su -s". – NoozNooz42 Jun 9 '10 at 16:27
feedback

In sshd_config add a line DenyUser [username]

Note that this will not prevent that user from logging in via the console.

link|improve this answer
feedback

If an account has no password (passwd -d username), they can't log in interactively (console, SSH, etc.). If they have a valid shell, su will still work. Note the "interactively," though; if somebody decides to set up an SSH keypair for the account, it will work!

link|improve this answer
does the user need to have a valid shell to su? I'm pretty sure you're still in the same original shell after you su to another user... you don't actually LOG IN as the other user... So, just setting the shell to /dev/null might work aswell. – Brian Postow Jun 9 '10 at 16:24
Yep, it still needs a valid shell: [root@localhost ~]# su daemon This account is currently not available. [root@localhost ~]# su - daemon This account is currently not available. (RHEL system, daemon's shell is /sbin/nologin) – astrostl Jun 9 '10 at 16:41
feedback

as others have said;

DenyUser username or DenyGroup groupname in sshd_config would prevent keypair/password login via ssh.

though i usually do something like AllowGroup ssh or something along those lines, and explicitly add people who need ssh access to that group.

then, you can do as other's have said: passwd -d username to blank out the users password, so they cannot log in at the console, or some other way. or better yet passwd -l username to 'lock' the account. it is possible ssh will deny access to a locked account, even with keys, but i'm not positive.

link|improve this answer
Actually ssh will allow you to log in using key authentication even when the account password is locked. – Richard Holloway Jun 9 '10 at 22:34
Good to know, thanks for the clarification... – cpbills Jun 10 '10 at 14:47
feedback

As I mentioned in a comment, I think that you can still su into an account with an invalid shell. So if you set the user's shell to /dev/null or whatever the shell of bin is, you should be able to still su into that user... but any attempt to log in in any way will quit you back out...

link|improve this answer
feedback

If you still want su to work, you can use sudo -u [username] or pass -s \bin\bash to su as a temporary shell. They both do the same in absence of a shell in /etc/passwd.

link|improve this answer
feedback

Don't specify a password for the user not allowed to log in or delete it.

# passwd -d myuser
link|improve this answer
feedback

Knowing which mechanism is best depends on the requirements. If you know the requirements, you can choose the appropriate mechanism. All of the above answers are valid for some set of requirements.

Do you only want to restrict SSH access? Do you need access for mail or ssh methods? Is access only from root?

su - user will require a password for user if it is run be a user other than root. However, sudo -i user -i does not require a password for user.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.