Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vagrant ssh --command not working

I'm trying to pass a simple command to the vagrant machine.

vagrant ssh -c "mysql -u root -e'CREATE DATABASE testing';"

The db doesn't get created. Instead I just get 'logged in' to the vagrant machine via ssh.

The same thing happens when I run

vagrant ssh -c "touch test.txt"

I've also tried with:

  • single quotes around command
  • --command instead of shorthand -c

Any ideas what's going on?

Edit

I'm going to distribute my script and it's going to integrate my script with Homestead, so can't really tweak the vagrant configuration.

Edit

Since I'm using Homestead the user and pw is different. It should have been

vagrant ssh -c "mysql -u homestead -psecret -e'CREATE DATABASE testing;'"

not changing the code above to avoid confusion with already submitted answers.

But nonetheless, this does not solve the problem. The same thing is happening as described above.

like image 861
John_911 Avatar asked Nov 12 '16 20:11

John_911


People also ask

How do I connect my vagrant box to SSH?

Simply CMD-SHIFT-P then "Remote-SSH: Connect to Host..." and the ssh . config entry you just added is automatically listed - you simply select it and voila, vscode connects to your remote vagrant vm!

What is the command to connect to a running virtual machine using vagrant?

Command: vagrant ssh [name|id] [-- extra_ssh_args] This will SSH into a running Vagrant machine and give you access to a shell.

What is the command to change into vagrant box?

Command: vagrant box update This command updates the box for the current Vagrant environment if there are updates available. The command can also update a specific box (outside of an active Vagrant environment), by specifying the --box flag. Note that updating the box will not update an already-running Vagrant machine.


1 Answers

This works

vagrant ssh -- -t 'touch test.txt'

The flag -t creates a pseudo-tty for bash to use. Which is a variation on the answer to this question.

Running remote commands after vagrant ssh

Which is explained in more detail here.

https://unix.stackexchange.com/questions/119894/single-command-to-login-to-ssh-and-run-program/119899#119899

like image 116
Keith John Hutchison Avatar answered Sep 28 '22 01:09

Keith John Hutchison