QuickTip: Transferring files between two Ubuntu systems

Posted on Sunday, February 3, 2008

10


Easy way to transfer files between Ubuntu systems is by using scp and sftp. The SSH File Transfer Protocol or SFTP is a network protocol that provides file transfer and manipulation functionality over any reliable data stream. It is typically used with the SSH-2 protocol (TCP port 22) to provide secure file transfer, but is intended to be usable with other protocols as well.

STEP I

Make sure that the 2 systems can talk to each other

You will need the network address of both machines – I will call them A and B.

Ping test – using the IP address of B try to ping it from A: From machine A, open a terminal window and type ‘ping’ followed by the IP address of machine B, for example:

ping 192.168.0.1

You should get messages like ‘xxx bytes received from 192.168.0.1 in 8 ms’ – if you get errors, this is your first problem.

If you got no errors, then repeat from machine B, pinging machine A.

Once you are sure your basic networking is functioning correctly, you can go back to setting up your data transfer.

STEP II

Make sure that SSH server is running on the remote machine to which you want to connect.

Test by issuing the command

ssh user@remotehost

If not follow the following steps for setting up the SSH server

Type the following two command to install both ssh client and server:

# sudo apt-get install openssh-server openssh-client

To be frank your server is ready by default. Just test it from your home computer or from same system with the command:

# ssh localhost

OR

# ssh user@your-server-ip-address

Assuming that your server hostname is userver.mydomain.com and username is vikas, you need to type the following command:

# ssh vikas@userver.mydomain.com

you might be asked for a password to connect to the server.

To stop ssh server, enter:

# sudo /etc/init.d/ssh stop

To start sshs server, enter:

# sudo /etc/init.d/ssh start

To restart ssh server, enter:

# sudo /etc/init.d/ssh restart

OK back to file transfer. Let us assume that you want to transfer data between User richa on one machine and user vhazrati on another machine

Using sftp

richa@richa-laptop:~$ sftp vhazrati@192.168.0.107

Connecting to 192.168.0.107…

vhazrati@192.168.0.107’s password: <enter the password here>

sftp>

Now you can transfer files using the command

sftp> get vikas.sql /home/richa/Documents

Other useful sftp commands

cd path Changes remote directory to path.

lcd path Changes local directory to path.

chgrp grp path Changes group of file path to grp. grp must be a numeric GID.

chmod mode path Changes permisssion of file path to mode.

chown own path Changes owner of file path to own. own must be a numeric UID.

exit Quits sftp

get remote-path [local-path] Retrieves the remote-path and stores in on the local machine. If the local-path name is not specified, it is given the same name that it has on the remote machine.

help Displays help text.

lmkdir path Creates local directory specified by path.

ln oldpath newpath Creates a symbolic link from oldpath to newpath.

lpwd Prints local working directory.

ls [path] Displays remote directory listing of either path or current directory (if path is not specified).

mkdir path Creates remote directory in location specified by path.

put local-path [remote-path] Uploads local-path and stores it on the remote machine. If the remote-path name is not specified, it is given the same name as it has on the local machine.

pwd Displays remote working directory.

quit Quits sftp

rename oldpath newpath Renames remote file from oldpath to newpath.

rmdir path Removes remote directory specified by path.

rm path Deletes remote file specified by path.

symlink oldpath newpath Create a symbolic link from oldpath to newpath.

Using scp

The scp syntax is

scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 […] [[user@]host2:]file2

for more details about the options do “man scp” on your linux terminal.

Secure file transfers to and from local or remote systems can be initiated with the scp UNIX command.

To use the scp command to copy files between systems, use the following command:

scp filename1 userid@hostname:filename2

where filename1 is the file on the local system that you wish to copy, userid@hostname is the userid and hostname where you wish to copy it, and filename2 is the name you want to call the file on the remote system. For example:

scp myfile richa@richa-laptop:myfile

Note: SCP may also be used to copy files from a remote system to a local system. To do this in the first example above, reverse the order of filename1 and userid@hostname:filename2.

When this command is typed on the Glue system, it will copy the file to the richa system.

When you issue the command, you’ll be prompted for the password on the remote system. You will then be given the stats of the transfer. Pay attention to the second item on the stat line; it’s how much of the file got transferred (you want to see 100%). For example:

scp myfile richa@richa-laptop:myfile
richa@richa-laptop’s password:
myfile 100% |***************************| 1232 00:00

The password will not show up when typed. The statistics indicate the percentage of the file that was transferred, the size of the file, and the estimated time to transfer the file (in this case, immediate). The scp command gives you three chances to get the password right before it aborts the connection.

To use the scp command to copy directories between systems, use the following command:

scp -r directoryname userid@hostname:directoryname2

where directoryname is the directory on the local system you wish to copy, userid@hostname is the userid and hostname where you wish to copy it, and directoryname2 is the name you want to call the file on the remote system. For example:

scp -r mydirectory richa@richa-laptop:mydirectory

Note: SCP may also be used to copy directories from a remote system to a local system. To do this in the first example above, reverse the order of directoryname and userid@hostname:directoryname2.


AddThis Social Bookmark Button

Posted in: linux