How To SSH Into A Particular Directory On Linux

0 81

Have you ever been in a situation where you want to SSH to a remote server and immediately cd into a directory and continue work interactively? You’re on the right track! This brief tutorial describes how to directly SSH into a particular directory of a remote Linux system. Not just SSH into a specific directory, you can run any command immediately right after connecting to an SSH server as described in this guide. It is not that difficult as you might think. Read on.

SSH Into A Particular Directory Of A Remote System

Before I knew this method, I would usually first SSH to the remote remote system using command:

$ ssh [email protected]

And then cd into a directory like below:

$ cd 

However, you need not to use two separate commands. You can combine these commands and simplify the task with one command.

Have a look at the following example.

$ ssh -t [email protected] 'cd /home/sk/ostechnix ; bash'

The above command will SSH into a remote system ( and immediately cd into a directory named ‘/home/sk/ostechnix/’ directory and leave yourself at the prompt.

Here, the -t flag is used to force pseudo-terminal allocation, which is necessary or an interactive shell.

Here is the sample output of the above command:

SSH Into A Particular Directory

Directly SSH Into A Particular Directory

You can also use this command as well.

$ ssh -t [email protected] 'cd /home/sk/ostechnix ; exec bash'


$ ssh -t [email protected] 'cd /home/sk/ostechnix && exec bash -l'

Here, the -l flag sets the bash as login shell.

In the above example, I have used bash in the last argument. It is the default shell in my remote system. If you don’t know the shell type on the remote system, use the following command:

$ ssh -t [email protected] 'cd /home/sk/ostechnix && exec $SHELL'

Like I already said, this is not just for cd into directory after connecting to an remote system. You can use this trick to run other commands as well. For example, the following command will land you inside ‘/home/sk/ostechnix/’ directory and then execute ‘uname -a’ command.

$ ssh -t [email protected] 'cd /home/sk/ostechnix && uname -a && exec $SHELL'

Alternatively, you can add the command(s) you wanted to run after connecting to an SSH server on the remote system’s .bash_profile file.

Edit .bash_profile file:

 $ nano ~/.bash_profile

Add the command(s) one by one. In my case, I am adding the following line:

cd /home/sk/ostechnix >& /dev/null

Save and close the file. Finally, run the following command to update the changes.

 $ source ~/.bash_profile

Please note that you should add this line on the remote system’s .bash_profile or .bashrc file, not in your local system’s. From now on, whenever you login (whether by SSH or direct), the cd command will execute and you will be automatically landed inside “/home/sk/ostechnix/” directory.

Suggested read:

And, that’s all for now. Hope this was useful. More good stuffs to come. Stay tuned!


Source : Visit

You might also like More from author

Leave A Reply

Your email address will not be published.