{"id":530,"date":"2024-11-07T10:06:50","date_gmt":"2024-11-07T03:06:50","guid":{"rendered":"https:\/\/nbvps.anhtuanlqd.com\/?p=530"},"modified":"2025-01-09T10:02:46","modified_gmt":"2025-01-09T03:02:46","slug":"start-ssh-agent-and-add-your-key","status":"publish","type":"post","link":"https:\/\/nbvps.anhtuanlqd.com\/?p=530","title":{"rendered":"Start ssh-agent and Add Your Key"},"content":{"rendered":"\n<p>If you\u2019ve already set up SSH key-based authentication with a passphrase for connecting to the remote server, you can use rsync along with ssh-agent to handle the passphrase for you. Here\u2019s a step-by-step guide to get this working:<\/p>\n\n\n\n<p><strong>Step 1: Start ssh-agent and Add Your Key<\/strong><\/p>\n\n\n\n<p>The ssh-agent will keep your SSH key unlocked for the duration of your session, so you won\u2019t need to enter the passphrase each time you use rsync.<\/p>\n\n\n\n<p>1. Open a terminal.<\/p>\n\n\n\n<p>2. Start the ssh-agent:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>eval \"$(ssh-agent -s)\"<\/code><\/pre>\n\n\n\n<p>3. Add your SSH key to the agent. This will prompt you for the passphrase one time:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-add \/path\/to\/your\/private_key<\/code><\/pre>\n\n\n\n<p>Replace \/path\/to\/your\/private_key with the actual path to your SSH private key file (usually ~\/.ssh\/id_rsa or ~\/.ssh\/id_ed25519).<\/p>\n\n\n\n<p><strong>Step 2: Use rsync to Sync Files<\/strong><\/p>\n\n\n\n<p>Now that ssh-agent is managing your key, you can use rsync without having to enter the passphrase each time.<\/p>\n\n\n\n<p>The basic syntax for rsync to a remote server is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>rsync -avz \/local\/directory\/ user@remote_host:\/remote\/directory\/<\/code><\/code><\/pre>\n\n\n\n<p>\u2022&nbsp;<strong>Options<\/strong>:<\/p>\n\n\n\n<p>\u2022 -a: Archive mode, which preserves file permissions, timestamps, and symlinks.<\/p>\n\n\n\n<p>\u2022 -v: Verbose, to show detailed output.<\/p>\n\n\n\n<p>\u2022 -z: Compresses data during the transfer for faster sync.<\/p>\n\n\n\n<p><strong>Example Command<\/strong><\/p>\n\n\n\n<p>Assuming your local directory is \/var\/www\/html and you want to sync it to \/var\/www\/html on the remote server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>rsync -avz \/var\/www\/html\/ user@remote_host:\/var\/www\/html\/<\/code><\/code><\/pre>\n\n\n\n<p>Replace user@remote_host with your actual username and remote server address.<\/p>\n\n\n\n<p><strong>Step 3: Automate with a Script (Optional)<\/strong><\/p>\n\n\n\n<p>If you need to run rsync regularly, you can create a script and schedule it with cron:<\/p>\n\n\n\n<p>1.&nbsp;<strong>Create a script<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>nano ~\/sync_script.sh<\/code><\/code><\/pre>\n\n\n\n<p>2.&nbsp;<strong>Add the rsync command<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\nrsync -avz \/local\/directory\/ user@remote_host:\/remote\/directory\/<\/code><\/pre>\n\n\n\n<p>3.\u00a0<strong>Make the script executable<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod +x ~\/sync_script.sh<\/code><\/pre>\n\n\n\n<p>4.\u00a0<strong>Schedule it with\u00a0<\/strong>cron:<\/p>\n\n\n\n<p>Open your cron file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -e<\/code><\/pre>\n\n\n\n<p>Add a line to schedule the sync (e.g., every night at 2 a.m.):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 2 * * * ~\/sync_script.sh<\/code><\/pre>\n\n\n\n<p>This setup will use ssh-agent to avoid repeatedly asking for the passphrase and allow your script to run automatically at scheduled times.<\/p>\n ","protected":false},"excerpt":{"rendered":"<p>If you\u2019ve already set up SSH key-based authentication with a passphrase for connecting to the remote server, you can use rsync along with ssh-agent to handle the passphrase for you. Here\u2019s a step-by-step guide to get this working: Step 1: Start ssh-agent and Add Your Key The ssh-agent will keep your SSH key unlocked for [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","footnotes":""},"categories":[50,2,23],"tags":[184,132,185,183,38],"class_list":["post-530","post","type-post","status-publish","format-standard","hentry","category-command","category-linux","category-tips-and-trick","tag-rsync-without-password-for-key","tag-ssh","tag-ssh-add","tag-ssh-agent","tag-ubuntu"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/nbvps.anhtuanlqd.com\/index.php?rest_route=\/wp\/v2\/posts\/530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nbvps.anhtuanlqd.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nbvps.anhtuanlqd.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nbvps.anhtuanlqd.com\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/nbvps.anhtuanlqd.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=530"}],"version-history":[{"count":2,"href":"https:\/\/nbvps.anhtuanlqd.com\/index.php?rest_route=\/wp\/v2\/posts\/530\/revisions"}],"predecessor-version":[{"id":565,"href":"https:\/\/nbvps.anhtuanlqd.com\/index.php?rest_route=\/wp\/v2\/posts\/530\/revisions\/565"}],"wp:attachment":[{"href":"https:\/\/nbvps.anhtuanlqd.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nbvps.anhtuanlqd.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nbvps.anhtuanlqd.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}