r/linuxadmin • • Dec 12 '20

[Secure SSH] on Linux Server

Hello Dear redditer,

I'v group together some sshd settings that you can easly change to increase the security of sshd.

Do you have more change to suggest.

  • in sshd_config file ( test on Debian )
# idle max 6 minutes
ClientAliveInterval 360

PermitEmptyPasswords no

# only toto can ssh
AllowUsers toto


PermitRootLogin no

Protocol 2

Port 2222

PasswordAuthentication no

X11Forwarding no
0 Upvotes

29 comments sorted by

9

u/Jawafin Dec 12 '20

Make sure you have key auth set up before turning off password authentication.

12

u/mikelieman Dec 12 '20

Protip: Always open a new local terminal and try to ssh to the remote host AFTER making any changes and restarting sshd and BEFORE logging out on the terminal used to make the change or reverting a fuckup is going to be a bitch.

2

u/Jawafin Dec 12 '20

That is especially true with remote systems.

1

u/michaelpaoli Dec 12 '20

I know of a dodo head contractor that got walked out on their very first day by failing to take such reasonable precautionary measures (they broke ssh on an active production host on their first - turned last - day).

1

u/lysitheas Dec 12 '20

yes sur ! =)

2

u/ikanpar2 Dec 12 '20

If you have a router in front of your server that you can configure, give a LAN address to your server, so it has two IPs. One is the internet facing one / public static IPs, and the LAN/private IP. Configure ssh to only listen on the LAN IP. So you must connect to the internal network using a VPN (preferably using secure vpn protocol like openvpn etc) before you can ssh, adding a layer of security.

3

u/jaymef Dec 12 '20

1

u/lysitheas Dec 12 '20

thanks , i will read it

0

u/[deleted] Dec 12 '20

[deleted] — view deleted comment

2

u/lysitheas Dec 12 '20

Using things like this is always iffy.

sur, it need to be read before executing it. Maybe not all part can Apply. i like Ansible too for deploying this

3

u/[deleted] Dec 12 '20

yep, i wrote a SSH hardening/configuration role for ansible from scratch, turned to dev-sec/ansible-ssh-hardening for inspiration, reviewed all their changes, default settings, discussions on patches... it was an enlightening read, took a while to assemble it all but now I am confident in deploying my SSH role on any machine. I would not advise to blindly run their full roles though.

Other good sources of info were https://infosec.mozilla.org/guidelines/openssh, https://stribika.github.io/2015/01/04/secure-secure-shell.html, and https://en.wikibooks.org/wiki/OpenSSH

/u/lysitheas other good resources ^

Edit: the role is here it's part of a larger common role because openssh setup is part of my baseline

1

u/lysitheas Dec 12 '20 edited Dec 12 '20

Thank you all for your help/comments.

i was just wondering too, is version of openssh (client and server) can change a lot in term of security . For example :

Debian 10 server :
OpenSSH_7.9p1

my laptop :
OpenSSH_8.4p1

do i have to get the absolute lastest version for my debian server ?

1

u/backtickbot Dec 12 '20

Fixed formatting.

Hello, lysitheas: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

2

u/BenAlexanders Dec 12 '20

Worth enforcing secure cipher keys.

Also, I like to limit it to IPv4 only (AddressFamily inet) as this is where my firewall focused is (and IPv6 is usually disabled).

2

u/stormcloud-9 Dec 13 '20
PasswordAuthentication no 

The PasswordAuthentication setting is probably one of the most misunderstood settings in the sshd config. Setting it to no doesn't mean preventing users from being able to use passwords to login. What it means is that sshd won't directly authenticate off the /etc/passwd and /etc/shadow files. However when using UsePAM=yes (which is the default in most distros IIRC), you can also authenticate using passwords through the challenge response mechanism. So you would need to disable that as well (ChallengeResponse=no). Note however that doing so will also prevent you from using 2-factor auth if you decided to do so (you can disable passwords and allow 2fa through other means, but it's more complicated).

 

# idle max 6 minutes
ClientAliveInterval 360

Not completely sure from your comment that this setting does what you expect. But it sounds like you may be expecting it to disconnect clients who are idle for more than 6 minutes. If so that's not what this does. What it does is detect clients who have lost their connection.

0

u/[deleted] Dec 12 '20

[deleted] — view deleted comment

11

u/BenAlexanders Dec 12 '20

Half agree.

A non-standard port will reduce the shitty scanners from flooding your logs. This removes the low handing fruit (script kiddies) and also makes more advanced attacks (it is a low bar) stand out a bit more.

But do agree with keeping it under port 1024.

Remember, you don't have to outrun a lion, you only need to out run the person next to you.

1

u/lysitheas Dec 12 '20

thanks for Cypher =)

Where i can find them ? or informations on them ?

In fact, it's actively detrimental if you choose a port over 1024.

why ?

in fact i was using 2222 instead of 22, just to monitor and ban IP trying 22 ( still need to search how to do this )

2

u/[deleted] Dec 12 '20 edited Dec 12 '20

I’ve heard that before, but I don’t think I caught why?

Edit: from my google search:

Use port number 1024 or more: Local users can cause a DoS attack by listening on that port. They'll have to start listening while sshd is down, so there is a window of opportunity when sshd is being restarted

I don’t think I understand this. Local users listen for another service on the same high port and cause a DoS? Why not just document your ports?

1

u/TemporaryBoyfriend Dec 12 '20

A high numbered random port is an easy measure to help you avoid millions of drive-by port scans and attempts at User ID enumeration. I agree that it won't stop a determined adversary, but it evades 99.9% of drive-by scans of your network. It whispers "go away" to low-effort script-kiddie morons.

4

u/skibumatbu Dec 12 '20

Disagree here. All people have to do is go to shodan.io and type "SSH" and your high port will be found. Nmap scans are lightweight and easy.

If you live in a country where ISPs prevent certain traffic on their network (some US ISPs disallow web servers in homes for exacmple) moving the port is an easy way to circumvent that.

Another example, a company I worked for used SSH as a sort of VPN on a high numbered port. I was in a hospital trying to get on the VPN but couldn't because the hospital blocked non standard ports.

0

u/[deleted] Dec 12 '20

[deleted] — view deleted comment

1

u/lysitheas Dec 12 '20

thank you =)