File Transfer in Unix using scp, ssh, ftp, sftp commands
We will cover below topic in this article:
scp file transfer command in Unix
In Unix, SCP (the scp command) securely copy files and directories between remote hosts without starting an FTP session or logging into the remote systems explicitly. The scp command uses SSH to transfer data, so it requires a password or passphrase for authentication or can use FTP , simplest file transfer protocol to exchange files to and from a remote computer or network.
SCP in Unix allows you to securely copy files and directories between two locations:
- From local system to a remote system.
- Remote system to your local system.
- Between two remote systems from your local system.
- When transferring data with scp, both the files and password are encrypted so that anyone intruding doesn’t get any sensitive information.
SYNTAX: scp [OPTION] sourcefile1 destfile2
options are scp options like ssh port, recursive copy etc.
Local files should be specified using an absolute or relative path, while remote file names should include a user and host specification.
Points to remember:
1. scp command depends on ssh for data transfer, ssh key or password to authenticate on the remote systems.
2. colon (:) is used to distinguish between local and remote locations.
3. Read permissions on the source file and write access on the target system is required to copy file.
Copy Files using scp command in Unix
- Copy a file from local system to remote system
To copy file from local systen to remote system,below command can be used:
scp localfile1.txt [email protected]:/remote/myDataDir
here remote_username is the user of the remote server and /remote/myDataDir is the location on the remote server where file will be copied.
- Copy a file from remote system to local system
To copy a file from remote system to your local system, below command can be used:
scp [email protected]:/remote/myDataDir/remotefile1.txt local/myFiles
- To copy files from one remote server to other remote server, below command can be used:
scp [email protected]:/remote/myDataDir/remotefile1.txt [email protected]:/remote/Directory
rlogin command in Unix
- Copy a file from local system to remote system
- rlogin establishes a remote connection from your terminal to a remote machine.
- allows an authorized user to login to other UNIX machines on a network and to interact
- A secure version of rlogin (slogin) was combined with two other UNIX utilities, ssh and scp, created to replace the earlier utilities.
- rlogin is considered useful for simple logins that don’t require a lot of control over the client/host interaction
SYNTAX: rlogin [options ] username hostname
**Security Issues in rlogin:The main problem with rlogin was that it sent user passwords over a network without any encryption.
This meant that any attacker with access to the network could read user names and passwords.
telnet command in Unix
- Both data transfer including user-id and password is unencrypted
- Known vulnerabilities with telnet
- Telnet is used where a lot of customization is desired, for multiple sessions
ssh(Secure Shell) command in Unix
Both rlogin and telnet are not secure connections with many vulnerabilities helping malicious users/hackers to get access to login details and session content.
ssh stands for “Secure Shell”.
It is used to securely connect to a remote server/system. SSH transfers data in encrypted form between host and client.ssh(secure shell connection) is :
- encrypted form of remote connection
- preferred over most other methods
- SSH client of your choice
- SSH server on the remote machine
- The IP address or name of the remote server
SYNTAX: ssh hostname
hostname is IP address or name of remote server.
Default user is current login user on client machine:
ssh [email protected] (or) ssh –l username hostname [ telnet syntax ]
Command execution through ssh:
ssh [email protected] "ls"
Every ssh login/command execution requests for user password
Generate SSH Keys Using SSH Keygen
- Generate a key pair with the keygen utility to improve security of SSH connections.
- The pair consists of a public and private key.
- The public key can be shared, while the private key needs to stay secure.
- SSH key pairs are used to authenticate clients to servers automatically.
- When you create an SSH key pair, entering password to access a server is not required.
Create a key pair on host machine
ssh-keygen -t rsa
To use default settings, hit Enter on the prompts for file location and passphrase.
Copy Public SSH Key
Copy the public key to a server. The key is the file id_rsa.pub previously created with SSH keygen command above.
Run the command below from the client, to copy your key generated to the server:
Enter the password to authenticate when asked.
After this, you will no longer need to use the password to connect to the same server.
Copy a File Remotely over SSH with SCP
You can securely copy files over the SSH protocol using the SCP tool. The basic syntax is:
scp fileName [email protected]:/home/username/destination
To know more on SCP refer here.
ftp command in Unix
FTP (File Transfer Protocol) is a standard network protocol used to transfer files to and from a remote network.
- Transfer files to and from a remote machine.
- Allows the user to work with files on a remote machine.
Note: FTP traffic is not encrypted. You can use SCP or SFTP or secure transfer.
- Read permission on source file.
- Write permissions on target/remote system.
SYNTAX: ftp [hostname]
here hostname is the remote server IP address or domain name.
Few commands that are used when doing file transfer using FTP command:
- help – list all available FTP commands.
- cd – change directory on the remote machine.
- lcd – change directory on the local machine.
- ls – list the names of the files and directories in the current remote directory.
- mkdir – create a new directory within the current remote directory.
- delete – remove a file in the current remote directory.
- rmdir- remove a directory in the current remote directory.
- get – copy one file from the remote to the local machine.
- mget – copy multiple files from the remote to the local machine.
- put – copy one file from the local to the remote machine.
- mput – copy one file from the local to the remote machine.
put command in Unix
put command is used to transfer file from the source terminal to the destination terminal.
- you can use this command to upload the file.
SYNTAX: put filename
get command in Unix
get command is used to transfer a file from destination terminal to source terminal.
- Use get command to download single file from the remote server.
- File is downloaded to the directory from which ftp command is typed.
SYNTAX: get filename
- multiple files can be downloaded using mget command.
SYNTAX: mget file1 file2 file3
sftp (Secure File Transfer) command in Unix
SFTP (SSH File Transfer Protocol) is a secure file protocol that is used to access, manage, and transfer files over an encrypted SSH transport.
- It is more secure and easier to configure as compared to FTP.
- Supports most of the interactive commands
- SFTP allows you to perform operations on remote files.
- SFTP works on a client-server model
SYNTAX: sftp [email protected]_Server_OR_hostName
Specified file is copied:
sftp [[[email protected]]host[:file [file]]]
Connect to particular directory:
sftp [[[email protected]]host[:dir[/]]]