Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a command to set settings in sshd_config, instead of manually editing the file? I would prefer it to do this way, because it's easier to automate. Otherwise I'd have to grep config with my script.

share|improve this question
Not a great idea. This has the potential for going horribly wrong. If you go that route, I hope you at least have your /etc files under version control. I recommend etckeeper. – Faheem Mitha Dec 17 '11 at 15:38
up vote 1 down vote accepted

Although there is no standard tool to change settings in sshd_config, Ubuntu's post-installation script has some functions for modifying the configuration.

It supports:

  • enabling and disabling options
  • renaming options
  • reading the value of settings, e.g. it retrieves 22 from Port 22
  • setting the value of settings, e.g. it can set 22 for Port: Port 22

There are limitations:

  • it does not support contexts (like Match)
  • it's not aware of the values a key expects, e.g. AllowUsers user@host more@multiple values

It can be found on http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/oneiric/openssh/oneiric/view/head:/debian/openssh-server.postinst

An alternative to such functions using sed. For example, to change a Port setting:

sed "s/^ *Port .*/Port 22/i" -i sshd_config

Obviously, this only works if Port was defined before. As an alternative, you can remove existing settings and append the new setting:

sed "/^ *Port/di" -i sshd_config
echo Port 22 >> sshd_config
share|improve this answer

As with approximately 99.9999999% of *nix config files, no.

share|improve this answer

Most applications that provide separate wrappers to edit their config files do so to avoid dangerous race conditions (like the visudo command) or to avoid a syntax error from corrupting the application (again, like what visudo is there for). These things don't really apply to ssh_config.

So the short answer is no. However, that doesn't stop you from making your own.

share|improve this answer
There's no reason to turn off sshd while editing sshd_config! In fact, you would normally never turn off sshd, just restart it after changing the configuration. The main reason for visudo isn't race conditions (though that's a factor too), it's primarily there to avoid locking yourself out due to a syntax error that you would no longer be able to fix because sudo wouldn't be letting you do it. – Gilles Dec 17 '11 at 22:58
Thanks @Gilles, I edit my answer to reflect that. – n0pe Dec 17 '11 at 23:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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