1

My Ubuntu18 VMs come up with the following default config in /etc/ssh/sshd_config upon deployment, which makes my ssh sessions into them terminate upon 5min inactivity:

ClientAliveInterval 300
ClientAliveCountMax 0

Which exactly configuration do I add into my cloud_init.cfg to have them all deployed with much higher values? Security is not a concern.

Update: Adding this into cloud_init.cfg seems to fix the issue for Ubuntu16 and Ubuntu18 as my idle ssh session lasts >5min:

write_files:
  - path: /etc/ssh/sshd_config
    content: |
         ClientAliveInterval 30000
         ClientAliveCountMax 50

However all other parameters normally present in the /etc/ssh/sshd_config file are lost. Is there an equivalent of this sshd config file residing elsewhere by chance, and if so what's the full path and filename and how do I make just those 2 values in it edited leaving everything else intact?

Many thanks in advance!

2
  • Might it be easier to have your clients send 'keep-alives', such as putty's setting in Connection, named "Seconds between Keep-Alives".? Set it to like 15 seconds or something
    – infixed
    Nov 28, 2020 at 12:17
  • Yes that's an option indeed - thank you; another one would be manually changing those 2 in all VMs, which I did this time. Still wondering how to do it via cloud config: I can see several other things done at start this way, e.g. DNS servers injected into /etc/resolv.conf, so hopefully what I'm after is also possible..
    – dandreye
    Nov 28, 2020 at 12:53

3 Answers 3

4

Expanding on @dandreye, this takes into account that these could be commented out and sets the proper option regardless of their current value.

runcmd:
  - sed -i 's/\#\?ClientAliveInterval .\+/ClientAliveInterval 30000/' /etc/ssh/sshd_config
  - sed -i 's/\#\?ClientAliveCountMax .\+/ClientAliveCountMax 50/' /etc/ssh/sshd_config
  - service ssh restart

Though, a better option might be to just comment out the undesired options and drop a custom config into sshd_config.d:

write_files:
  - path: /etc/ssh/sshd_config.d/00-cloud-init
    content: |
      ClientAliveInterval 30000
      ClientAliveCountMax 50

runcmd:
  - sed -i 's/\#\?\(ClientAliveInterval .\+\)/#\1/' /etc/ssh/sshd_config
  - sed -i 's/\#\?\(ClientAliveCountMax .\+\)/#\1/' /etc/ssh/sshd_config
  - service ssh restart
1
  • Thanks for suggesting alternative approach @Eldon McGuiness: yes that certainly could be used instead and in fact looks nicer to my liking. I just had some kind of restrictions preventing me from using it back then: IIRC something like automatic removal of any non-default cloud config files from under sshd_config.d etc, which made me amend sshd_config.
    – dandreye
    Jan 1, 2022 at 17:51
1

What you need is to include a single line to tell cloud-init to append to sshd_config rather that replace the whole file, i.e append: true. So your cloud_init.cfg file will look like this:

---
write_files:
  - path: /etc/ssh/sshd_config
    content: |
         ClientAliveInterval 30000
         ClientAliveCountMax 50
    append: true
1
  • thanks for your feedback (sorry just saw it) - will give it a try, just hope it won't result in having two instances of each of those parameters (one "theirs" and one mine, obviously with different values).
    – dandreye
    May 24, 2021 at 18:14
0

Apparently adding this one into /etc/ssh/sshd_config does the trick (tested, works):

runcmd:
  - sed -i 's/ClientAliveInterval 300/ClientAliveInterval 30000/' /etc/ssh/sshd_config
  - sed -i 's/ClientAliveCountMax 0/ClientAliveCountMax 50/' /etc/ssh/sshd_config
  - service ssh restart

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .