Skip to main content

Managing SSH Connections

1. Connecting via SSH

Command Description Example
vagrant ssh Connects to the Vagrant box via SSH vagrant ssh
vagrant ssh <box-name> Connects to a specific box by name vagrant ssh web-server
vagrant ssh-config Displays SSH configuration for the box vagrant ssh-config

Example: SSH into a Vagrant Box

vagrant ssh

Example: SSH into a Specific Box

vagrant ssh database
Note: The SSH configuration is managed by Vagrant and can be customized in the Vagrantfile.

2. Running Single Command

Command Description Example
vagrant ssh Connects to the guest machine via SSH vagrant ssh
vagrant ssh -c Runs a single command on the guest machine vagrant ssh -c "ls -la"

Example: Running a single command via SSH

vagrant ssh -c "df -h"

3. Configuring SSH Settings

Setting Description Default Value Notes
config.ssh.host SSH host to connect to localhost Can be overridden in Vagrantfile
config.ssh.port SSH port to connect to 22 Change if using non-standard port
config.ssh.username SSH username for connection vagrant Typically set to vagrant in Vagrant boxes
config.ssh.password SSH password for authentication vagrant Not recommended for production use
config.ssh.insert_key Whether to insert SSH key into guest true Set to false if using password auth
config.ssh.privileged_user Privileged user for SSH connection root Used in some base boxes for root access

Example: SSH Configuration in Vagrantfile

config.ssh.host = "192.168.10.10"
config.ssh.port = 2222
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
config.ssh.insert_key = false
Note: When using custom SSH settings, ensure the guest VM allows connections on the specified port and that the user has appropriate permissions.

4. Setting SSH Port (config.ssh.port)

Option Values Description
config.ssh.port 22 SSH port to connect to the guest machine

Example: Setting SSH Port

Vagrant.configure("2") do |config|
  config.ssh.port = 2222
end

5. Using Custom SSH Keys

Option Values Notes
config.ssh.private_key_path ~/.ssh/my_key Path to the private key file for SSH connection
config.ssh.public_key_path ~/.ssh/my_key.pub Path to the public key file for SSH connection
config.ssh.insert_key true Whether to insert the private key into the VM (default: false)

Example: Configure Custom SSH Keys

Vagrant.configure("2") do |config|
  config.ssh.private_key_path = "~/.ssh/my_custom_key"
  config.ssh.public_key_path = "~/.ssh/my_custom_key.pub"
  config.ssh.insert_key = true
end
Note: Ensure the private key has appropriate permissions (e.g., chmod 600) to prevent SSH connection issues.

6. Disabling Agent Forwarding

Option Values Notes
config.ssh.forward_agent false Disables SSH agent forwarding for the Vagrant box

Example: Disable SSH Agent Forwarding

Vagrant.configure("2") do |config|
  config.ssh.forward_agent = false
end
Note: Agent forwarding is enabled by default in Vagrant for convenience, but can be a security risk if not required.

7. Setting Insert Key (config.ssh.insert_key)

Option Values Notes
config.ssh.insert_key true | false Controls whether Vagrant should insert a new private key into the VM

Example: Enable SSH key insertion

Vagrant.configure("2") do |config|
  config.ssh.insert_key = true
end
Note: When set to true, Vagrant will inject a new private key into the VM's ~/.ssh/id_rsa file, allowing for passwordless SSH access.

8. Configuring SSH Timeout (config.ssh.timeout)

Option Values Description
config.ssh.timeout 300 Timeout in seconds for SSH connections to the guest machine

Example: Setting SSH Timeout

Vagrant.configure("2") do |config|
  config.ssh.timeout = 600
end
Note: The default SSH timeout is 300 seconds (5 minutes). Adjust this value if your Vagrant environment requires longer connection times due to slow network or large file transfers.

9. Using Proxy Command (config.ssh.proxy_command)

Option Values Description
config.ssh.proxy_command ssh -o ProxyCommand='nc -X connect -x proxy.example.com:8080 %h %p' user@host Specifies the command to use for SSH proxying. Allows connecting through a proxy server.

Example: Setting up SSH proxy command in Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.hostname = "example.local"
  config.ssh.proxy_command = "ssh -o ProxyCommand='nc -X connect -x proxy.example.com:8080 %h %p' user@host"
end
Note: The %h and %p placeholders are replaced with the host and port respectively during connection.

10. Exporting SSH Config

Option Values Notes
config.ssh.config true Exports SSH configuration to ~/.ssh/config
config.ssh.host 127.0.0.1 Host address for SSH connection
config.ssh.port 2222 SSH port to connect to
config.ssh.username vagrant Username for SSH connection
config.ssh.private_key_path ~/.vagrant.d/insecure_private_key Path to private key for authentication

Example: Exporting SSH Config

Vagrant.configure("2") do |config|
  config.ssh.config = true
  config.ssh.host = "127.0.0.1"
  config.ssh.port = 2222
  config.ssh.username = "vagrant"
  config.ssh.private_key_path = "~/.vagrant.d/insecure_private_key"
end

11. Setting Compression (config.ssh.compression)

Option Values Description
config.ssh.compression false | true Enables or disables SSH compression. Default is false.

Example: Enable SSH Compression

Vagrant.configure("2") do |config|
  config.ssh.compression = true
end
Note: Enabling compression can reduce bandwidth usage but may increase CPU overhead.

12. Configuring Keep Alive (config.ssh.keep_alive)

Option Values Description
config.ssh.keep_alive true or false Enables or disables SSH keep-alive to prevent connection timeouts during long-running operations.

Example: Enabling Keep Alive

Vagrant.configure("2") do |config|
  config.ssh.keep_alive = true
end
Note: This setting is particularly useful when working with Vagrant boxes that have long-running processes or when using SSH connections in CI/CD pipelines where timeouts may occur.

Was this article helpful?