LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 03-06-2016, 07:39 AM   #1
Fracker
Member
 
Registered: Mar 2009
Posts: 90

Rep: Reputation: 0
SSH User from Specific IP only


How to restrict each SSH user to logon from their respected trusted host or IP only.
 
Old 03-06-2016, 07:48 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
In my opinion, the best way to do it would be to use OpenVPN, instead. (But you can do a similar thing with ssh.)

Only one host possesses the necessary unique digital certificate that opens the lock. It doesn't matter what IP-address he's coming in from.

IP-address checks ("whitelisting") are, IMHO, never an adequate part of network security.
 
Old 03-06-2016, 07:53 AM   #3
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,627

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
read the man pages

I can think of several ways, all would be custom work and not standard use of the tools. OpenSSH and the security tools have many advances that I have not yet examined: there may be an answer in the standard tools. I would research that in the man and info pages first, then google for more.

I am not sure about the point, perhaps you could explain a little.

Do you need to only prevent them from getting a shell, or close off things like sftp also?

I take it that the clients are in a limited network, are there already some restrictions there that we might take into account?
 
Old 03-06-2016, 08:57 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by Fracker View Post
How to restrict each SSH user to logon from their respected trusted host or IP only.
That can be done in the keys, specifically the known_hosts authorized_keys file, but only if the users don't have write access to their known_hosts over on the server. You'd have to put the known_hosts authorized_keys files for each user into a special directory. The permissions would then be set so that each user could read their key but not write it. For example, something like /etc/ssh/known_hosts/%u /etc/ssh/authorized_keys/%u might work. See the manual page for sshd_config(5) in the part about AuthorizedKeysFile.

Perhaps the easier way to maintain would be to put in a lot of Match clauses in sshd_config - one per user - allowing specific users in from specific IP addresses, but banning everyone otherwise. You'd need to start with letting in the administrative users first. See sshd_config(5) in the part about Match and ForceCommand or maybe MaxAuthTries.

Edit: managed to write the wrong file name three times

Last edited by Turbocapitalist; 03-07-2016 at 08:30 AM. Reason: wrong file name
 
1 members found this post helpful.
Old 03-07-2016, 07:57 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Looking at it, MaxAuthTries seems to do the trick. I'd recommend requiring keys as well. Don't lock yourself out.

Code:
PasswordAuthentication no
MaxAuthTries 0

Match User fracker, Address 192.0.2.37
        MaxAuthTries 6

Match User user2, Address 192.0.2.137
        MaxAuthTries 6

Match User user3, Address 192.0.2.33
        MaxAuthTries 6
I don't think that method scales well though.
 
1 members found this post helpful.
Old 03-07-2016, 08:18 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Remember that the keys held by a user are public-keys issued by the server. (The keys can also be cryptographically protected.) But the essential principle behind this security is that each key issued is unique, and accountable to a particular person. If any key is compromised, it can be revoked without affecting other system access.

Nevertheless, in my opinion, the over-arching advantage of VPN over ssh is that the former is transparent to all system users: "connect it and fuhgeddaboudit." Every other piece of software simply perceives that "there is a subnet, and there is a router that gets me there, and that's that." They neither know nor care that the aforesaid connection is secure. They don't have to "do anything special" in order to achieve security. Otherwise, it is very easy to accidentally do something that is not secure, and to not realize it.

Another "plus" is that none of the files relating to this form of security are specific to them. The security applies as a blanket to the entire computer.

Last edited by sundialsvcs; 03-07-2016 at 08:20 AM.
 
Old 03-10-2016, 08:22 AM   #7
sunnysthakur
Member
 
Registered: Aug 2012
Posts: 92

Rep: Reputation: Disabled
You can do the configuration on /etc/host.allow and /etc/host.deny file for IP based restriction to SSH.
SSHD daemon support the tcp_wrapper and can control via /etc/host.allow and /etc/host.deny
 
