sshutil

package
v2.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 31 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// FormatCmd prints the full ssh command line.
	//
	//	ssh -o IdentityFile="/Users/example/.lima/_config/user" -o User=example -o Hostname=127.0.0.1 -o Port=60022 lima-default
	FormatCmd = FormatT("cmd")

	// FormatArgs is similar to FormatCmd but omits "ssh" and the destination address.
	//
	//	-o IdentityFile="/Users/example/.lima/_config/user" -o User=example -o Hostname=127.0.0.1 -o Port=60022
	FormatArgs = FormatT("args")

	// FormatOptions prints the ssh option key value pairs.
	//
	//	IdentityFile="/Users/example/.lima/_config/user"
	//	User=example
	//	Hostname=127.0.0.1
	//	Port=60022
	FormatOptions = FormatT("options")

	// FormatConfig uses the ~/.ssh/config format
	//
	//	Host lima-default
	//	  IdentityFile "/Users/example/.lima/_config/user "
	//	  User example
	//	  Hostname 127.0.0.1
	//	  Port 60022
	FormatConfig = FormatT("config")
)
View Source
const EnvShellSSH = "SSH"

Environment variable that allows configuring the command (alias) to execute in place of the 'ssh' executable.

Variables

View Source
var ErrHostKeyMismatch = errors.New("host key mismatch")

ErrHostKeyMismatch is returned by PinHostKey when the guest presents a host key that differs from the pinned key.

Formats is the list of the supported formats.

Functions

func CommonOpts

func CommonOpts(ctx context.Context, sshExe SSHExe, toolPath string, useDotSSH bool) ([]string, error)

CommonOpts returns ssh option key-value pairs like {"IdentityFile=/path/to/id_foo"}. The result may contain different values with the same key.

The result always contains the IdentityFile option. The result never contains the Port option, nor the options for the host key verification (see HostKeyOpts).

Path options take toolPath's form, for the binary that ends up reading them. scp hands its options to the ssh beside itself, which need not be sshExe. Version detection still runs against sshExe, since only ssh reports a version.

func CompanionForSSH added in v2.3.0

func CompanionForSSH(sshExe SSHExe, tool string) string

CompanionForSSH returns tool from the same toolchain as sshExe. NewSSHExe can select an ssh outside PATH, so resolving a companion by bare name can pick a different toolchain than the one PathForSSH formats paths for. It returns tool unchanged on non-Windows, or when no sibling exists, leaving it to PATH.

func DetectOpenSSHVersion

func DetectOpenSSHVersion(ctx context.Context, sshExe SSHExe) semver.Version

func DisableControlMasterOptsFromSSHArgs

func DisableControlMasterOptsFromSSHArgs(sshArgs []string) []string

DisableControlMasterOptsFromSSHArgs returns ssh args that disable ControlMaster, ControlPath, and ControlPersist.

func Format

func Format(w io.Writer, sshPath, instName string, format FormatT, opts []string) error

Format formats the ssh options.

func HostKeyOpts added in v2.3.0

func HostKeyOpts(ctx context.Context, toolPath string, instDirs ...string) ([]string, error)

HostKeyOpts returns ssh option key-value pairs for verifying the SSH host keys of the instances. With a single instance, the key is looked up by the "lima-<INSTANCE>" alias, which does not depend on the SSH address and port. With multiple instances, the key is looked up by the address and port.

An instance without the known_hosts file, i.e., an instance that this version of Lima has never started, or whose last start did not pin a key, is not verified. When none of the instances has the file, the result disables the host key verification. Otherwise, the keys of the instances without the file are accepted with StrictHostKeyChecking=accept-new, which still refuses a key that does not match the pinned key, and are recorded in /dev/null, as ssh records new keys in the first UserKnownHostsFile.

Path options take toolPath's form, as described on CommonOpts.

func IsControlMasterExisting

func IsControlMasterExisting(instDir string) bool

IsControlMasterExisting returns true if the control socket file exists.

func IsControlMasterRunning added in v2.2.0

func IsControlMasterRunning(ctx context.Context, instDir string) bool

IsControlMasterRunning reports whether an SSH ControlMaster process is actively listening on the instance's control socket. Unlike IsControlMasterExisting, which only checks that the socket file is present, this dials the socket to verify that a master is alive. A stale socket left behind by an unclean master exit (kill -9, OOM, host crash, or the Cygwin emulation file outliving its process) has no listener and returns false.

func ParseOpenSSHVersion

func ParseOpenSSHVersion(version []byte) *semver.Version

func PathForSSH added in v2.2.0

func PathForSSH(ctx context.Context, sshExe SSHExe, orig string) (string, error)

PathForSSH converts orig to the path form Lima's ssh-family invocations expect; unchanged on non-Windows. On Windows, Cygwin-based ssh (Git for Windows, MSYS2) gets a /c/Users/... form from the sibling cygpath, which respects the toolchain's fstab; native Windows OpenSSH gets forward slashes (C:/Users/...), which native ssh, ssh-keygen, and scp accept. It is a convenience wrapper for callers holding an SSHExe; PathForTool takes the binary's path directly.

func PathForTool added in v2.3.0

func PathForTool(ctx context.Context, toolPath, orig string) (string, error)

PathForTool is PathForSSH keyed to an arbitrary binary, for a tool that Lima resolves separately from ssh. Pass the tool's own path so the form follows the toolchain that reads it, whether the tool parses the path itself or hands it to the ssh beside it. Callers whose tool is Cygwin-based on every Windows host should convert through cygpath directly instead: the native form this returns as a fallback would be wrong for them.

