TrumanWong

rcp

Makes file copying between two Linux hosts easier

Supplementary instructions

rcp command makes file copying operations between two Linux hosts easier. With proper configuration, copying files between two Linux hosts without entering a password is as easy as local file copying.

grammar

rcp(options)(parameters)

Options

-p: retain the attributes of the source file or directory, including owner, group, permissions and time;
-r: Recursive processing, processing files and subdirectories in the specified directory together;
-x: Encrypt all information transmitted between two Linux hosts.
-D: Specify the port number of the remote server.

The home directory of the user with the same name. If no remote username is given, the current username is used. If the path on the remote machine contains special shell characters, it needs to be enclosed in backslashes \\, double quotes "" or single quotes '' so that all shell metacharacters can be interpreted remotely. It should be noted that rcp does not prompt for a password. It performs copying through the rsh command.

directory Each file or directory parameter can be either a remote file name or a local file name. The remote file name has the following form rname@rhost:path, where rname is the remote user name, rhost is the remote computer name, and path is the path to the file.

Parameters

Source file: Specify the source file to copy. There can be multiple source files.

Example

rcp command usage conditions

If there is a /etc/hosts file on the system, the system administrator should ensure that the file contains an entry for the remote host with which to communicate. Configuration process:

Only effective for root user

  1. Create the rhosts file in the root directory of the root users of both parties and add the hostnames of both parties to it. Before this, the other party’s IP and hostname should be added to the /etc/hosts files of both parties.
  2. Start the rsh service. Redhat is not started by default. Method: Execute the ntsysv command, use the space bar before the rsh option to select it, and confirm to exit. Then execute service xinetd restart.
  3. Go to the /etc/pam.d/ directory and comment out the auth required /lib/security/pam_securetty.so line in the rsh file with "#". (Only by commenting out this line can you log in as the root user)

Copy test1 in the current directory to the remote system named webserver1:

rcp test1 webserver1:/home/root/test3

In this case, test1 is copied to the remote subdirectory test3, still named test1. If only the remote hostname is provided, rcp will copy test1 to the remote home directory, still named test1 .

**You can also include the file name in the destination directory. For example, copy the file to a system named webserver1: **

rcp test1 webserver1:/home/root/test3

In this case, copy test1 to the remote directory root and name it test3.

Copy files from remote system: To copy files from remote system to local directory:

rcp remote_hostname:remote_file local_fileEnter

Copy test2 in the remote system webserver1 to the current directory:

rcp webserver1:/home/root/test2 .Enter

. is the abbreviation for "current directory". In this case, test2 in the remote directory is copied to the current directory, still named test2 .

If you wish to copy the file under a new name, provide the destination file name. If you wish to copy test2 to another directory on your local system, use the following absolute or relative pathnames:

rcp webserver1:/home/root/test2 otherdir/ Enter

Or, if you want to copy the file to a different directory with a different file name:

rcp webserver1:/home/root/test2 otherdir/otherfile Enter

Copy directory to remote system:

To copy a local directory and its files and subdirectories to a remote system, use rcp with the -r (recursive) option.

rcp -r local_dir remote_hostname:remote_dir Enter

If there is no local_dir in the current directory, in addition to the local directory name, you also need to provide a relative path name (starting from the current directory) or an absolute path name (starting from the / top-level directory). In addition, if there is no remote_dir in the home directory, remote_dir will need a relative path (starting from the home directory) or an absolute path (starting from / ).

To copy the entire subdirectory named work to a directory named products under the home directory on the webserver1 remote machine, type the following:

rcp -r work webserver1:/home/root/products Enter

This command creates a directory named work and its entire contents under webserver1:/home/root/products (assuming /home/root/products already exists in webserver1).

This example assumes the user is in the local directory containing work . Otherwise, a relative or absolute path to the directory must be provided, such as /home/root/work.

Copy directory from remote system:

To copy a remote directory and all its files and subdirectories to a local directory, use rcp with the -r (recursive) option in the following syntax.

rcp –r remote_hostname:remote_dir local_dir Enter

To copy a remote directory named work to the current directory, type the following:

rcp –r webserver1:/home/root/work .Enter

. represents the current directory. The work directory will be created under this directory.