Old 03-10-2016, 08:34 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by sunnysthakur View Post
You can do the configuration on /etc/host.allow and /etc/host.deny file for IP based restriction to SSH.
SSHD daemon support the tcp_wrapper and can control via /etc/host.allow and /etc/host.deny
Only in version 6.6 and older. In OpenSSH 6.7 and newer, tcpd (tcpwrappers) is not supported. iptables works for restrictions in some general cases, but I think they may not be useful in this particular case.
 
1 members found this post helpful.
Old 03-10-2016, 11:43 AM   #9
/dev/random
Member
 
Registered: Aug 2012
Location: Ontario, Canada
Distribution: Slackware 14.2, LFS-current, NetBSD 6.1.3, OpenIndiana
Posts: 319

Rep: Reputation: 112Reputation: 112
Quote:
Originally Posted by Turbocapitalist View Post
Only in version 6.6 and older. In OpenSSH 6.7 and newer, tcpd (tcpwrappers) is not supported. iptables works for restrictions in some general cases, but I think they may not be useful in this particular case.
Here ya go

EDIT: Works with 7.2p2 as well, but I have not tested it.
Attached Files
File Type: txt openssh-7.1p1-libwrap.txt (4.1 KB, 31 views)

Last edited by /dev/random; 03-10-2016 at 12:04 PM. Reason: More info
 
1 members found this post helpful.
Old 03-11-2016, 07:26 PM   #10
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,627

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Not quite the right solution.

Quote:
Originally Posted by sunnysthakur View Post
You can do the configuration on /etc/host.allow and /etc/host.deny file for IP based restriction to SSH.
SSHD daemon support the tcp_wrapper and can control via /etc/host.allow and /etc/host.deny
No, I think what he wants is user A form ip address a' only, user B from ip address b' only, etc. That requires fine control that tcp wrappers does not (in my experience) support. Latest versions of OpenSSH, however, do. Maintaining it might be somewhat labor intensive if you have much turnover.
 
Old 03-11-2016, 09:25 PM   #11
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
An alternative technique would be to have the server create a reverse secure shell connection to the trusted host that the user can then use to connect.

Last edited by allend; 03-11-2016 at 09:33 PM.
 
1 members found this post helpful.
Old 03-12-2016, 11:56 PM   #12
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
There's a more simpler method. use AllowUsers and AllowGroups - See this thread. OP is requesting simply access or no access by the ip, so match statements aren't needed.

In sshd config file /etc/ssh/sshd_config use this

Code:
AllowUsers user@ipaddr
eg
Code:
AllowUsers sefyir@127.0.0.1
ranges and wildcards work too

Code:
AllowUsers sefyir@127.0.0.1/24 sefyir@192.168.1.*
as do multiple specifications

Code:
AllowUsers sefyir@127.0.0.1,192.168.3.252
To give all admin access add them to the group admin (or whatever) - whatever being restricted to 192.168.1.10

Code:
AllowGroups admin whatever@192.168.1.10

Last edited by Sefyir; 03-12-2016 at 11:58 PM.
 
3 members found this post helpful.
Old 03-14-2016, 01:29 PM   #13
/dev/random
Member
 
Registered: Aug 2012
Location: Ontario, Canada
Distribution: Slackware 14.2, LFS-current, NetBSD 6.1.3, OpenIndiana
Posts: 319

Rep: Reputation: 112Reputation: 112
Quote:
Originally Posted by /dev/random View Post
Here ya go

EDIT: Works with 7.2p2 as well, but I have not tested it.
I can now confirm it works on 7.2p2 as well.
 
  


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
ssh works for all users except specific user (connection reset by peer) fcbman Linux - General 11 08-28-2014 08:35 AM
Disable telnet and ssh for a specific user krisr Linux - Newbie 7 11-17-2010 08:40 AM
How to block a specific user to run SSH and leave him the access to FTP ? frenchn00b Linux - General 3 06-19-2010 11:18 PM
run a specific script with root privilege as any user(possibly with ssh) z940303 Programming 5 09-05-2007 08:20 PM
Menu instead of shell for a specific user in ssh? jon_k Linux - Software 1 05-14-2004 06:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

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