Linux common commands: rcp command


  rcp stands for "remote file copy". This command is used to copy files between computers. The rcp command has two formats. The first format is used for file-to-file copying; the second format is used for copying a file or directory into another directory.

1. Command format:

  rcp [parameters] [source file] [object file]

2. Command function:

  The rcp command is used to copy files or directories remotely. If two or more files or directories are specified at the same time, and the final destination is an existing directory, it will copy all the previously specified files or directories to the directory. .

3. Command parameters:

Meaning of each option:

-r Copies all contents of the source directory recursively to the destination directory. To use this option, the destination must be a directory.

-p Tries to preserve the modification time and mode of the source file, ignoring the umask.

-k Requests rcp to obtain Kerberos permissions for remote hosts within the specified zone instead of obtaining Kerberos permissions for remote hosts within the remote host zone determined by krb_relmofhostCD.

-x Turn on DES encryption for all data transmitted. This affects response time and CPU utilization, but improves security. If the path specified in the filename is not a full pathname, then the path is interpreted relative to the home directory of the user with the same name on the remote machine. 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. Yes, rcp does not prompt for a password, it does the copy through the rsh command.

directory Each file or directory argument can be either a remote filename or a local filename. A remote filename has the form: rname@rhost:path, where rname is the remote user name, rhost is the remote computer name, and path is the path to this file.

4. Example of use:

To use rcp, the following conditions are required:

If there is an /etc/hosts file on the system, the system administrator should ensure that the file contains entries for the remote hosts with which to communicate.

There is a line in the /etc/hosts file that contains the following information for each remote system:

internet_address   official_name   alias

E.g:

9.186.10. *** webserver1.com.58.webserver

.rhosts file

The .rhosts file is located in the home directory of the remote system and contains the name of the local system and the local login.

For example, an entry in a remote system's .rhosts file might be:

webserver1 root

where webserver1 is the name of the local system and root is the local login name. This allows root on webserver1 to copy files to and from the remote system containing the .rhosts file.

Configuration process:

Only valid for root user

1. Create a .rhosts file in the root directory of the root user of both parties, and add the hostname of both parties. Before that, add the IP and hostname of the other party to the /etc/hosts file of both parties

2. Start the rsh service, redhat is not started by default.

Method: execute the ntsysv command, select it with the space bar before the rsh option, and confirm to exit. Then execute: service xinetd restart.

3. Go to the /etc/pam.d/ directory and comment out the line "auth required /lib/security/pam_securetty.so" in the rsh file with "#". (Only comment out this line to log in as root user)

Command use:

Copy files to remote system

To copy files from the local system to the remote system, use the following command:

  rcplocal_fileremote_hostname:remote_fileEnter

Note that if there is no local_file in the current directory, you need to provide a relative path (starting from the current directory) or an absolute pathname (starting with / ) in addition to the local file name.

You only need to specify the full (absolute) path to the remote_file on remote_hostname if you want to put it under a different directory (other than the remote home directory).


 

Use example 1: Copy test1 in the current directory to a remote system named webserver1

Order:

  rcp test1 webserver1:/home/root/test3

illustrate:

  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 filenames in the destination directory. For example, copy the files 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.

Use example 2: Copy files from a remote system: To copy files from a remote system to a local directory

Order:

  rcp remote_hostname:remote_file local_fileEnter

Use example: 3: Copy test2 in the remote system webserver1 to the current directory:

Order:

  rcp webserver1:/home/root/test2 .Enter

illustrate:

The dot (.) is shorthand for "current directory". In this case, test2 in the remote directory is copied to the current directory, still named test2 .

Provide the destination filename if you want the file to be copied under a new name.

If you wish to copy test2 to a different 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 filename:

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

Use Case 4: Copying a Directory to a Remote System: To copy a local directory and its files and subdirectories to a remote system, use both the rcp and -r (recursive) options.

Order:

  rcp –r local_dir remote_hostname:remote_dir Enter

illustrate:

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

 

Use example 5:

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

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

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

This example assumes that 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.

 

Use example 6: 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.

Order:

  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

A dot (.) indicates the current directory. The work directory will be created under this directory.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325168902&siteId=291194637