<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    jojo's blog--快樂憂傷都與你同在
    為夢想而來,為自由而生。 性情若水,風起水興,風息水止,故時而激蕩,時又清平……
    posts - 11,  comments - 30,  trackbacks - 0

    How to Automate Secure File Synchronization using SSH and rsync

    Tom Hilinski
    Natural Resource Ecology Laboratory,
    Colorado State University
    Last updated: Dec 2008

    Introduction

    In order to automate file transfers between computers without a password, a private-public key identification key simplifies the process. This is useful, for instance, when using rsync to synchronize files in local and remote directories. For instance, after editing files on your local Linux desktop or Microsoft Windows laptop, you want to automatically update the files on the office computer with your modified files. In this case, a utility such as rsync can be used to do the update without prompting you for a password on the office computer.

    The process of creating the key is described here in the context of using rsync on a local Windows computer with Cygwin installed. Use the Cygwin setup program to install SSH, rsync, and Bash. Here, I assume that the remote computer, say, your office server, is running Linux, and you have an account on it with the user name yourUserName.

    In the examples below, command lines begin with a $ character, comment lines begin with a # character, while text beginning without either is written to the console display. Example text that is italicized means you substitute your own information there; for example, yourUserName is replace by your actual user name.

    Create a Key

    On your local Windows computer, open a Bash shell console window. If you don't have a directory named .ssh create one by using SSH to connect to your office computer for the first time. You will be prompted to accept the key.

    $ ls -d .ssh
    # if the directory does not exist, run ssh
    $ ssh yourUserName@calypso.nrel.colostate.edu

    Go into the .ssh directory and create a key. This key will have two files, a private file and a public file. When prompted, do not enter a password or passphrase.

    $ ssh-keygen -t dsa -b 1024 -f yourUserName-rsync-key
    Generating public/private dsa key pair.
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in yourUserName-rsync-key.
    Your public key has been saved in yourUserName-rsync-key.pub.
    The key fingerprint is:
    (a long string of hexadecimal digits)

    Check the permissions on your key files (e.g., ls -l). The permissions should be 600 (or rw-----).

    On your office computer, make sure you have in your home directory, a subdirectory named .ssh (note the leading dot).

    $ ssh yourUserName@calypso.nrel.colostate.edu
    $ ls -d .ssh
    # If this directory doesn't exist, create it:
    $ mkdir .ssh
    # Make sure the permissions are secure:
    $ chmod 700 .ssh

    Now, log off your office computer.

    Next, copy the public key file to your office computer, log onto that computer, then append the key file to the SSH file containing keys it knows about.

    # Copy the public key to your office computer:
    $ scp yourUserName-rsync-key.pub yourUserName@calypso.nrel.colostate.edu:/home/nrel/yourUserName/.ssh/
    # Log on to the remote computer:
    $ ssh yourUserName@calypso.nrel.colostate.edu
    # If your are not in a bash shell, then start one:
    $ bash
    # If the key file does not exist, create it:
    $ if [ ! -f authorized_keys ]; then touch authorized_keys; chmod 600 authorized_keys; fi
    # Append your new public key to the key file:
    $ cat yourUserName-rsync-key.pub >> authorized_keys
    $ rm yourUserName-rsync-key.pub

    Your key is now ready to use with rsync. Optionally, you can restrict the use of the key to an IP address and a particular process (e.g., rsync). To restrict the key to rsync, create the file listed in Appendix A in your ~/.ssh directory on your office computer. You can use a text editor to paste that text in. Then set the permissions so no one else can read it. For example:

    # Use vi to create the file; paste in the script from Appendix A.
    $ vi restrict-to-rsync
    $ chmod 700 restrict-to-rsync

    Next, edit the file authorized_keys so that the line with your key (the last line, since the key was just appended to the file) begins with a command to run that script. The command points to the full path of the script file. The line originally began with:

    ssh-dss AAAAB3...

    After inserting the script command, the line starts with:

    command="/home/nrel/yourUserName/.ssh/restrict-to-rsync" ssh-dss AAAAB3...

    Using rsync With SSH and Your Key

    Test the use of your new key by copying a junk file from your local computer to your office computer. Here, the local file is junk.txt and the remote directory in your office computer is tmp, and the direction of transfer is local-to-remote. Give SSH the name of your private key file on your local computer, including its path, using the following form:

    rsync -auvz -e "ssh -i private-key-file" source destination

    Here, source is a file or a directory, and destination has the form yourUserName@remote-computer:/remote-path

    A real example, using the file names from the previous examples, is:

    rsync -auvz -e "ssh -i /home/yourUserName/.ssh/yourUserName-rsync-key" junk.txt yourUserName@calypso.nrel.colostate.edu

    The rsync flags -auvz specify "archive", "update", "verbose messages", and "compress files for transfer", respectively. "Update" means that files on the destination that are newer than your local files are not overwritten. The "-e" flag tells rsync the SSH command.

    If you want details on what SSH is doing, add "-v" to the ssh options. To run rsync quietly, remove the "-v" option from both rsync and SSH option list.

    To reverse the synchronization so the remote file is updated on your local computer, reverse the source and destinations.

    You can store your rsync commands that you use all the time in a script file. Keep this script with your project files or in a script directory that is specified in your PATH environment variable.

    Additional Information

    rsync document: http://rsync.samba.org/ftp/rsync/rsync.html

    rsync web site: http://rsync.samba.org/

    Acknowledgements

    Many online sources provided the information I used to create this process. A particularly succinct source was http://troy.jdmz.net/rsync/index.html provided the basis of the script in Appendix A. Thanks to all.

    Appendix A: File restrict-to-rsync

    The following shell script checks that rsync is the process attempting to connect. If it is not, the script fails, and SSH also fails. A log file named validate-rsync.log is created or appended to with each connection.

    #!/bin/sh
    logfile=/home/nrel/yourUserName/.ssh/restrict-to-rsync.log
    case "$SSH_ORIGINAL_COMMAND" in
    *\&*)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    *\(*)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    *\{*)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    *\;*)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    *\<*)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    *\`*)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    *\|*)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    rsync\ --server*)
    {
    echo `date` "- SSH connection accepted" >> $logfile
    $SSH_ORIGINAL_COMMAND
    }
    ;;
    *)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    esac

    posted on 2009-01-20 17:18 Blog of JoJo 閱讀(266) 評論(0)  編輯  收藏 所屬分類: Linux 技術相關

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章分類

    文章檔案

    新聞分類

    新聞檔案

    相冊

    收藏夾

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 精品成人一区二区三区免费视频| 亚洲成AV人片在| 亚洲综合中文字幕无线码| 日本免费人成网ww555在线| 亚洲熟妇丰满多毛XXXX| 三级片免费观看久久| 亚洲AV无码之日韩精品| 亚洲AV成人无码网站| 国产精品国产自线拍免费软件 | 久久久久亚洲av成人无码电影| 小说专区亚洲春色校园| 成人永久免费福利视频网站| 亚洲国产成人在线视频| 最近2019中文字幕mv免费看 | 亚洲熟伦熟女专区hd高清| 成熟女人牲交片免费观看视频| 亚洲色大成网站www尤物| 日韩成人免费aa在线看| 免费看黄网站在线看| 国产精品久久亚洲不卡动漫| 四虎在线视频免费观看视频| 亚洲一区免费视频| 日韩一区二区三区免费体验| 色屁屁在线观看视频免费| 国产亚洲精品成人AA片新蒲金 | 亚洲美女激情视频| 久久久久国产精品免费免费搜索| 亚洲va中文字幕| 亚洲日本一区二区三区在线不卡| 亚洲精品中文字幕麻豆| 成人看的午夜免费毛片| 污视频网站免费在线观看| 国产亚洲精品无码成人| 无码av免费毛片一区二区| 免费福利在线观看| 亚洲国产精品自在线一区二区| 成年女人男人免费视频播放 | 国产在线观看免费完整版中文版 | 亚洲av永久无码精品秋霞电影影院 | 亚洲香蕉久久一区二区三区四区| 国产在线播放免费|