I have a script that starts my vagrant machine, opens up multiple terminals and connects to the vagrant machine via ssh in every newly opened terminal. My problem is that I need about five terminals, and I don't want to type in the password for each terminal manually. Is there a way to get prompted for the password only once in the main terminal, and use the same password for the ssh command?

cd /home/kkri/public_html/freitag/vagrant
vagrant up
for run in $(seq 1 $1)
  gnome-terminal --window-with-profile=dark -e "ssh vagrant@localhost -p 2222" --$
gnome-terminal --window-with-profile=git          
echo "~~~ Have fun! ~~~"
  • 12
    Why don't you set up passwordless access and use ssh keys instead? That's both more secure and simpler. Would that be an acceptable solution?
    – terdon
    Commented Sep 5, 2016 at 9:16
  • As terdon said, you can use a private/public SSH key; this will also make actions like scp more comfortable without compromising security. The following link provides a HowTo that should be applicable to your remote system, too. If you trust your environment enough (firewalled intranet) you can even generate a key without passphrase. linode.com/docs/security/use-public-key-authentication-with-ssh
    – Murphy
    Commented Sep 5, 2016 at 9:35
  • 3
    There is such as cssh and pssh (Cluster SSH respectively Parallel SSH) Commented Sep 5, 2016 at 9:36
  • As an aside: Why use ssh vagrant@localhost -p 2222 rather than vagrant ssh? The latter is better as it will correct the port if you have more then one vagrant box running. Commented Sep 5, 2016 at 9:53
  • Use vagrant ssh!! Commented Sep 5, 2016 at 11:08

3 Answers 3


In general (ignoring vagrant or other system-specific details) your best bet is to set up authentication with SSH keys, and run ssh-agent. Then open the ssh sessions with something like:

# load the key to the agent with a 10 s timeout
# this asks for the key passphrase
ssh-add -t10  ~/.ssh/id_rsa  
for x in 1 2 3 ; do 
    ssh .... 

Or, if you can't use keys, you could rig something up with sshpass.

read -p "Enter password: " -s SSHPASS ; echo
for x in 1 2 3 ; do 
    sshpass -e ssh ...

Though with the terminal in the middle, this would leave the password set in the terminal's environment. To work around that, you could save the password temporarily in a file:

read -p "Enter password: " -s SSHPASS ; echo
cat <<< "$SSHPASS" > "$PWFILE"
for x in 1 2 3 ; do 
    sshpass -f "$PWFILE" ssh ...
shred --remove "$PWFILE"

This still isn't optimal since there's a chance the password hits the disk, so keys would be better.


Since you are using vagrant you can simply run vagrant ssh to ssh to the box without having to enter a password. This will also handle port collisions if you have more then one vagrant box running and various other things.

Alternatively you can use tmux instead of gnome-terminals and make use of its synchronize-panes feature:

tmux new -s $tmux_name -d
for run in $(seq 1 $1); do
    tmux splitw -t $tmux_name -v "ssh vagrant@localhost -p 2222"
    tmux select-layout -t $tmux_name tiled
tmux kill-pane -t 1
tmux set-window-option -t $tmux_name synchronize-panes on
tmux select-layout -t $tmux_name tiled
tmux new-window -t $tmux_name
tmux kill-window
tmux attach -t $tmux_name

This will launch tmux and run the ssh command in the number of panes that you have specified, with all the panes synchronized (tmux forwards keys to all the panes). Once you have done what you want to all the panes you can press ctrl+b then :set synchronize-panes off to turn off the input sync. This can be bound to a key for convenience.

  • 1
    tmux seems like a good idea, however, I'm wondering why you would want multiple SSH connections instead of simply one connection and running tmux on the vagrant machine?
    – tmh
    Commented Sep 5, 2016 at 18:26
  • That also works, assuming you have it installed inside vagrant - not always the case. You also get to use your tmux config if you have modified it at all without needing to copy it to every vagrant box you provision. This script was also adapted from a script I used to tmux to multiple different hosts so is more flexible in that regard. Commented Sep 6, 2016 at 9:24
  • Also, you can modify the script to start some panes not inside vagrant and have them all managed by one tmux session. Commented Sep 6, 2016 at 9:25

The approach would be to use key authentication, it is safer, and solves the authentication problem.

Just generate a key pair with:

ssh-keygen -t rsa

And copy/paste the content of ~/.ssh/id_rsa.pub file to the ~/.authorized_keys in the server machine. If the file doesn't exist, check the permissions to user only.

If you use multiples terminals I recommend terminator, it actually solves this kind of problem with broadcast keybord to all terminals features

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .