How to use ssh and rsync commands in Linux?
Suppose you want to work on a computer/server which is situated 20K KM far from you. In this case you have two options which are as follows
- You need to travel 20K KM to reach that place to access that computer/server
- You can login server from your working place.
According to me first method will not be physible for everyone. But instead of this second method can be used. Which is as follows
ssh is command which is used to login into a remote machine from you local machine. This command provides secure encrypted communications between remote and local machine over an insecure network.
- ssh stands for Secure Shell.
- $ssh [options] user_name@Remote_IP_address
The ssh command can also be used to remotely execute commands at the remote machine without logging in to a shell prompt. The syntax is as follows:
- $ssh hostname command
How to use ssh?
-1 Protocol version 1 will be used only.
-2 Protocol version 2 will be used only.
-4 IPv4 addresses will be used only.
-6 IPv6 addresses will be used only.
-A Enable forwarding of the authentication agent connection.
-a Disable forwarding of the authentication agent connection.
-C Used for data compression
-c cipher_spec Selects the cipher specification for encrypting the session.
-D [bind_address:] port Dynamic application-level port forwarding. A socket will allocated to listen to port on the local side. After establishment of a connection at this port, the connection will forwarded over the secure channel, and the application protocol is then used to determine where to connect to from the remote machine.
-E log_file Append debug logs to log_file instead of standard error.
-F configfile Specifies a per-user configuration file. The default for the per-user configuration file is ~/.ssh/config.
-g Allows remote hosts to connect to local forwarded ports.
-i identity_file A file from which the identity key (private key) for public key authentication is read.
-J [user@]host[:port] Connect to the target host by first making a ssh connection to the pjump host[(/iam/jump-host) and then establishing a TCP forwarding to the ultimate destination from there.
-l login_name Specifies the user to log in as on the remote machine.
-p port Port to connect to on the remote host.
-q Quiet mode.
-V Display the version number.
-v Verbose mode.
-X X11 forwarding will be enabled.
-x X11 forwarding will be disabled.
cp do not work longer to transfer file from remote machine to local machine and vice-versa. But by using rsync command it can be made possible. rsync can also be used to transfer data within local machine.
- rsync stands for remote sync
In this article I used two systems, whose user names are as follow:
Within local machine
$rsync [ option ] path/to/source path/to/destination
Local to Remote machine
while using this command you need to be at your local machine instead of remote
$rsync [ option ] path/to/source email@example.com:/path/to/destination
Remote to Local machine
While using this command you need to be at your local machine instead of remote
$rsync [option] firstname.lastname@example.org:/path/to/source path/to/destination
Note: To transfer a directory from local to remote or vice-vers , you need to use option -avzh as follows
From remote to local machine
$rsync -avzh email@example.com:/path/to/source path/to/destination
Commonly used options
- -v : verbose is used to increase verbosity
- -r : copies data recursively
- -a : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships and timestamps
- -z : z is used to compress file data
- -h : human-readable, output numbers in a human-readable format
- -u :update skip files that are newer on the receiver
- -d :dirs transfer directories without recursing
Why do you use rysnc ?
- Support for copying links, devices, owners, groups, and permissions
- A CVS (current version program ) exclude mode for ignoring the same files that CVS would ignore
- Can use any transparent remote shell, including ssh or rsh
- Does not require super-user privileges
- Pipelining of file transfers to minimize latency costs
- Support for anonymous or authenticated rsync daemons (ideal for mirroring)
- Less bandwidth is required as it uses compression and decompression method during sending and receiving data.
rsync for local machine
rsync can be used to copy data within local machine as follows
Copy/Sync a file using rsync
$rysnc -av path/to/source path/to/destination
In this example you have to transfer a file form directory test1 to test2
Copy/Sync a directory using rsync
$rsync -avzh path/to/source/dire path/to/destination
Note: Don’t use “/” at the send of source path else it will copy all content of source directory into destination directory instead of source directory
Here, in this example I will transferring tset1 directory into test2
How to use rsync along with ssh ?
rsync command can be used along with ssh. In this case you can transfer files or directories from local to remote and vice-vers with logging into remote machine.
Also when you use rsync you need to provide the remote_user password to accomplish that particular task, so using ssh option will send your logins in an encrypted manner so that your password will be safe.
Remote machine to local machine
To use rsync along with ssh you need to specify option “e” with previously used options, syntax is as follows
$rsync -avzhe ssh firstname.lastname@example.org:/path/to/source path/to/destination
Local machine to remote machine
$rsync -avzhe ssh path/to/source email@example.com:/path/to/destination
How to use rysnc where you do not have destination directory ?
Using rsync command you can create a directory while it is running or processing. syntax is as follows
$rsync -avzh /path/to/source/ path/to/destination
Use “/” at the end of source path, if you want to copy only contents from source directory to destination else whole directory will copied into destination.