<<< Back to the Linux Tips Index
8th October 2016
Copy directory and files from local to remote server. You can use either scp or rsync to copy folder and files from local to ssh or copy folder and files from ssh to local within in the same or different directory. By default copy files and folders happen sequentially. Copying SSH /.ssh/idrsa between machines. Handy ssh command of the day: ssh-copy-id. The ssh-copy-id command copies your Public Key to a remote machine. It also configures all of the file permissions correctly, because SSH is (rightly) very picky about who can access your SSH configuration. Ssh-copy-id installs an SSH key on a server as an authorized key. Its purpose is to provision access without requiring a password for each login. This facilitates automated, passwordless logins and single sign-on using the SSH protocol. The ssh-copy-id tool is part of OpenSSH. The ssh-copy-id tool, part of the OpenSSH package, is available in all major Linux distribution repositories, and you can use your package manager to install this command. To install the ssh-copy-id tool on Debian, use the following command: sudo apt-get update && sudo apt-get install openssh-client. SCP (Secure CoPy) – is a remote file copy program, that copies files between hosts on a network. It uses SSH for data transfer, and uses the same authentication and provides the same security as SSH. When copying a source file to a target file which already exists, SCP will replace the contents of the.
Handy ssh command of the day: ssh-copy-id
. The ssh-copy-id
command copies your Public Key to a remote machine. It also configures all of the file permissions correctly, because SSH is (rightly) very picky about who can access your SSH configuration.
Step 1: Create an SSH keypair
If you don't already have a id_rsa
and id_rsa.pub
in your $HOME/.ssh/
directory, then you will need to create them. The ssh-keygen
command does this for you. You may optionally add a passphrase to the key. This means that even if someone gets hold of your private key file (such as, a system where somebody else has root
access, or if you need to be certain that the key won't be usable by anybody else if they get hold of it at some time in the future), it won't do them any good, because they will need the passphrase to unlock the key itself.
I cannot think of a situation in which I would create a key without a passphrase; if there is no passphrase, then as soon as somebody gets that file, they can impersonate you.
By default, it will create the Private key in $HOME/.ssh/id_rsa
and the Public key in $HOME/.ssh/id_rsa.pub
. You can give it a different prefix if you like, when it prompts you below.
Here, you could press ENTER to create a key with no passphrase, though in this example, we use a passphrase of sshkey-passphrase.
Step 2: Copy the key to the remote box
This is where ssh-copy-id
comes in. You will have to enter the password to log in to the remote box, of course, because you do not (yet!) have SSH key-based login enabled.
If this is the first time that you have connected to remotebox
, SSH will also say that 'The authenticity of host 'remotebox (192.168.1.92)' can't be established
'. This is because the two machines share the machine-level SSH public keys, so that you can tell, on subsequent connections, that it is the same machine that you connected to before (not a substitute, aka 'man-in-the-middle', trying to steal your authentication tokens). Assuming you have reason to trust that remotebox
is the machine you think it is, it is safe to say yes here.
Note: By default, ssh-copy-id
will copy the most recent $HOME/.ssh/id*.pub
file to the remote box, and add it to the $HOME/.ssh/authorized_keys
file there. That is normally what you want. However, you can specify a different keyfile to copy with the -i
switch: ssh-copy-id -i ~/.ssh/jenkins_id_rsa.pub remotebox
.
Because security is at the heart of SSH, this ssh-copy-id
tool finally asks you to confirm that it has done what you expected, and not - for example - added some other guy's keys to remotebox
so that they can log in to it, too.
Step 3: Test it
Now, you should be able to simply 'ssh remotebox
' and log in without a password. However, you will still be prompted to enter the passphrase of the SSH key, if you gave it a passphrase when you created the keypair with ssh-keygen
in Step 1:
Step 4: ssh-add and ssh-agent
You can also add your SSH key to your SSH Agent. This is a piece of software which will run in memory, holding your SSH keys (unlocked, if they have a passphrase), so that you never have to enter their passphrase for as long as the ssh-agent
is running.
Any subsequent uses of /home/steve/.ssh/id_rsa
, within this shell session, will use the key without asking you for the passphrase. Most window managers (GNOME, etc), will store the key within their session, meaning that any new shell that you start, will also inherit access to the same ssh-agent
, so that any terminal window you use will be able to use that facility.
The alternative
The alternative to using ssh-copy-id
is to copy the key file to remotebox
, create the $HOME/.ssh
directory, set 0700
(-rwx------
) permissions, create $HOME/.ssh/authorized_keys
with 0644
(-rw-r--r--
) permissions, then test it.
Footnote
The reason for the 'eval `ssh-agent`
' syntax above, is that ssh-agent
will launch the ssh-agent
as a background process, and then output the following text:
This, if read in via the 'eval `ssh-agent`
' method, will set those variables (SSH_AUTH_SOCK
and SSH_AGENT_PID
) in your shell. This allows your shell to communicate with the agent, which is now running in the background.
Ssh Copy File To Local
Invest in your career. Buy my Shell Scripting Tutorial today:
SCP (Secure CoPy) – is a remote file copy program, that copies files between hosts on a network.
It uses SSH for data transfer, and uses the same authentication and provides the same security as SSH.
When copying a source file to a target file which already exists, SCP will replace the contents of the target file. If the target file does not yet exist, an empty file with the target file name is created, then filled with the source file contents.
Copy Ssh Public Key
Example 1: Copy the file “file.txt” from a remote host to the local host.
Example 2: Copy the file “file.txt” from the local host to a remote host.
Example 3: Copy the directory “dir1” from the local host to a remote host’s directory “dir2”.
Csveditorpro. Example 4: Copy the file “file.txt” from remote host “remote.host1” to remote host “remote.host2”.
Example 5: Copy the files “file1.txt” and “file2.txt” from the local host to your home directory on the remote host.
Ssh Copy Rsa
Example 6: Copy the file “file.txt” from the local host to a remote host using port 2222.
Example 7: Copy the file “file.txt” from the local host to a remote host’s home directory. Preserve the modification and access times, as well as the permissions of the source-file in the destination-file.
Example 8: Copy the file “file.txt” from the local host to a remote host’s home directory. Increase SCP speed by changing the cipher from the default AES-128 to Blowfish.
Ssh Copy File
Example 9: Copy the file “file.txt” from the local host to a remote host’s home directory. limit the bandwidth used by SCP command to 100 Kbit/s.
Copy Ssh Key To Windows
Example 10: Copy multiple files from the remote host to your current directory on the local host. How to get clustertruck for free mac.