How can I ssh directly to a particular directory?

前端 未结 12 2071
情话喂你
情话喂你 2020-12-07 07:03

I often have to login to one of several servers and go to one of several directories on those machines. Currently I do something of this sort:

localhost ~]$ ssh          


        
相关标签:
12条回答
  • 2020-12-07 07:34

    Based on additions to @rogeriopvl's answer, I suggest the following:

    ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted && bash"
    

    Chaining commands by && will make the next command run only when the previous one was successful (as opposed to using ;, which executes commands sequentially). This is particularly useful when needing to cd to a directory performing the command.

    Imagine doing the following:

    /home/me$ cd /usr/share/teminal; rm -R *
    

    The directory teminal doesn't exist, which causes you to stay in the home directory and remove all the files in there with the following command.

    If you use &&:

    /home/me$ cd /usr/share/teminal && rm -R *
    

    The command will fail after not finding the directory.

    0 讨论(0)
  • 2020-12-07 07:38

    Another way of going to directly after logging in is create "Alias". When you login into your system just type that alias and you will be in that directory.

    Example : Alias = myfolder '/var/www/Folder'

    After you log in to your system type that alias (this works from any part of the system)
    this command if not in bashrc will work for current session. So you can also add this alias to bashrc to use that in future

    $ myfolder => takes you to that folder

    0 讨论(0)
  • 2020-12-07 07:38

    simply modify your home with the command: usermod -d /newhome username

    0 讨论(0)
  • 2020-12-07 07:40

    In my very specific case, I just wanted to execute a command in a remote host, inside a specific directory from a Jenkins slave machine:

    ssh myuser@mydomain
    cd /home/myuser/somedir 
    ./commandThatMustBeRunInside_somedir
    exit
    

    But my machine couldn't perform the ssh (it couldn't allocate a pseudo-tty I suppose) and kept me giving the following error:

    Pseudo-terminal will not be allocated because stdin is not a terminal
    

    I could get around this issue passing "cd to dir + my command" as a parameter of the ssh command (to not have to allocate a Pseudo-terminal) and by passing the option -T to explicitly tell to the ssh command that I didn't need pseudo-terminal allocation.

    ssh -T myuser@mydomain "cd /home/myuser/somedir; ./commandThatMustBeRunInside_somedir"
    
    0 讨论(0)
  • 2020-12-07 07:42

    SSH itself provides a means of communication, it does not know anything about directories. Since you can specify which remote command to execute (this is - by default - your shell), I'd start there.

    0 讨论(0)
  • 2020-12-07 07:44

    going one step further with the -t idea. I keep a set of scripts calling the one below to go to specific places in my frequently visited hosts. I keep them all in ~/bin and keep that directory in my path.

    #!/bin/bash
    
    # does ssh session switching to particular directory
    # $1, hostname from config file 
    # $2, directory to move to after login
    # can save this as say 'con' then
    # make another script calling this one, e.g.
    # con myhost repos/i2c
    
    ssh -t $1 "cd $2; exec \$SHELL --login"
    
    0 讨论(0)
提交回复
热议问题