func PinHostKey added in v2.3.0

func PinHostKey(ctx context.Context, instDir, instName, sshAddress string, sshPort int) error

PinHostKey verifies the SSH host key of the guest at sshAddress:sshPort against the known_hosts file of the instance. When the file pins no key yet, i.e., on the first boot after creating the instance, the key presented by the guest is trusted and pinned.

func ReadPinnedHostKey added in v2.3.0

func ReadPinnedHostKey(instDir string) (ssh.PublicKey, error)

ReadPinnedHostKey returns the SSH host key pinned in the known_hosts file of the instance. It returns nil when the file does not exist or pins no key yet. The host names in the file are ignored, so that the file remains valid after cloning or renaming the instance.

func RefreshKnownHostsFile added in v2.3.0

func RefreshKnownHostsFile(instDir, instName, sshAddress string, sshPort int) error

RefreshKnownHostsFile is similar to WriteKnownHostsFile, but does nothing when no key is pinned. Used for updating the address and the port in the known_hosts file of an instance that may be running with an older version of the host agent, which does not update the file.

func RemoveKnownHostsFileUnlessPinned added in v2.3.0

func RemoveKnownHostsFileUnlessPinned(instDir string) error

RemoveKnownHostsFileUnlessPinned removes the known_hosts file of the instance when it pins no key. An older version of Lima, which never pins a key, may start the instance next, and this version of limactl would then refuse to connect to the instance due to the empty file.

func RemoveStaleControlMaster added in v2.2.0

func RemoveStaleControlMaster(ctx context.Context, instDir string) (bool, error)

RemoveStaleControlMaster removes the SSH control socket only when no master process is listening on it. It returns true when a stale socket was removed. A live master's socket is never touched (returns false), and a missing socket is treated as a no-op (returns false). This lets callers recover from a wedged session without clobbering a healthy ControlMaster.

func SSHArgsFromOpts

func SSHArgsFromOpts(opts []string) []string

SSHArgsFromOpts returns ssh args from opts. The result always contains {"-F", "/dev/null} in addition to {"-o", "KEY=VALUE", ...}.

func SSHOpts

func SSHOpts(ctx context.Context, sshExe SSHExe, instDir, username string, useDotSSH, forwardAgent, forwardX11, forwardX11Trusted bool) ([]string, error)

SSHOpts adds the following options to CommonOpts: User, ControlMaster, ControlPath, ControlPersist, and whichever of ForwardAgent, ForwardX11, and ForwardX11Trusted the caller asks for.

func SSHOptsRemovingControlPath

func SSHOptsRemovingControlPath(opts []string) []string

SSHOptsRemovingControlPath removes ControlMaster, ControlPath, and ControlPersist options from SSH options.

func SSHOptsWithoutMultiplexing added in v2.3.0

func SSHOptsWithoutMultiplexing(ctx context.Context, sshExe SSHExe, toolPath, instDir, username string, useDotSSH bool) ([]string, error)

SSHOptsWithoutMultiplexing returns CommonOpts plus HostKeyOpts and an explicit User, adding neither multiplexing nor forwarding options. Use it for invocations that cannot share a control socket: native Windows OpenSSH has no multiplexing, and Cygwin ssh's is unreliable. It builds no control path, so the caller also escapes the socket length limit SSHOpts enforces. Path options take toolPath's form; pass sshExe.Exe unless another binary receives the options, as described on CommonOpts.

func ScanHostKey added in v2.3.0

func ScanHostKey(ctx context.Context, address string, port int) (ssh.PublicKey, error)

ScanHostKey returns the host key of the SSH server at address:port, like ssh-keyscan, preferring ssh-ed25519. The server has proven the possession of the private key when the key is returned.

func SftpServerForSSH added in v2.2.0

func SftpServerForSSH(ctx context.Context, sshExe SSHExe) string

SftpServerForSSH returns the path of sftp-server binary from sshExe's toolchain, so the server behind a reverse-sshfs mount comes from the same install as the ssh driving it. For Cygwin-based ssh the sibling cygpath resolves /usr/lib/ssh/sftp-server; for native OpenSSH it is sftp-server.exe beside ssh.exe. Returns "" on non-Windows, empty input, or no match, leaving the caller to fall back to its library's auto-detection.

func WriteKnownHostsFile added in v2.3.0

func WriteKnownHostsFile(instDir, instName, sshAddress string, sshPort int) error

WriteKnownHostsFile rewrites the known_hosts file of the instance, so that the pinned SSH host key is looked up by both the "lima-<INSTANCE>" alias and sshAddress:sshPort. When no key is pinned yet, an empty file is written, so that HostKeyOpts does not disable the host key verification until PinHostKey pins the key.

Types

type FormatT

type FormatT = string

FormatT specifies the format type.

type PubKey

type PubKey struct {
	Filename string
	Content  string
}

func DefaultPubKeys

func DefaultPubKeys(ctx context.Context, loadDotSSH bool) ([]PubKey, error)

DefaultPubKeys returns the public key from $LIMA_HOME/_config/user.pub. The key will be created if it does not yet exist.

When loadDotSSH is true, ~/.ssh/*.pub will be appended to make the VM accessible without specifying an identity explicitly.

type SSHExe

type SSHExe struct {
	Exe  string
	Args []string
}

func NewSSHExe

func NewSSHExe() (SSHExe, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL