Some Vagrant Commands
Let's explore some common Vagrant commands.
Basic Vagrant commands
vagrant init [name [url]]
Initializes the Vagrant environment and creates the Vagrantfile.
$ vagrant init centos/7
Output
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
vagrant up [name|id]
Creates and configures the Virtual Machine from the Vagrantfile.
$ vagrant up
Output
==> default: Configuring and enabling network interfaces...
default: SSH address: 192.168.121.24:22
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Rsyncing folder: /home/naavi/Vagrant/my-vagrant/ => /vagrant
vagrant status [name|id]
Displays running or stopped VMs.
$ vagrant status
Output
Current machine states:
default running (virtualbox)
The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
vagrant halt [name|id]
Attempts a graceful shutdown.
$ vagrant halt
Output
==> default: Attempting graceful shutdown of VM...
vagrant destroy [name|id]
Removes the guest machine.
$ vagrant destroy
Output
default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...
vagrant ssh [name|id]
SSH into the guest machine or logs into the guest machine.
$ vagrant ssh
Output
Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-187-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
* Kubernetes 1.19 is out! Get it in one command with:
sudo snap install microk8s --channel=1.19 --classic
https://microk8s.io/ has docs and details.
8 packages can be updated.
0 updates are security updates.
New release '18.04.5 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Tue Sep 1 05:42:38 2020 from 10.0.2.2
vagrant@vagrantbox1:~$
vagrant suspend [name|id]
Suspends the machine in its current state and does not shutdown.
$ vagrant suspend
Output
==> default: Saving VM state and suspending execution...
vagrant resume [name|id]
Starts the guest machine from suspended or halt.
$ vagrant resume
Output
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
vagrant box add ADDRESS
The vagrant box add ADDRESS
command downloads the box from the cloud repository and stores it in your local environment. It does not initialize the Vagrantfile
.
$ vagrant box add centos/7
vagrant global-status
This command will show the current state of the Vagrant VM in your system, regardless of the directory.
$ vagrant global-status
Output
id name provider state directory
-------------------------------------------------------------------------
5c7d007 default virtualbox poweroff /home/naavi/Downloads/vag
14d0e56 default virtualbox poweroff /home/naavi/Downloads/vag2
The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date (use "vagrant global-status --prune" to prune invalid
entries). To interact with any of the machines, you can go to that
directory and run Vagrant, or you can use the ID directly with
Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"
Get hands-on with 1200+ tech skills courses.