diff --git a/cmd/control/console_init.go b/cmd/control/console_init.go index ba0cfa2b..616e1c89 100644 --- a/cmd/control/console_init.go +++ b/cmd/control/console_init.go @@ -86,7 +86,7 @@ func consoleInitFunc() error { log.Error(err) } - if err := modifySshdConfig(); err != nil { + if err := modifySshdConfig(cfg); err != nil { log.Error(err) } @@ -242,19 +242,28 @@ func writeRespawn(user string, sshd, recovery bool) error { return ioutil.WriteFile("/etc/respawn.conf", []byte(respawn), 0644) } -func modifySshdConfig() error { +func modifySshdConfig(cfg *config.CloudConfig) error { sshdConfig, err := ioutil.ReadFile("/etc/ssh/sshd_config") if err != nil { return err } sshdConfigString := string(sshdConfig) - for _, item := range []string{ + modifiedLines := []string{ "UseDNS no", "PermitRootLogin no", "ServerKeyBits 2048", "AllowGroups docker", - } { + } + + if cfg.Rancher.SSH.Port > 0 && cfg.Rancher.SSH.Port < 65355 { + modifiedLines = append(modifiedLines, fmt.Sprintf("Port %d", cfg.Rancher.SSH.Port)) + } + if cfg.Rancher.SSH.ListenAddress != "" { + modifiedLines = append(modifiedLines, fmt.Sprintf("ListenAddress %s", cfg.Rancher.SSH.ListenAddress)) + } + + for _, item := range modifiedLines { match, err := regexp.Match("^"+item, sshdConfig) if err != nil { return err diff --git a/config/types.go b/config/types.go index 91fcfa37..117e62b2 100755 --- a/config/types.go +++ b/config/types.go @@ -182,8 +182,10 @@ type DockerConfig struct { } type SSHConfig struct { - Keys map[string]string `yaml:"keys,omitempty"` - Daemon bool `yaml:"daemon,omitempty"` + Keys map[string]string `yaml:"keys,omitempty"` + Daemon bool `yaml:"daemon,omitempty"` + Port int `yaml:"port,omitempty"` + ListenAddress string `yaml:"listen_address,omitempty"` } type StateConfig struct {