Skip to content

feat: add support for exiting ssh session #3331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cmd/limactl/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/lima-vm/lima/pkg/ioutilx"
"github.com/lima-vm/lima/pkg/sshutil"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/sshocker/pkg/ssh"
"github.com/mattn/go-isatty"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,6 +50,7 @@ func newShellCommand() *cobra.Command {

shellCmd.Flags().String("shell", "", "shell interpreter, e.g. /bin/bash")
shellCmd.Flags().String("workdir", "", "working directory")
shellCmd.Flags().Bool("reconnect", false, "reconnect to the SSH session")
return shellCmd
}

Expand Down Expand Up @@ -81,6 +83,24 @@ func shellAction(cmd *cobra.Command, args []string) error {
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
}

restart, err := cmd.Flags().GetBool("reconnect")
if err != nil {
return err
}
if restart {
logrus.Infof("Exiting ssh session for the instance %q", instName)

sshConfig := &ssh.SSHConfig{
ConfigFile: inst.SSHConfigFile,
Persist: false,
AdditionalArgs: []string{},
}

if err := ssh.ExitMaster(inst.Hostname, inst.SSHLocalPort, sshConfig); err != nil {
return err
}
}

// When workDir is explicitly set, the shell MUST have workDir as the cwd, or exit with an error.
//
// changeDirCmd := "cd workDir || exit 1" if workDir != ""
Expand Down
Loading