Executing a remote command

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite
2












  1. I can't edit the .bashrc of the fs I'm connecting to


  2. I have to use the ~/.ssh/config file


Here is my current config:



Host my-ssh
HostName <ip>
User <user>
IdentityFile <file location>
Compression yes
RemoteCommand cd <path to folder>


When I run ssh my-ssh nothing happens. The connection seems to automatically close. If I remove the RemoteCommand line it connects without an issue.



Is this something on the server config? It's an EC2 instance, either CentOS or RHEL and bash is the shell.







share|improve this question






















  • The list you've entered is in a desktop shortcut? man ssh to find out how to specify those items on a command line. Is this homework?
    – Xalorous
    Jan 23 at 21:56














up vote
4
down vote

favorite
2












  1. I can't edit the .bashrc of the fs I'm connecting to


  2. I have to use the ~/.ssh/config file


Here is my current config:



Host my-ssh
HostName <ip>
User <user>
IdentityFile <file location>
Compression yes
RemoteCommand cd <path to folder>


When I run ssh my-ssh nothing happens. The connection seems to automatically close. If I remove the RemoteCommand line it connects without an issue.



Is this something on the server config? It's an EC2 instance, either CentOS or RHEL and bash is the shell.







share|improve this question






















  • The list you've entered is in a desktop shortcut? man ssh to find out how to specify those items on a command line. Is this homework?
    – Xalorous
    Jan 23 at 21:56












up vote
4
down vote

favorite
2









up vote
4
down vote

favorite
2






2





  1. I can't edit the .bashrc of the fs I'm connecting to


  2. I have to use the ~/.ssh/config file


Here is my current config:



Host my-ssh
HostName <ip>
User <user>
IdentityFile <file location>
Compression yes
RemoteCommand cd <path to folder>


When I run ssh my-ssh nothing happens. The connection seems to automatically close. If I remove the RemoteCommand line it connects without an issue.



Is this something on the server config? It's an EC2 instance, either CentOS or RHEL and bash is the shell.







share|improve this question














  1. I can't edit the .bashrc of the fs I'm connecting to


  2. I have to use the ~/.ssh/config file


Here is my current config:



Host my-ssh
HostName <ip>
User <user>
IdentityFile <file location>
Compression yes
RemoteCommand cd <path to folder>


When I run ssh my-ssh nothing happens. The connection seems to automatically close. If I remove the RemoteCommand line it connects without an issue.



Is this something on the server config? It's an EC2 instance, either CentOS or RHEL and bash is the shell.









share|improve this question













share|improve this question




share|improve this question








edited Jan 24 at 11:43









Jeff Schaller

31.7k847107




31.7k847107










asked Jan 23 at 21:49









Robbie Milejczak

1286




1286











  • The list you've entered is in a desktop shortcut? man ssh to find out how to specify those items on a command line. Is this homework?
    – Xalorous
    Jan 23 at 21:56
















  • The list you've entered is in a desktop shortcut? man ssh to find out how to specify those items on a command line. Is this homework?
    – Xalorous
    Jan 23 at 21:56















The list you've entered is in a desktop shortcut? man ssh to find out how to specify those items on a command line. Is this homework?
– Xalorous
Jan 23 at 21:56




The list you've entered is in a desktop shortcut? man ssh to find out how to specify those items on a command line. Is this homework?
– Xalorous
Jan 23 at 21:56










3 Answers
3






active

oldest

votes

















up vote
7
down vote



accepted










If you specify a remote command, then the ssh connection is going to close as soon as the remote command exits. A cd command will exit almost immediately.



A common way to do what you want is:



RemoteCommand cd /some/path && bash


(Substitute your desired shell in place of "bash"). This cd's to the path and then invokes a subshell if the cd operation succeeded. The ssh connection will close when the subshell exits.



You will also want to force ssh to allocate a PTY for the session:



RequestTTY yes


If you don't, then ssh won't request one by default, and you'll get a non-interactive shell session. Notably, you won't get a command prompt.






share|improve this answer



























    up vote
    3
    down vote













    If you have a RemoteCommand specified it is the same as the following:



    ssh user@host <command>


    What happens is that ssh connects to the host, runs the command and exits, and returns the exit status of the command as the exit status of ssh.



    From the man page:




    If command is specified, it is executed on the remote host instead of a login shell.




    So you are logging into the host, changing directory and then ssh is exiting. I doubt this is what you want. Remote command is non-interactive, i.e. to run one specific command on a remote machine.






    share|improve this answer





























      up vote
      -2
      down vote













      Try to run ssh -t my-ssh. You may need to force pseudo terminal allocation which is ommitted by the config in ~/.ssh/config.






      share|improve this answer




















      • this is not complete, as the asker needs also to do something more than just issuing a cd ... (which then exits, ending the session)
        – thrig
        Jan 23 at 22:17










      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f419198%2fexecuting-a-remote-command%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      7
      down vote



      accepted










      If you specify a remote command, then the ssh connection is going to close as soon as the remote command exits. A cd command will exit almost immediately.



      A common way to do what you want is:



      RemoteCommand cd /some/path && bash


      (Substitute your desired shell in place of "bash"). This cd's to the path and then invokes a subshell if the cd operation succeeded. The ssh connection will close when the subshell exits.



      You will also want to force ssh to allocate a PTY for the session:



      RequestTTY yes


      If you don't, then ssh won't request one by default, and you'll get a non-interactive shell session. Notably, you won't get a command prompt.






      share|improve this answer
























        up vote
        7
        down vote



        accepted










        If you specify a remote command, then the ssh connection is going to close as soon as the remote command exits. A cd command will exit almost immediately.



        A common way to do what you want is:



        RemoteCommand cd /some/path && bash


        (Substitute your desired shell in place of "bash"). This cd's to the path and then invokes a subshell if the cd operation succeeded. The ssh connection will close when the subshell exits.



        You will also want to force ssh to allocate a PTY for the session:



        RequestTTY yes


        If you don't, then ssh won't request one by default, and you'll get a non-interactive shell session. Notably, you won't get a command prompt.






        share|improve this answer






















          up vote
          7
          down vote



          accepted







          up vote
          7
          down vote



          accepted






          If you specify a remote command, then the ssh connection is going to close as soon as the remote command exits. A cd command will exit almost immediately.



          A common way to do what you want is:



          RemoteCommand cd /some/path && bash


          (Substitute your desired shell in place of "bash"). This cd's to the path and then invokes a subshell if the cd operation succeeded. The ssh connection will close when the subshell exits.



          You will also want to force ssh to allocate a PTY for the session:



          RequestTTY yes


          If you don't, then ssh won't request one by default, and you'll get a non-interactive shell session. Notably, you won't get a command prompt.






          share|improve this answer












          If you specify a remote command, then the ssh connection is going to close as soon as the remote command exits. A cd command will exit almost immediately.



          A common way to do what you want is:



          RemoteCommand cd /some/path && bash


          (Substitute your desired shell in place of "bash"). This cd's to the path and then invokes a subshell if the cd operation succeeded. The ssh connection will close when the subshell exits.



          You will also want to force ssh to allocate a PTY for the session:



          RequestTTY yes


          If you don't, then ssh won't request one by default, and you'll get a non-interactive shell session. Notably, you won't get a command prompt.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 23 at 22:14









          Kenster

          1,3281611




          1,3281611






















              up vote
              3
              down vote













              If you have a RemoteCommand specified it is the same as the following:



              ssh user@host <command>


              What happens is that ssh connects to the host, runs the command and exits, and returns the exit status of the command as the exit status of ssh.



              From the man page:




              If command is specified, it is executed on the remote host instead of a login shell.




              So you are logging into the host, changing directory and then ssh is exiting. I doubt this is what you want. Remote command is non-interactive, i.e. to run one specific command on a remote machine.






              share|improve this answer


























                up vote
                3
                down vote













                If you have a RemoteCommand specified it is the same as the following:



                ssh user@host <command>


                What happens is that ssh connects to the host, runs the command and exits, and returns the exit status of the command as the exit status of ssh.



                From the man page:




                If command is specified, it is executed on the remote host instead of a login shell.




                So you are logging into the host, changing directory and then ssh is exiting. I doubt this is what you want. Remote command is non-interactive, i.e. to run one specific command on a remote machine.






                share|improve this answer
























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  If you have a RemoteCommand specified it is the same as the following:



                  ssh user@host <command>


                  What happens is that ssh connects to the host, runs the command and exits, and returns the exit status of the command as the exit status of ssh.



                  From the man page:




                  If command is specified, it is executed on the remote host instead of a login shell.




                  So you are logging into the host, changing directory and then ssh is exiting. I doubt this is what you want. Remote command is non-interactive, i.e. to run one specific command on a remote machine.






                  share|improve this answer














                  If you have a RemoteCommand specified it is the same as the following:



                  ssh user@host <command>


                  What happens is that ssh connects to the host, runs the command and exits, and returns the exit status of the command as the exit status of ssh.



                  From the man page:




                  If command is specified, it is executed on the remote host instead of a login shell.




                  So you are logging into the host, changing directory and then ssh is exiting. I doubt this is what you want. Remote command is non-interactive, i.e. to run one specific command on a remote machine.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 23 at 22:10

























                  answered Jan 23 at 22:05









                  datUser

                  2,2811032




                  2,2811032




















                      up vote
                      -2
                      down vote













                      Try to run ssh -t my-ssh. You may need to force pseudo terminal allocation which is ommitted by the config in ~/.ssh/config.






                      share|improve this answer




















                      • this is not complete, as the asker needs also to do something more than just issuing a cd ... (which then exits, ending the session)
                        – thrig
                        Jan 23 at 22:17














                      up vote
                      -2
                      down vote













                      Try to run ssh -t my-ssh. You may need to force pseudo terminal allocation which is ommitted by the config in ~/.ssh/config.






                      share|improve this answer




















                      • this is not complete, as the asker needs also to do something more than just issuing a cd ... (which then exits, ending the session)
                        – thrig
                        Jan 23 at 22:17












                      up vote
                      -2
                      down vote










                      up vote
                      -2
                      down vote









                      Try to run ssh -t my-ssh. You may need to force pseudo terminal allocation which is ommitted by the config in ~/.ssh/config.






                      share|improve this answer












                      Try to run ssh -t my-ssh. You may need to force pseudo terminal allocation which is ommitted by the config in ~/.ssh/config.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 23 at 22:10









                      grinnan

                      1




                      1











                      • this is not complete, as the asker needs also to do something more than just issuing a cd ... (which then exits, ending the session)
                        – thrig
                        Jan 23 at 22:17
















                      • this is not complete, as the asker needs also to do something more than just issuing a cd ... (which then exits, ending the session)
                        – thrig
                        Jan 23 at 22:17















                      this is not complete, as the asker needs also to do something more than just issuing a cd ... (which then exits, ending the session)
                      – thrig
                      Jan 23 at 22:17




                      this is not complete, as the asker needs also to do something more than just issuing a cd ... (which then exits, ending the session)
                      – thrig
                      Jan 23 at 22:17












                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f419198%2fexecuting-a-remote-command%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay