Have you ever been in a situation where you wanted to SSH to a remote server and immediately cd into a directory? Yes? Well, you're on the right track! This brief tutorial describes how to directly SSH into a particular directory on Linux. Meaning - we can automatically change to a particular directory when log in to a remote system via SSH. Not just SSH into a specific directory, it is even possible run any command immediately right after connecting to the remote server over SSH.
Table of Contents
SSH Into A Particular Directory Of A Remote Linux System
Before I knew this method, I would usually first SSH into the remote remote system using command:
$ ssh user@remote-system
And then cd into a directory like below:
$ cd <some-directory>
That's how I change in to a directory in a remote system. Not anymore! No need to use two separate commands!!
For example, the following command allows me to SSH into a remote system (192.168.225.52), and automatically cd into a directory called "dir1":
$ ssh -t ostechnix@192.168.225.52 'cd /home/ostechnix/dir1 ; bash'
The above command will SSH into a remote system (192.168.225.52) and immediately cd into a directory named '/home/ostechnix/dir1' directory and finally leave you at the remote system's shell.
Here, the -t flag is used to force pseudo-terminal allocation, which is necessary for an interactive shell. If you don't specify this flag, then no prompt will appear. And also if you don't add " bash" at the end of the above command, the connection will get terminated and return control to the local system.
Here is the sample output of the above command:
Here are a few more example commands to SSH into a particular directory:
$ ssh -t ostechnix@192.168.225.52 'cd /home/ostechnix/dir1 ; bash --login'
Here, the --login flag sets "bash" as login shell.
Or,
$ ssh -t ostechnix@192.168.225.52 'cd /home/ostechnix/dir1 ; exec bash'
Or,
$ ssh -t ostechnix@192.168.225.52 'cd /home/ostechnix/dir1 && exec bash -l'
Here, the -l flag sets "bash" as login shell.
In the above examples, I have explicitly mentioned bash as login shell. Because, I know that Bash is is the default shell in my remote system. If you don't know the shell type on the remote system, the use the following command:
$ ssh -t ostechnix@192.168.225.52 'cd /home/ostechnix/dir1 && exec $SHELL'
Create a directory on remote system and automatically SSH into it
If you try to SSH into a directory that doesn't exists in the remote system, you will see the following message upon successful authentication:
bash: line 0: cd: /home/ostechnix/dir2: No such file or directory
As you can see, the directory named "dir2" is not available in the remote system. If you want to create a directory in a remote system and automatically cd into it over SSH, simply run the following command from your local system:
$ ssh -t ostechnix@192.168.225.52 'mkdir -p dir2 ; cd /home/ostechnix/dir2 ; bash --login'
Making the changes permanent
If you don't want to type the above commands manually every time, just add the command(s) you wanted to run after connecting to an SSH server on the remote system's ~/.bashrc or ~/.bash_profile or ~/.profile files.
Edit bashrc file:
$ nano ~/.bashrc
Add the command(s) one by one. In my case, I am adding the following line:
cd /home/ostechnix/dir1 >& /dev/null
Please note that you should add this line on the remote system's .bash_profile or .bashrc or .profile file, not in your local system's. Press Ctrl+O and hit ENTER to save and press Ctrl+X to close the file.
Finally, run the following command to update the changes:
$ source ~/.bashrc
From now on, whenever you login (either via SSH or direct), the cd command will execute and you will automatically land inside the "/home/ostechnix/dir1" directory.
Execute commands over SSH on remote Linux systems
Like I already said, this trick is not just to cd into directory after connecting to a remote system. You can also use this method to run other commands as well.
For example, the following command will land you inside '/home/ostechnix/dir1' directory and then execute 'uname -a' command.
$ ssh -t ostechnix@192.168.225.52 'cd /home/ostechnix/dir1 && uname -a && exec $SHELL'
More examples on executing commands over SSH on remote systems are given in the following guide.
Hope this helps.
Related read: