Working with Vagrant Share
1. Sharing VM via HTTP
Use vagrant share to create a public URL that allows access to your Vagrant VM over HTTP.
| Command | Description | Notes |
|---|---|---|
| vagrant share | Starts sharing the VM via HTTP | Requires Vagrant Cloud account and vagrant share plugin |
| vagrant share --http-port 8080 | Shares a specific HTTP port | Defaults to port 80 if not specified |
Note: The
vagrant share command requires the vagrant-share plugin to be installed.
Warning: Sharing your VM publicly exposes it to the internet. Ensure sensitive data is not accessible.
2. Setting Up SSH Sharing
| Option | Values | Description |
|---|---|---|
| vagrant share | ssh |
Enables SSH sharing for the Vagrant environment |
| vagrant share --help | ssh |
Displays help for SSH sharing options |
Note: SSH sharing requires the Vagrant Share plugin to be installed.
Warning: Ensure that the Vagrant environment is running before enabling SSH sharing.
3. Configuring Custom Subdomain
| Option | Values | Notes |
|---|---|---|
| config.vm.share | "subdomain" |
Set custom subdomain for Vagrant Share |
Example: Setting Custom Subdomain
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/focal64"
config.vm.network "private_network", type: "dhcp"
config.vm.share = "myapp"
end
Note: The subdomain must be unique across all Vagrant Share users.
4. Using HTTPS Sharing
| Option | Values | Description |
|---|---|---|
| vagrant share --https | true |
Enables HTTPS sharing for the Vagrant box |
| vagrant share --https-port | 443 |
Specifies the HTTPS port to use for sharing |
| vagrant share --https-cert | path/to/cert.pem |
Path to the custom SSL certificate file |
| vagrant share --https-key | path/to/key.pem |
Path to the custom SSL key file |
Example: Custom HTTPS Certificate
vagrant share --https --https-cert /path/to/cert.pem --https-key /path/to/key.pem
Note: HTTPS sharing requires a valid SSL certificate and key. If not provided, Vagrant will generate a self-signed certificate for testing purposes.
5. Disabling HTTP Sharing
| Option | Values | Notes |
|---|---|---|
| config.share.http | false |
Disables HTTP sharing for the Vagrant box |
6. Connecting to Shared VM
| Connection Method | Description | Details |
|---|---|---|
| vagrant share | Shares the VM via a public URL | Uses ngrok or similar to expose local ports. Requires Vagrant Cloud account. |
| vagrant share --help | Displays help for sharing options | Lists all available flags and usage examples. |
Note: The shared URL is valid for the duration of the Vagrant session unless explicitly closed.
Warning: Sharing VMs publicly exposes local services. Ensure sensitive data is not accessible.
7. Managing Share Sessions
| Command | Description | Notes |
|---|---|---|
| vagrant share | Starts a new share session for the current Vagrant environment | Requires Vagrant Cloud account and valid API token |
| vagrant share --help | Displays help information for the share command | Lists all available options and usage examples |
| vagrant share --name <name> | Assigns a custom name to the shared environment | Useful for organizing multiple shared environments |
| vagrant share --domain <domain> | Specifies a custom domain for the shared environment | Must be unique and available in Vagrant Cloud |
| vagrant share --no-tunnel | Disables tunneling for the shared session | Useful when direct access is preferred over tunneling |
Note: The
vagrant share command requires the Vagrant Cloud API token to be set in the environment variable VAGRANT_CLOUD_TOKEN.
Warning: Sharing environments publicly exposes them to the internet. Ensure sensitive data is not included in shared environments.
8. Implementing Security Considerations
Note: Vagrant Share exposes your development environment to the public internet, which introduces significant security risks. Always ensure proper access controls and network configurations.
| Security Aspect | Description | Recommendation |
|---|---|---|
| Public Access | Share exposes the VM to public internet via a tunnel. | Use vagrant share with caution; only expose necessary services. |
| Authentication | No built-in authentication for shared services. | Implement custom authentication (e.g., HTTP Basic Auth) in your app or proxy. |
| Network Exposure | Port forwarding to public endpoints. | Restrict access using firewall rules or Vagrant's config.vm.network settings. |
| Data Protection | Unencrypted data transmission over public tunnels. | Use HTTPS and secure protocols for sensitive data handling. |
Example: Securing Vagrant Share with Basic Auth
vagrant share --http 80
After running this command, Vagrant will provide a public URL. To add basic auth:
// In your app's config (e.g., Express.js)
const express = require('express');
const basicAuth = require('express-basic-auth');
const app = express();
app.use(basicAuth({
users: { 'admin': 'password123' },
challenge: true
}));
Warning: Vagrant Share is intended for development and testing. Never use it in production or expose sensitive services without additional security layers.
9. Using Ngrok Alternative
Note: Vagrant Share uses
ngrok under the hood for tunneling. If you're unable to use ngrok, Vagrant provides an alternative using localtunnel.| Option | Values | Description |
|---|---|---|
| config.share.host | localtunnel |
Specifies the tunneling service to use. Set to localtunnel to use the alternative. |
| config.share.port | 8080 |
The port to tunnel through the localtunnel service. |
Example: Configuring Vagrant Share with localtunnel
config.vm.network "forwarded_port", guest: 80, host: 8080
config.share.host = "localtunnel"
config.share.port = 8080
Note: Ensure
localtunnel is installed and accessible in your environment. You can install it via npm install -g localtunnel.10. Troubleshooting Share Issues
| Issue | Description | Solution |
|---|---|---|
| Vagrant share fails with "Permission denied" | Share command fails due to insufficient permissions or firewall restrictions. | Ensure the Vagrant user has proper network access and that firewalls are configured to allow connections on required ports. |
| Share URL not accessible from external networks | The shared URL is unreachable from outside the local network. | Verify that port forwarding is correctly configured in your router and that the Vagrant box has a public IP address or uses NAT. |
| Share times out or disconnects frequently | Connection drops or times out during sharing sessions. | Check for network instability, increase timeout settings in Vagrant configuration, or use a more stable hosting provider. |
| Vagrant share command not found | The command is not recognized by the terminal. | Ensure Vagrant is installed correctly and that the command-line interface includes the share plugin. Run vagrant plugin install vagrant-share if needed. |
Note: For persistent issues, check Vagrant logs using
vagrant share --debug to get more detailed error information.