Have you ever ever been in a state of affairs the place you wish to SSH to a distant server and instantly cd right into a listing and proceed work interactively? You’re heading in the right direction! This temporary tutorial describes methods to immediately SSH into a specific listing of a distant Linux system. Not simply SSH into a selected listing, you’ll be able to run any command instantly proper after connecting to an SSH server as described on this information. It isn’t that troublesome as you may suppose. Learn on.
SSH Into A Explicit Listing Of A Distant System
Earlier than I knew this methodology, I’d normally first SSH to the distant distant system utilizing command:
$ ssh [email protected]
After which cd right into a listing like under:
Nonetheless, you needn’t to make use of two separate instructions. You possibly can mix these instructions and simplify the duty with one command.
Take a look on the following instance.
$ ssh -t [email protected] ‘cd /residence/sk/ostechnix ; bash’
The above command will SSH right into a distant system (192.168.225.22) and instantly cd right into a listing named ‘/residence/sk/ostechnix/’ listing and go away your self on the immediate.
Right here, the -t flag is used to power pseudo-terminal allocation, which is critical or an interactive shell.
Right here is the pattern output of the above command:
You can even use this command as properly.
$ ssh -t [email protected] ‘cd /residence/sk/ostechnix ; exec bash’
$ ssh -t [email protected] ‘cd /residence/sk/ostechnix && exec bash -l’
Right here, the -l flag units the bash as login shell.
Within the above instance, I’ve used bash within the final argument. It’s the default shell in my distant system. In case you don’t know the shell kind on the distant system, use the next command:
$ ssh -t [email protected] ‘cd /residence/sk/ostechnix && exec $SHELL’
Like I already mentioned, this isn’t only for cd into listing after connecting to an distant system. You need to use this trick to run different instructions as properly. For instance, the next command will land you inside ‘/residence/sk/ostechnix/’ listing after which execute ‘uname -a’ command.
$ ssh -t [email protected] ‘cd /residence/sk/ostechnix && uname -a && exec $SHELL’
Alternatively, you’ll be able to add the command(s) you wished to run after connecting to an SSH server on the distant system’s .bash_profile file.
Edit .bash_profile file:
$ nano ~/.bash_profile
Add the command(s) one after the other. In my case, I’m including the next line:
cd /residence/sk/ostechnix >& /dev/null
Save and shut the file. Lastly, run the next command to replace the adjustments.
$ supply ~/.bash_profile
Please be aware that it is best to add this line on the distant system’s .bash_profile or .bashrc file, not in your native system’s. Any further, everytime you login (whether or not by SSH or direct), the cd command will execute and you’ll be routinely landed inside “/residence/sk/ostechnix/” listing.
And, that’s all for now. Hope this was helpful. Extra good stuffs to return. Keep tuned!
Thanks for stopping by!
Assist us that can assist you:
Have a Good day!!