Search This Blog

Saturday, February 26, 2011

login into a directory after sshing into a machine

To login into a machine via ssh and cd into a particular directory immediately afterwards, use
ssh -t 'cd ~/destination/directory && exec /bin/bash --login -i'
The -t option forces the ssh to open a pseudo-terminal. Without the -t option, ssh does not open the pseudo-terminal when a command is specified.

Commands can also contain variable names. For example,
export DYNAMIC=${PWD#$HOME/}
ssh -t "cd ~/$DYNAMIC && exec bash --login -i"
Other possible uses of the -t option

To see the output of top of a remote machine, use
ssh -t top

No comments: