Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make SSH go directly to specific directory?

Tags:

when you do an "ssh second_machine" you are able to connect to second_machine on your home directory

But usually i am working in my_machine in directory with very long path, and i want to connect to second_machine and move to my working directory right away. So everytime i have to:

ssh second_machine
cd /very/long/path/to/directory/

Is there a way to make it automatic ?? ( ssh automatically go to the desired directory )

like image 743
Debugger Avatar asked Apr 07 '10 15:04

Debugger


People also ask

Can you SSH to a specific directory?

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.

How do I find directory in SSH?

To list all files and directories using an SSH client, you would need to execute the appropriate command. The command name, in this case, is ls and it accepts various parameters. the output will be all visible files and folders without additional formatting or information.

What is SSH config file?

Your SSH config file allows you to define specific settings for each SSH host that makes connecting to that host far easier. By defining many of these common, or uncommon, properties within the file, it eliminates the need to remember this parameter set each and every time a connection is needed.


2 Answers

This should work for you

ssh  -t second_machine "cd /very/long/path/to/directory/; bash" 

Assumes you're wanting to run bash, substitute for a different shell if required.

like image 166
Garethr Avatar answered Oct 27 '22 19:10

Garethr


To make it permanent, use RemoteCommand in your ~/.ssh/config file, e.g.

Host myhost   HostName IP   User ubuntu   IdentityFile ~/.ssh/id_rsa   RemoteCommand cd /path/to/directory; $SHELL -il 

Related:

  • SSH Config File Alias To Get To a Directory On Server
  • How can I automatically change directory on ssh login?
  • Run a remote command using ssh config file
like image 31
kenorb Avatar answered Oct 27 '22 20:10

kenorb