Username and password in command line with sshfs












10















I'm creating a small backup script using sshfs:



sshfs backup_user@target_ip:/home /mnt/backup


Is there a way to include the password in this command?



Or is there another file transfer solution where the login password can be included other than FTP/SFTP?










share|improve this question




















  • 4





    You can use ssh-gkeygen to generate an RSA key pair then configure ssh (client and server) to use RSA authentication. Have you done this before for "regular" ssh authentication?

    – airhuff
    Jan 15 '17 at 0:22











  • yes but then I have to change the way servers are connecting via ssh .. as far I know authentication can be key based or login/password not both at the same time..

    – Zaza
    Jan 15 '17 at 0:27






  • 2





    They can be both. They usually are. This way, a newly created user can push their key on the server by entering their password. Subsequently, they use their key.

    – xhienne
    Jan 15 '17 at 0:28


















10















I'm creating a small backup script using sshfs:



sshfs backup_user@target_ip:/home /mnt/backup


Is there a way to include the password in this command?



Or is there another file transfer solution where the login password can be included other than FTP/SFTP?










share|improve this question




















  • 4





    You can use ssh-gkeygen to generate an RSA key pair then configure ssh (client and server) to use RSA authentication. Have you done this before for "regular" ssh authentication?

    – airhuff
    Jan 15 '17 at 0:22











  • yes but then I have to change the way servers are connecting via ssh .. as far I know authentication can be key based or login/password not both at the same time..

    – Zaza
    Jan 15 '17 at 0:27






  • 2





    They can be both. They usually are. This way, a newly created user can push their key on the server by entering their password. Subsequently, they use their key.

    – xhienne
    Jan 15 '17 at 0:28
















10












10








10


5






I'm creating a small backup script using sshfs:



sshfs backup_user@target_ip:/home /mnt/backup


Is there a way to include the password in this command?



Or is there another file transfer solution where the login password can be included other than FTP/SFTP?










share|improve this question
















I'm creating a small backup script using sshfs:



sshfs backup_user@target_ip:/home /mnt/backup


Is there a way to include the password in this command?



Or is there another file transfer solution where the login password can be included other than FTP/SFTP?







linux ssh sshfs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 13 '17 at 0:57









Timothy Martin

5,2442129




5,2442129










asked Jan 15 '17 at 0:10









ZazaZaza

1452213




1452213








  • 4





    You can use ssh-gkeygen to generate an RSA key pair then configure ssh (client and server) to use RSA authentication. Have you done this before for "regular" ssh authentication?

    – airhuff
    Jan 15 '17 at 0:22











  • yes but then I have to change the way servers are connecting via ssh .. as far I know authentication can be key based or login/password not both at the same time..

    – Zaza
    Jan 15 '17 at 0:27






  • 2





    They can be both. They usually are. This way, a newly created user can push their key on the server by entering their password. Subsequently, they use their key.

    – xhienne
    Jan 15 '17 at 0:28
















  • 4





    You can use ssh-gkeygen to generate an RSA key pair then configure ssh (client and server) to use RSA authentication. Have you done this before for "regular" ssh authentication?

    – airhuff
    Jan 15 '17 at 0:22











  • yes but then I have to change the way servers are connecting via ssh .. as far I know authentication can be key based or login/password not both at the same time..

    – Zaza
    Jan 15 '17 at 0:27






  • 2





    They can be both. They usually are. This way, a newly created user can push their key on the server by entering their password. Subsequently, they use their key.

    – xhienne
    Jan 15 '17 at 0:28










4




4





You can use ssh-gkeygen to generate an RSA key pair then configure ssh (client and server) to use RSA authentication. Have you done this before for "regular" ssh authentication?

– airhuff
Jan 15 '17 at 0:22





You can use ssh-gkeygen to generate an RSA key pair then configure ssh (client and server) to use RSA authentication. Have you done this before for "regular" ssh authentication?

– airhuff
Jan 15 '17 at 0:22













yes but then I have to change the way servers are connecting via ssh .. as far I know authentication can be key based or login/password not both at the same time..

– Zaza
Jan 15 '17 at 0:27





yes but then I have to change the way servers are connecting via ssh .. as far I know authentication can be key based or login/password not both at the same time..

– Zaza
Jan 15 '17 at 0:27




2




2





They can be both. They usually are. This way, a newly created user can push their key on the server by entering their password. Subsequently, they use their key.

– xhienne
Jan 15 '17 at 0:28







They can be both. They usually are. This way, a newly created user can push their key on the server by entering their password. Subsequently, they use their key.

– xhienne
Jan 15 '17 at 0:28












6 Answers
6






active

oldest

votes


















13














According to the manual, there is an option -o password_stdin which might allow to read the password from standard input, which can probably be a redirection. I have never used it, so I'm speculating.



That said, I strongly advise against such a solution which is inherently insecure.



ssh works very well with a system of private/public keys. It is simple and secure. No need to enter a password or to write it in clear in a shell script. Just push your public key on the server and you can connect immediately.






share|improve this answer
























  • Can anyone provide a working example of this solution? I'm trying the following, but it is not working: printf "my_passwordn" | sshfs username@hostname:/remote_fs $HOME/local_dir

    – MikeyE
    Jul 10 '18 at 12:42













  • FYI, I should have mentioned I'm using a password protected SSH key. I was able to get it working using the answer provided by @nathan-s-watson-haigh found here: unix.stackexchange.com/questions/128974/…

    – MikeyE
    Jul 10 '18 at 13:07











  • I don't have any matching environment at hand that would allow me to test a ssfs-mount with a password protected SSH key. Anyway, what strikes me at first in your command is that I don't see any -o password_stdin option.

    – xhienne
    Jul 10 '18 at 14:09











  • Thanks for the input. I tried the following command just now, and it does not ask for a password, but it also does not return. Meaning, it just sits there waiting for the command to complete, no command prompt displayed. I tried the command: printf "my_passwordn" | sshfs -o password_stdin user@hostname:/ $HOME/local_dir P.S. I'm on Debian Buster P.P.S. This works, but prompts for a password: sshfs -o password_stdin user@hostname:/ $HOME/local_dir

    – MikeyE
    Jul 13 '18 at 16:32





















6














-o password_stdin do not seem to be working on all systems, for instance freeBSD. etc.


You can also use expect Interpreter, it should work with sshfs
and should do the trick.


Another solution would be sshpass, for instance, let say your are backing up directory /var/www



Backing up:



name=$(date '+%y-%m-%d')
mkdir /backup/$name && tar -czvf /backup/$name/"$name.tar.gz" /var/www


uploading backup file to backup server



sshpass -p "your_password" scp -r backup_user@target_ip:/home/ /backup/$name


So it will upload directory with today's backup


But still, as it was said higher, best(safe and simple) way would be to use ssh key pair

The only inconvenience would be that you have to go through the key generation process once on every server you need to pair, but it is better than keeping a password in plain text format on all servers you want to back up :),

Generating a Key Pair the Proper way





  • On Local server



    ssh-keygen -t rsa



  • On remote Server



    ssh root@remote_servers_ip "mkdir -p .ssh"



  • Uploading Generated Public Keys to the Remote Server



    cat ~/.ssh/id_rsa.pub | ssh root@remote_servers_ip "cat >> ~/.ssh/authorized_keys"



  • Set Permissions on Remote server



    ssh root@remote_servers_ip "chmod 700 ~/.ssh; chmod 640 ~/.ssh/authorized_keys"



  • Login



    ssh root@remote_servers_ip



  • Enabling SSH Protocol v2




    uncomment "Protocol 2" in /etc/ssh/sshd_config





  • enabling public key authorization in sshd




    uncomment "PubkeyAuthentication yes" in /etc/ssh/sshd_config





  • If StrictModes is set to yes in /etc/ssh/sshd_config then



    restorecon -Rv ~/.ssh







share|improve this answer

































    3














    Piping the "sshfs password" with <<< to -o password_stdin works on bash:



    sshfs -o password_stdin backup_user@target_ip:/home /mnt/backup <<< "sshfs password"





    share|improve this answer


























    • This should be the accepted answer .. works on any Linux OS.

      – argon
      Oct 27 '18 at 6:21



















    0














    echo 'password' | sshfs user@host:/dir /mnt -o password_stdin


    The "-o password_stdin" option is what enables you to pipe your password.



    That said, keys are a better option, unless your service provider doesn't let you use them for sftp. (This is one of WP Engine's notable failures.)






    share|improve this answer


























    • Can you please clarify your answer. Only one line of code do not count in SE as good answer

      – Romeo Ninov
      Jun 6 '17 at 3:44











    • @RomeoNinov I'd say this post does answer the question well enough. Not everybody is inclined to write a novel explaining why the obvious is obvious.

      – Satō Katsura
      Jun 6 '17 at 6:48











    • @SatoKatsura Thanks. I'm up to the challenge, though.

      – iateadonut
      Jun 7 '17 at 5:39



















    0














    one thing to keep in mind is that if you are using the -o password_stdin option, it might appear to not be working because sshfs will ask whether to connect to the host or not (if it is the first time you connect to it and it is not added to the known hosts file yet). If you are running it in a batch you will never see sshfs asking. Workarounds to avoid this are:




    1. add the -o StrictHostKeyChecking=no option to sshfs
      or

    2. run sshfs manually once to add the host to the known hosts file






    share|improve this answer































      0














      Mount script:



      #!/bin/bash
      server=<host>
      user=<username>
      pass=<password>
      root=<hostpath>
      mount=$(pwd)/sshfs

      type sshfs >/dev/null 2>&1 || { echo >&2 "sshfs required"; exit 1; }
      type sshpass >/dev/null 2>&1 || { echo >&2 "sshpass required"; exit 1; }

      mkdir -p $mount

      SSHPASS="$pass" sshfs $server:$root $mount -o ssh_command="sshpass -e ssh -l $user"


      Unmount:



      #!/bin/bash

      mount=$(pwd)/sshfs

      fusermount -u $mount





      share|improve this answer








      New contributor




      Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





















      • Welcome on U&L! Could you add some words of explanation? Answers that provide only code are generally considered of low quality on Stack Exchange sites (possibly even when the code is really good) - see, for extended discussion and explanation, this question on Meta SE.

        – fra-san
        Jan 10 at 15:53











      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      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%2f337465%2fusername-and-password-in-command-line-with-sshfs%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      13














      According to the manual, there is an option -o password_stdin which might allow to read the password from standard input, which can probably be a redirection. I have never used it, so I'm speculating.



      That said, I strongly advise against such a solution which is inherently insecure.



      ssh works very well with a system of private/public keys. It is simple and secure. No need to enter a password or to write it in clear in a shell script. Just push your public key on the server and you can connect immediately.






      share|improve this answer
























      • Can anyone provide a working example of this solution? I'm trying the following, but it is not working: printf "my_passwordn" | sshfs username@hostname:/remote_fs $HOME/local_dir

        – MikeyE
        Jul 10 '18 at 12:42













      • FYI, I should have mentioned I'm using a password protected SSH key. I was able to get it working using the answer provided by @nathan-s-watson-haigh found here: unix.stackexchange.com/questions/128974/…

        – MikeyE
        Jul 10 '18 at 13:07











      • I don't have any matching environment at hand that would allow me to test a ssfs-mount with a password protected SSH key. Anyway, what strikes me at first in your command is that I don't see any -o password_stdin option.

        – xhienne
        Jul 10 '18 at 14:09











      • Thanks for the input. I tried the following command just now, and it does not ask for a password, but it also does not return. Meaning, it just sits there waiting for the command to complete, no command prompt displayed. I tried the command: printf "my_passwordn" | sshfs -o password_stdin user@hostname:/ $HOME/local_dir P.S. I'm on Debian Buster P.P.S. This works, but prompts for a password: sshfs -o password_stdin user@hostname:/ $HOME/local_dir

        – MikeyE
        Jul 13 '18 at 16:32


















      13














      According to the manual, there is an option -o password_stdin which might allow to read the password from standard input, which can probably be a redirection. I have never used it, so I'm speculating.



      That said, I strongly advise against such a solution which is inherently insecure.



      ssh works very well with a system of private/public keys. It is simple and secure. No need to enter a password or to write it in clear in a shell script. Just push your public key on the server and you can connect immediately.






      share|improve this answer
























      • Can anyone provide a working example of this solution? I'm trying the following, but it is not working: printf "my_passwordn" | sshfs username@hostname:/remote_fs $HOME/local_dir

        – MikeyE
        Jul 10 '18 at 12:42













      • FYI, I should have mentioned I'm using a password protected SSH key. I was able to get it working using the answer provided by @nathan-s-watson-haigh found here: unix.stackexchange.com/questions/128974/…

        – MikeyE
        Jul 10 '18 at 13:07











      • I don't have any matching environment at hand that would allow me to test a ssfs-mount with a password protected SSH key. Anyway, what strikes me at first in your command is that I don't see any -o password_stdin option.

        – xhienne
        Jul 10 '18 at 14:09











      • Thanks for the input. I tried the following command just now, and it does not ask for a password, but it also does not return. Meaning, it just sits there waiting for the command to complete, no command prompt displayed. I tried the command: printf "my_passwordn" | sshfs -o password_stdin user@hostname:/ $HOME/local_dir P.S. I'm on Debian Buster P.P.S. This works, but prompts for a password: sshfs -o password_stdin user@hostname:/ $HOME/local_dir

        – MikeyE
        Jul 13 '18 at 16:32
















      13












      13








      13







      According to the manual, there is an option -o password_stdin which might allow to read the password from standard input, which can probably be a redirection. I have never used it, so I'm speculating.



      That said, I strongly advise against such a solution which is inherently insecure.



      ssh works very well with a system of private/public keys. It is simple and secure. No need to enter a password or to write it in clear in a shell script. Just push your public key on the server and you can connect immediately.






      share|improve this answer













      According to the manual, there is an option -o password_stdin which might allow to read the password from standard input, which can probably be a redirection. I have never used it, so I'm speculating.



      That said, I strongly advise against such a solution which is inherently insecure.



      ssh works very well with a system of private/public keys. It is simple and secure. No need to enter a password or to write it in clear in a shell script. Just push your public key on the server and you can connect immediately.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 15 '17 at 0:26









      xhiennexhienne

      12k2654




      12k2654













      • Can anyone provide a working example of this solution? I'm trying the following, but it is not working: printf "my_passwordn" | sshfs username@hostname:/remote_fs $HOME/local_dir

        – MikeyE
        Jul 10 '18 at 12:42













      • FYI, I should have mentioned I'm using a password protected SSH key. I was able to get it working using the answer provided by @nathan-s-watson-haigh found here: unix.stackexchange.com/questions/128974/…

        – MikeyE
        Jul 10 '18 at 13:07











      • I don't have any matching environment at hand that would allow me to test a ssfs-mount with a password protected SSH key. Anyway, what strikes me at first in your command is that I don't see any -o password_stdin option.

        – xhienne
        Jul 10 '18 at 14:09











      • Thanks for the input. I tried the following command just now, and it does not ask for a password, but it also does not return. Meaning, it just sits there waiting for the command to complete, no command prompt displayed. I tried the command: printf "my_passwordn" | sshfs -o password_stdin user@hostname:/ $HOME/local_dir P.S. I'm on Debian Buster P.P.S. This works, but prompts for a password: sshfs -o password_stdin user@hostname:/ $HOME/local_dir

        – MikeyE
        Jul 13 '18 at 16:32





















      • Can anyone provide a working example of this solution? I'm trying the following, but it is not working: printf "my_passwordn" | sshfs username@hostname:/remote_fs $HOME/local_dir

        – MikeyE
        Jul 10 '18 at 12:42













      • FYI, I should have mentioned I'm using a password protected SSH key. I was able to get it working using the answer provided by @nathan-s-watson-haigh found here: unix.stackexchange.com/questions/128974/…

        – MikeyE
        Jul 10 '18 at 13:07











      • I don't have any matching environment at hand that would allow me to test a ssfs-mount with a password protected SSH key. Anyway, what strikes me at first in your command is that I don't see any -o password_stdin option.

        – xhienne
        Jul 10 '18 at 14:09











      • Thanks for the input. I tried the following command just now, and it does not ask for a password, but it also does not return. Meaning, it just sits there waiting for the command to complete, no command prompt displayed. I tried the command: printf "my_passwordn" | sshfs -o password_stdin user@hostname:/ $HOME/local_dir P.S. I'm on Debian Buster P.P.S. This works, but prompts for a password: sshfs -o password_stdin user@hostname:/ $HOME/local_dir

        – MikeyE
        Jul 13 '18 at 16:32



















      Can anyone provide a working example of this solution? I'm trying the following, but it is not working: printf "my_passwordn" | sshfs username@hostname:/remote_fs $HOME/local_dir

      – MikeyE
      Jul 10 '18 at 12:42







      Can anyone provide a working example of this solution? I'm trying the following, but it is not working: printf "my_passwordn" | sshfs username@hostname:/remote_fs $HOME/local_dir

      – MikeyE
      Jul 10 '18 at 12:42















      FYI, I should have mentioned I'm using a password protected SSH key. I was able to get it working using the answer provided by @nathan-s-watson-haigh found here: unix.stackexchange.com/questions/128974/…

      – MikeyE
      Jul 10 '18 at 13:07





      FYI, I should have mentioned I'm using a password protected SSH key. I was able to get it working using the answer provided by @nathan-s-watson-haigh found here: unix.stackexchange.com/questions/128974/…

      – MikeyE
      Jul 10 '18 at 13:07













      I don't have any matching environment at hand that would allow me to test a ssfs-mount with a password protected SSH key. Anyway, what strikes me at first in your command is that I don't see any -o password_stdin option.

      – xhienne
      Jul 10 '18 at 14:09





      I don't have any matching environment at hand that would allow me to test a ssfs-mount with a password protected SSH key. Anyway, what strikes me at first in your command is that I don't see any -o password_stdin option.

      – xhienne
      Jul 10 '18 at 14:09













      Thanks for the input. I tried the following command just now, and it does not ask for a password, but it also does not return. Meaning, it just sits there waiting for the command to complete, no command prompt displayed. I tried the command: printf "my_passwordn" | sshfs -o password_stdin user@hostname:/ $HOME/local_dir P.S. I'm on Debian Buster P.P.S. This works, but prompts for a password: sshfs -o password_stdin user@hostname:/ $HOME/local_dir

      – MikeyE
      Jul 13 '18 at 16:32







      Thanks for the input. I tried the following command just now, and it does not ask for a password, but it also does not return. Meaning, it just sits there waiting for the command to complete, no command prompt displayed. I tried the command: printf "my_passwordn" | sshfs -o password_stdin user@hostname:/ $HOME/local_dir P.S. I'm on Debian Buster P.P.S. This works, but prompts for a password: sshfs -o password_stdin user@hostname:/ $HOME/local_dir

      – MikeyE
      Jul 13 '18 at 16:32















      6














      -o password_stdin do not seem to be working on all systems, for instance freeBSD. etc.


      You can also use expect Interpreter, it should work with sshfs
      and should do the trick.


      Another solution would be sshpass, for instance, let say your are backing up directory /var/www



      Backing up:



      name=$(date '+%y-%m-%d')
      mkdir /backup/$name && tar -czvf /backup/$name/"$name.tar.gz" /var/www


      uploading backup file to backup server



      sshpass -p "your_password" scp -r backup_user@target_ip:/home/ /backup/$name


      So it will upload directory with today's backup


      But still, as it was said higher, best(safe and simple) way would be to use ssh key pair

      The only inconvenience would be that you have to go through the key generation process once on every server you need to pair, but it is better than keeping a password in plain text format on all servers you want to back up :),

      Generating a Key Pair the Proper way





      • On Local server



        ssh-keygen -t rsa



      • On remote Server



        ssh root@remote_servers_ip "mkdir -p .ssh"



      • Uploading Generated Public Keys to the Remote Server



        cat ~/.ssh/id_rsa.pub | ssh root@remote_servers_ip "cat >> ~/.ssh/authorized_keys"



      • Set Permissions on Remote server



        ssh root@remote_servers_ip "chmod 700 ~/.ssh; chmod 640 ~/.ssh/authorized_keys"



      • Login



        ssh root@remote_servers_ip



      • Enabling SSH Protocol v2




        uncomment "Protocol 2" in /etc/ssh/sshd_config





      • enabling public key authorization in sshd




        uncomment "PubkeyAuthentication yes" in /etc/ssh/sshd_config





      • If StrictModes is set to yes in /etc/ssh/sshd_config then



        restorecon -Rv ~/.ssh







      share|improve this answer






























        6














        -o password_stdin do not seem to be working on all systems, for instance freeBSD. etc.


        You can also use expect Interpreter, it should work with sshfs
        and should do the trick.


        Another solution would be sshpass, for instance, let say your are backing up directory /var/www



        Backing up:



        name=$(date '+%y-%m-%d')
        mkdir /backup/$name && tar -czvf /backup/$name/"$name.tar.gz" /var/www


        uploading backup file to backup server



        sshpass -p "your_password" scp -r backup_user@target_ip:/home/ /backup/$name


        So it will upload directory with today's backup


        But still, as it was said higher, best(safe and simple) way would be to use ssh key pair

        The only inconvenience would be that you have to go through the key generation process once on every server you need to pair, but it is better than keeping a password in plain text format on all servers you want to back up :),

        Generating a Key Pair the Proper way





        • On Local server



          ssh-keygen -t rsa



        • On remote Server



          ssh root@remote_servers_ip "mkdir -p .ssh"



        • Uploading Generated Public Keys to the Remote Server



          cat ~/.ssh/id_rsa.pub | ssh root@remote_servers_ip "cat >> ~/.ssh/authorized_keys"



        • Set Permissions on Remote server



          ssh root@remote_servers_ip "chmod 700 ~/.ssh; chmod 640 ~/.ssh/authorized_keys"



        • Login



          ssh root@remote_servers_ip



        • Enabling SSH Protocol v2




          uncomment "Protocol 2" in /etc/ssh/sshd_config





        • enabling public key authorization in sshd




          uncomment "PubkeyAuthentication yes" in /etc/ssh/sshd_config





        • If StrictModes is set to yes in /etc/ssh/sshd_config then



          restorecon -Rv ~/.ssh







        share|improve this answer




























          6












          6








          6







          -o password_stdin do not seem to be working on all systems, for instance freeBSD. etc.


          You can also use expect Interpreter, it should work with sshfs
          and should do the trick.


          Another solution would be sshpass, for instance, let say your are backing up directory /var/www



          Backing up:



          name=$(date '+%y-%m-%d')
          mkdir /backup/$name && tar -czvf /backup/$name/"$name.tar.gz" /var/www


          uploading backup file to backup server



          sshpass -p "your_password" scp -r backup_user@target_ip:/home/ /backup/$name


          So it will upload directory with today's backup


          But still, as it was said higher, best(safe and simple) way would be to use ssh key pair

          The only inconvenience would be that you have to go through the key generation process once on every server you need to pair, but it is better than keeping a password in plain text format on all servers you want to back up :),

          Generating a Key Pair the Proper way





          • On Local server



            ssh-keygen -t rsa



          • On remote Server



            ssh root@remote_servers_ip "mkdir -p .ssh"



          • Uploading Generated Public Keys to the Remote Server



            cat ~/.ssh/id_rsa.pub | ssh root@remote_servers_ip "cat >> ~/.ssh/authorized_keys"



          • Set Permissions on Remote server



            ssh root@remote_servers_ip "chmod 700 ~/.ssh; chmod 640 ~/.ssh/authorized_keys"



          • Login



            ssh root@remote_servers_ip



          • Enabling SSH Protocol v2




            uncomment "Protocol 2" in /etc/ssh/sshd_config





          • enabling public key authorization in sshd




            uncomment "PubkeyAuthentication yes" in /etc/ssh/sshd_config





          • If StrictModes is set to yes in /etc/ssh/sshd_config then



            restorecon -Rv ~/.ssh







          share|improve this answer















          -o password_stdin do not seem to be working on all systems, for instance freeBSD. etc.


          You can also use expect Interpreter, it should work with sshfs
          and should do the trick.


          Another solution would be sshpass, for instance, let say your are backing up directory /var/www



          Backing up:



          name=$(date '+%y-%m-%d')
          mkdir /backup/$name && tar -czvf /backup/$name/"$name.tar.gz" /var/www


          uploading backup file to backup server



          sshpass -p "your_password" scp -r backup_user@target_ip:/home/ /backup/$name


          So it will upload directory with today's backup


          But still, as it was said higher, best(safe and simple) way would be to use ssh key pair

          The only inconvenience would be that you have to go through the key generation process once on every server you need to pair, but it is better than keeping a password in plain text format on all servers you want to back up :),

          Generating a Key Pair the Proper way





          • On Local server



            ssh-keygen -t rsa



          • On remote Server



            ssh root@remote_servers_ip "mkdir -p .ssh"



          • Uploading Generated Public Keys to the Remote Server



            cat ~/.ssh/id_rsa.pub | ssh root@remote_servers_ip "cat >> ~/.ssh/authorized_keys"



          • Set Permissions on Remote server



            ssh root@remote_servers_ip "chmod 700 ~/.ssh; chmod 640 ~/.ssh/authorized_keys"



          • Login



            ssh root@remote_servers_ip



          • Enabling SSH Protocol v2




            uncomment "Protocol 2" in /etc/ssh/sshd_config





          • enabling public key authorization in sshd




            uncomment "PubkeyAuthentication yes" in /etc/ssh/sshd_config





          • If StrictModes is set to yes in /etc/ssh/sshd_config then



            restorecon -Rv ~/.ssh








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 6 '18 at 19:14









          Drakonoved

          7091520




          7091520










          answered Jan 15 '17 at 4:19







          user209328






























              3














              Piping the "sshfs password" with <<< to -o password_stdin works on bash:



              sshfs -o password_stdin backup_user@target_ip:/home /mnt/backup <<< "sshfs password"





              share|improve this answer


























              • This should be the accepted answer .. works on any Linux OS.

                – argon
                Oct 27 '18 at 6:21
















              3














              Piping the "sshfs password" with <<< to -o password_stdin works on bash:



              sshfs -o password_stdin backup_user@target_ip:/home /mnt/backup <<< "sshfs password"





              share|improve this answer


























              • This should be the accepted answer .. works on any Linux OS.

                – argon
                Oct 27 '18 at 6:21














              3












              3








              3







              Piping the "sshfs password" with <<< to -o password_stdin works on bash:



              sshfs -o password_stdin backup_user@target_ip:/home /mnt/backup <<< "sshfs password"





              share|improve this answer















              Piping the "sshfs password" with <<< to -o password_stdin works on bash:



              sshfs -o password_stdin backup_user@target_ip:/home /mnt/backup <<< "sshfs password"






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jul 24 '17 at 2:18

























              answered Jul 24 '17 at 2:03









              MazMaz

              313




              313













              • This should be the accepted answer .. works on any Linux OS.

                – argon
                Oct 27 '18 at 6:21



















              • This should be the accepted answer .. works on any Linux OS.

                – argon
                Oct 27 '18 at 6:21

















              This should be the accepted answer .. works on any Linux OS.

              – argon
              Oct 27 '18 at 6:21





              This should be the accepted answer .. works on any Linux OS.

              – argon
              Oct 27 '18 at 6:21











              0














              echo 'password' | sshfs user@host:/dir /mnt -o password_stdin


              The "-o password_stdin" option is what enables you to pipe your password.



              That said, keys are a better option, unless your service provider doesn't let you use them for sftp. (This is one of WP Engine's notable failures.)






              share|improve this answer


























              • Can you please clarify your answer. Only one line of code do not count in SE as good answer

                – Romeo Ninov
                Jun 6 '17 at 3:44











              • @RomeoNinov I'd say this post does answer the question well enough. Not everybody is inclined to write a novel explaining why the obvious is obvious.

                – Satō Katsura
                Jun 6 '17 at 6:48











              • @SatoKatsura Thanks. I'm up to the challenge, though.

                – iateadonut
                Jun 7 '17 at 5:39
















              0














              echo 'password' | sshfs user@host:/dir /mnt -o password_stdin


              The "-o password_stdin" option is what enables you to pipe your password.



              That said, keys are a better option, unless your service provider doesn't let you use them for sftp. (This is one of WP Engine's notable failures.)






              share|improve this answer


























              • Can you please clarify your answer. Only one line of code do not count in SE as good answer

                – Romeo Ninov
                Jun 6 '17 at 3:44











              • @RomeoNinov I'd say this post does answer the question well enough. Not everybody is inclined to write a novel explaining why the obvious is obvious.

                – Satō Katsura
                Jun 6 '17 at 6:48











              • @SatoKatsura Thanks. I'm up to the challenge, though.

                – iateadonut
                Jun 7 '17 at 5:39














              0












              0








              0







              echo 'password' | sshfs user@host:/dir /mnt -o password_stdin


              The "-o password_stdin" option is what enables you to pipe your password.



              That said, keys are a better option, unless your service provider doesn't let you use them for sftp. (This is one of WP Engine's notable failures.)






              share|improve this answer















              echo 'password' | sshfs user@host:/dir /mnt -o password_stdin


              The "-o password_stdin" option is what enables you to pipe your password.



              That said, keys are a better option, unless your service provider doesn't let you use them for sftp. (This is one of WP Engine's notable failures.)







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jun 7 '17 at 5:35

























              answered Jun 6 '17 at 2:17









              iateadonutiateadonut

              1111




              1111













              • Can you please clarify your answer. Only one line of code do not count in SE as good answer

                – Romeo Ninov
                Jun 6 '17 at 3:44











              • @RomeoNinov I'd say this post does answer the question well enough. Not everybody is inclined to write a novel explaining why the obvious is obvious.

                – Satō Katsura
                Jun 6 '17 at 6:48











              • @SatoKatsura Thanks. I'm up to the challenge, though.

                – iateadonut
                Jun 7 '17 at 5:39



















              • Can you please clarify your answer. Only one line of code do not count in SE as good answer

                – Romeo Ninov
                Jun 6 '17 at 3:44











              • @RomeoNinov I'd say this post does answer the question well enough. Not everybody is inclined to write a novel explaining why the obvious is obvious.

                – Satō Katsura
                Jun 6 '17 at 6:48











              • @SatoKatsura Thanks. I'm up to the challenge, though.

                – iateadonut
                Jun 7 '17 at 5:39

















              Can you please clarify your answer. Only one line of code do not count in SE as good answer

              – Romeo Ninov
              Jun 6 '17 at 3:44





              Can you please clarify your answer. Only one line of code do not count in SE as good answer

              – Romeo Ninov
              Jun 6 '17 at 3:44













              @RomeoNinov I'd say this post does answer the question well enough. Not everybody is inclined to write a novel explaining why the obvious is obvious.

              – Satō Katsura
              Jun 6 '17 at 6:48





              @RomeoNinov I'd say this post does answer the question well enough. Not everybody is inclined to write a novel explaining why the obvious is obvious.

              – Satō Katsura
              Jun 6 '17 at 6:48













              @SatoKatsura Thanks. I'm up to the challenge, though.

              – iateadonut
              Jun 7 '17 at 5:39





              @SatoKatsura Thanks. I'm up to the challenge, though.

              – iateadonut
              Jun 7 '17 at 5:39











              0














              one thing to keep in mind is that if you are using the -o password_stdin option, it might appear to not be working because sshfs will ask whether to connect to the host or not (if it is the first time you connect to it and it is not added to the known hosts file yet). If you are running it in a batch you will never see sshfs asking. Workarounds to avoid this are:




              1. add the -o StrictHostKeyChecking=no option to sshfs
                or

              2. run sshfs manually once to add the host to the known hosts file






              share|improve this answer




























                0














                one thing to keep in mind is that if you are using the -o password_stdin option, it might appear to not be working because sshfs will ask whether to connect to the host or not (if it is the first time you connect to it and it is not added to the known hosts file yet). If you are running it in a batch you will never see sshfs asking. Workarounds to avoid this are:




                1. add the -o StrictHostKeyChecking=no option to sshfs
                  or

                2. run sshfs manually once to add the host to the known hosts file






                share|improve this answer


























                  0












                  0








                  0







                  one thing to keep in mind is that if you are using the -o password_stdin option, it might appear to not be working because sshfs will ask whether to connect to the host or not (if it is the first time you connect to it and it is not added to the known hosts file yet). If you are running it in a batch you will never see sshfs asking. Workarounds to avoid this are:




                  1. add the -o StrictHostKeyChecking=no option to sshfs
                    or

                  2. run sshfs manually once to add the host to the known hosts file






                  share|improve this answer













                  one thing to keep in mind is that if you are using the -o password_stdin option, it might appear to not be working because sshfs will ask whether to connect to the host or not (if it is the first time you connect to it and it is not added to the known hosts file yet). If you are running it in a batch you will never see sshfs asking. Workarounds to avoid this are:




                  1. add the -o StrictHostKeyChecking=no option to sshfs
                    or

                  2. run sshfs manually once to add the host to the known hosts file







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 31 '18 at 11:16









                  frenchie71frenchie71

                  11




                  11























                      0














                      Mount script:



                      #!/bin/bash
                      server=<host>
                      user=<username>
                      pass=<password>
                      root=<hostpath>
                      mount=$(pwd)/sshfs

                      type sshfs >/dev/null 2>&1 || { echo >&2 "sshfs required"; exit 1; }
                      type sshpass >/dev/null 2>&1 || { echo >&2 "sshpass required"; exit 1; }

                      mkdir -p $mount

                      SSHPASS="$pass" sshfs $server:$root $mount -o ssh_command="sshpass -e ssh -l $user"


                      Unmount:



                      #!/bin/bash

                      mount=$(pwd)/sshfs

                      fusermount -u $mount





                      share|improve this answer








                      New contributor




                      Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





















                      • Welcome on U&L! Could you add some words of explanation? Answers that provide only code are generally considered of low quality on Stack Exchange sites (possibly even when the code is really good) - see, for extended discussion and explanation, this question on Meta SE.

                        – fra-san
                        Jan 10 at 15:53
















                      0














                      Mount script:



                      #!/bin/bash
                      server=<host>
                      user=<username>
                      pass=<password>
                      root=<hostpath>
                      mount=$(pwd)/sshfs

                      type sshfs >/dev/null 2>&1 || { echo >&2 "sshfs required"; exit 1; }
                      type sshpass >/dev/null 2>&1 || { echo >&2 "sshpass required"; exit 1; }

                      mkdir -p $mount

                      SSHPASS="$pass" sshfs $server:$root $mount -o ssh_command="sshpass -e ssh -l $user"


                      Unmount:



                      #!/bin/bash

                      mount=$(pwd)/sshfs

                      fusermount -u $mount





                      share|improve this answer








                      New contributor




                      Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





















                      • Welcome on U&L! Could you add some words of explanation? Answers that provide only code are generally considered of low quality on Stack Exchange sites (possibly even when the code is really good) - see, for extended discussion and explanation, this question on Meta SE.

                        – fra-san
                        Jan 10 at 15:53














                      0












                      0








                      0







                      Mount script:



                      #!/bin/bash
                      server=<host>
                      user=<username>
                      pass=<password>
                      root=<hostpath>
                      mount=$(pwd)/sshfs

                      type sshfs >/dev/null 2>&1 || { echo >&2 "sshfs required"; exit 1; }
                      type sshpass >/dev/null 2>&1 || { echo >&2 "sshpass required"; exit 1; }

                      mkdir -p $mount

                      SSHPASS="$pass" sshfs $server:$root $mount -o ssh_command="sshpass -e ssh -l $user"


                      Unmount:



                      #!/bin/bash

                      mount=$(pwd)/sshfs

                      fusermount -u $mount





                      share|improve this answer








                      New contributor




                      Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.










                      Mount script:



                      #!/bin/bash
                      server=<host>
                      user=<username>
                      pass=<password>
                      root=<hostpath>
                      mount=$(pwd)/sshfs

                      type sshfs >/dev/null 2>&1 || { echo >&2 "sshfs required"; exit 1; }
                      type sshpass >/dev/null 2>&1 || { echo >&2 "sshpass required"; exit 1; }

                      mkdir -p $mount

                      SSHPASS="$pass" sshfs $server:$root $mount -o ssh_command="sshpass -e ssh -l $user"


                      Unmount:



                      #!/bin/bash

                      mount=$(pwd)/sshfs

                      fusermount -u $mount






                      share|improve this answer








                      New contributor




                      Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      share|improve this answer



                      share|improve this answer






                      New contributor




                      Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      answered Jan 10 at 14:59









                      MichaelMichael

                      1




                      1




                      New contributor




                      Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





                      New contributor





                      Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.






                      Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.













                      • Welcome on U&L! Could you add some words of explanation? Answers that provide only code are generally considered of low quality on Stack Exchange sites (possibly even when the code is really good) - see, for extended discussion and explanation, this question on Meta SE.

                        – fra-san
                        Jan 10 at 15:53



















                      • Welcome on U&L! Could you add some words of explanation? Answers that provide only code are generally considered of low quality on Stack Exchange sites (possibly even when the code is really good) - see, for extended discussion and explanation, this question on Meta SE.

                        – fra-san
                        Jan 10 at 15:53

















                      Welcome on U&L! Could you add some words of explanation? Answers that provide only code are generally considered of low quality on Stack Exchange sites (possibly even when the code is really good) - see, for extended discussion and explanation, this question on Meta SE.

                      – fra-san
                      Jan 10 at 15:53





                      Welcome on U&L! Could you add some words of explanation? Answers that provide only code are generally considered of low quality on Stack Exchange sites (possibly even when the code is really good) - see, for extended discussion and explanation, this question on Meta SE.

                      – fra-san
                      Jan 10 at 15:53


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f337465%2fusername-and-password-in-command-line-with-sshfs%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to make a Squid Proxy server?

                      第一次世界大戦

                      Touch on Surface Book