ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    CentOS rsync between servers using keyfile to pass credentials

    IT Discussion
    rsync centos7 crontab
    9
    44
    4.5k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ObsolesceO
      Obsolesce @DustinB3403
      last edited by Obsolesce

      @DustinB3403 said in CentOS rsync between servers using keyfile to pass credentials:

      ok I've got the public and private keys

      If you need the md5 "fingerprint", you do that like this:

      ssh-keygen -E md5 -lf /etc/ssh/ssh_host_rsa_key.pub

      ..or whatever the filename of your key is.

      I had to do that for WinSCP and SSH.

      1 Reply Last reply Reply Quote 1
      • stacksofplatesS
        stacksofplates @scottalanmiller
        last edited by

        @scottalanmiller said in CentOS rsync between servers using keyfile to pass credentials:

        Okay, now do the same thing on the second server. Then take the contents of the public key from the first one and populate the /home/user/.ssh/authorized_keys file on the second server with it.

        I'd just do ssh-copy-id Its easier. That way there is no chance the SELinux context (or mode) for that file can change.

        1 Reply Last reply Reply Quote 4
        • Emad RE
          Emad R
          last edited by

          Hi,

          I see scott gave you some answers, but I will try to break it down easier otherwise I missunderstood the question.

          So in server A and B
          run this

          ssh-keygen -t rsa

          And press enter to accept the defaults

          Now in both servers under the user account you will see hidden folder called .ssh which can be found

          /home/USERNAME/.ssh
          or
          /root/.ssh

          And inside this folder you will find :
          id_rsa.pub

          Copy the contents of this file in both servers A and B into a new file called and name it authorized_keys and place it in the same directory, for example server A will have key 123 server B will have key 456 in this authorized_keys it should have both entries.

          And bam thats it , this is SSH pasword less login, not sure if its the same thing but seems easier and better.

          travisdh1T 1 Reply Last reply Reply Quote 2
          • travisdh1T
            travisdh1 @Emad R
            last edited by

            @msff-amman-Itofficer Yes.....

            Except for the root bit, that's not considered a good practice. If you want remote access as root, ssh in with a normal user account and then use sudo. Gives you a way to track what is being done in the logs.

            1 Reply Last reply Reply Quote 0
            • DustinB3403D
              DustinB3403
              last edited by

              This is still requiring the ssh access key, and the root password of the remote host.

              scottalanmillerS travisdh1T 2 Replies Last reply Reply Quote 0
              • scottalanmillerS
                scottalanmiller @DustinB3403
                last edited by

                @DustinB3403 said in CentOS rsync between servers using keyfile to pass credentials:

                This is still requiring the ssh access key, and the root password of the remote host.

                Which did you do?

                1 Reply Last reply Reply Quote 0
                • DustinB3403D
                  DustinB3403
                  last edited by

                  Generated a public and private key on each host.

                  Moved the public key onto each host overwriting authorized_keys.

                  Using rsync -avztopgr -e "ssh -i $HOME/.ssh/authorized_keys" /home/admin/summit-reg/wp-config.php sr2i:/home/admin/summit-reg/

                  I'm still asked for root password.

                  1 Reply Last reply Reply Quote 0
                  • travisdh1T
                    travisdh1 @DustinB3403
                    last edited by

                    @DustinB3403 said in CentOS rsync between servers using keyfile to pass credentials:

                    This is still requiring the ssh access key, and the root password of the remote host.

                    ???

                    Two different things! You ssh into the remote with the standard user. Remote access is now established and you're running as if local.

                    sudo does not require the root password, it requires the user authenticate to the system when used. With sudo you never require the root password. su does require the root password, but good security practice would not have anyone use su.

                    Also note I say authenticate to the system, a great number of different ways to do that without passwords.

                    DustinB3403D 1 Reply Last reply Reply Quote 0
                    • DustinB3403D
                      DustinB3403 @travisdh1
                      last edited by

                      @travisdh1 That is the goal, to use the public and private keys to authenticate between the servers and then rsync any changes from host 1 to host 2.

                      travisdh1T 1 Reply Last reply Reply Quote 1
                      • DustinB3403D
                        DustinB3403
                        last edited by

                        Any ideas guys?

                        Reid CooperR 1 Reply Last reply Reply Quote 0
                        • Reid CooperR
                          Reid Cooper @DustinB3403
                          last edited by

                          @DustinB3403 said in CentOS rsync between servers using keyfile to pass credentials:

                          Any ideas guys?

                          Any error? What did you do?

                          DustinB3403D 1 Reply Last reply Reply Quote 0
                          • travisdh1T
                            travisdh1 @DustinB3403
                            last edited by

                            @DustinB3403 said in CentOS rsync between servers using keyfile to pass credentials:

                            @travisdh1 That is the goal, to use the public and private keys to authenticate between the servers and then rsync any changes from host 1 to host 2.

                            Right, using ssh-copy-id is the easiest way to get the ssh keys to the proper places once generated. After that, just use rsync with ssh like normal and it won't ask for a password.

                            rsync -e ssh user@host:/dir /local/dir
                            

                            I've distributed keys manually in the past, but ssh-copy-id is so much faster and foolproof.... yes, I can be a fool with ssh keys quite easily.

                            1 Reply Last reply Reply Quote 1
                            • DustinB3403D
                              DustinB3403 @Reid Cooper
                              last edited by

                              @Reid-Cooper No errors, just a prompt for the root password to connect to the server.

                              Reid CooperR travisdh1T 2 Replies Last reply Reply Quote 0
                              • Reid CooperR
                                Reid Cooper @DustinB3403
                                last edited by

                                @DustinB3403 said in CentOS rsync between servers using keyfile to pass credentials:

                                @Reid-Cooper No errors, just a prompt for the root password to connect to the server.

                                Is it root's account that you set the public key to be used for? /root/.ssh instead of /home/user/.ssh

                                1 Reply Last reply Reply Quote 0
                                • travisdh1T
                                  travisdh1 @DustinB3403
                                  last edited by

                                  @DustinB3403 said in CentOS rsync between servers using keyfile to pass credentials:

                                  @Reid-Cooper No errors, just a prompt for the root password to connect to the server.

                                  Let's back up a step here. Do the local files need root permission to read, and does the remote directory need root permission to write? If so, you do need to create an ssh key for the local machine root and use ssh-copy-id to copy that to the remote one.

                                  1 Reply Last reply Reply Quote 0
                                  • RamblingBipedR
                                    RamblingBiped
                                    last edited by RamblingBiped

                                    You could create a user on each system, specific to this task; and give that user restricted passwordless sudo access to only run the specific rsync command you need to have run.

                                    Throw your rsync command in a script and restrict your user's passwordless sudo access to running that specific script. Et voila! Authentication issues resolved.

                                    --edit--

                                    I think this can help point you in the right direction: https://serverfault.com/questions/596940/sudoers-nopasswd-how-to-grant-access-to-a-specific-ln-command

                                    1 Reply Last reply Reply Quote 1
                                    • DustinB3403D
                                      DustinB3403
                                      last edited by

                                      Ok well I can ssh between the servers, but both are asking for the root/.ssh/id_rsa passphrase and the root credentials.

                                      RamblingBipedR 1 Reply Last reply Reply Quote 0
                                      • RamblingBipedR
                                        RamblingBiped @DustinB3403
                                        last edited by

                                        @DustinB3403

                                        @DustinB3403 said in CentOS rsync between servers using keyfile to pass credentials:

                                        Ok well I can ssh between the servers, but both are asking for the root/.ssh/id_rsa passphrase and the root credentials.

                                        That is where you would want passwordless sudo access tied to a specific user that will ONLY have that sudo access restricted to running your rsync command.

                                        You'll be able to log into the remote system using your keys, and with passwordless sudo enabled, you'll not be prompted for a password when you run your sudo rsync... command.

                                        1 Reply Last reply Reply Quote 0
                                        • DustinB3403D
                                          DustinB3403
                                          last edited by

                                          @RamblingBiped why can't I use the keys generated for this, rather than needing an outside account with passwordless sudo access?

                                          RamblingBipedR 1 Reply Last reply Reply Quote 0
                                          • DustinB3403D
                                            DustinB3403
                                            last edited by

                                            The goal is to only use the public and private keys to allow me to sync files from one server to the other via crontab (without needing to be prompted for credentials)

                                            The public and private keys should suffice for that.

                                            1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 3
                                            • 1 / 3
                                            • First post
                                              Last post