Wednesday, October 14, 2015

Transfer files with SSH. centos 6

     

In the following example we will use SCP (Secure Copy) to transfer the files.

# scp [Option] Source Target

# copy the [clado.txt] on local to remote server [easylinuxalways.com]

[clado@easylinux ~]$ scp ./clado.txt clado@easylinuxalways.com:~/

clado@easylinuxalways.com's password:# password of the user

clado.txt                                                100%   10     0.0KB/s   00:00

# copy the [/home/clado/clado.txt] on remote server [easylinuxalways.com] to the local

[clado@easylinux ~]$ scp clado@easylinuxalways.com:/home/clado/clado.txt ./clado.txt

clado@easylinuxalways.com's password:

clado.txt                                                100%   10     0.0KB/s   00:00

In the following example we use SFTP (SSH File Transfer Protocol).

SFTP server function is enabled by default but if not, enable it to add the line [Subsystem sftp /usr/libexec/openssh/sftp-server] in [/etc/ssh/sshd_config].

# sftp [Option] [user@host]

[clado@easylinux ~]$ sftp clado@easylinuxalways.com

clado@easylinuxalways.com's password:# password of the user

Connected to easylinuxalways.com.
sftp>
# show current directory on remote server

sftp> pwd

Remote working directory: /home/clado
# show current directory on local server

sftp> !pwd

/home/redhat
# show files in current directory on FTP server

sftp> ls -l

drwxrwxr-x    2 clado     clado            6 Jul 29 21:33 public_html
-rw-rw-r--    1 clado     clado           10 Jul 28 22:53 clado.txt

# show files in current directory on local server

sftp> !ls -l

total 4
-rw-rw-r-- 1 redhat redhat 10 Jul 29 21:31 clado.txt

# change directory

sftp> cd public_html

sftp> pwd

Remote working directory: /home/clado/public_html
# upload a file to remote server

sftp> put clado.txt redhat.txt

Uploading clado.txt to /home/clado/redhat.txt
clado.txt 100% 10 0.0KB/s 00:00
sftp> ls -l

drwxrwxr-x    2 clado     clado            6 Jul 29 21:33 public_html
-rw-rw-r--    1 clado     clado           10 Jul 29 21:39 redhat.txt
-rw-rw-r--    1 clado     clado           10 Jul 28 22:53 clado.txt

# upload some files to remote server

sftp> put *.txt

Uploading clado.txt to /home/clado/clado.txt
clado.txt 100% 10 0.0KB/s 00:00
Uploading test2.txt to /home/clado/test2.txt
test2.txt 100% 0 0.0KB/s 00:00
sftp> ls -l

drwxrwxr-x    2 clado     clado            6 Jul 29 21:33 public_html
-rw-rw-r--    1 clado     clado           10 Jul 29 21:39 redhat.txt
-rw-rw-r--    1 clado     clado           10 Jul 29 21:45 clado.txt
-rw-rw-r--    1 clado     clado           10 Jul 29 21:46 test2.txt

# download a file from remote server

sftp> get clado.txt

Fetching /home/clado/clado.txt to clado.txt
/home/clado/clado.txt 100% 10 0.0KB/s 00:00
# download some files from remote server

sftp> get *.txt

Fetching /home/clado/redhat.txt to redhat.txt
/home/clado/redhat.txt 100% 10 0.0KB/s 00:00
Fetching /home/clado/clado.txt to clado.txt
/home/clado/clado.txt 100% 10 0.0KB/s 00:00
Fetching /home/clado/test2.txt to test2.txt
/home/clado/test2.txt 100% 10 0.0KB/s 00:00
# create a directory on remote server

sftp> mkdir testdir

sftp> ls -l

drwxrwxr-x    2 clado     clado            6 Jul 29 21:33 public_html
-rw-rw-r--    1 clado     clado           10 Jul 29 21:39 redhat.txt
-rw-rw-r--    1 clado     clado           10 Jul 29 21:45 clado.txt
-rw-rw-r--    1 clado     clado           10 Jul 29 21:46 test2.txt
drwxrwxr-x    2 clado     clado            6 Jul 29 21:53 testdir

# delete a directory on remote server

sftp> rmdir testdir

rmdir ok, `testdir' removed
sftp> ls -l

drwxrwxr-x    2 clado     clado            6 Jul 29 21:33 public_html
-rw-rw-r--    1 clado     clado           10 Jul 29 21:39 redhat.txt
-rw-rw-r--    1 clado     clado           10 Jul 29 21:45 clado.txt
-rw-rw-r--    1 clado     clado           10 Jul 29 21:46 test2.txt

# delete a file on remote server

sftp> rm test2.txt

Removing /home/clado/test2.txt
sftp> ls -l

drwxrwxr-x    2 clado     clado            6 Jul 29 21:33 public_html
-rw-rw-r--    1 clado     clado           10 Jul 29 21:39 redhat.txt
-rw-rw-r--    1 clado     clado           10 Jul 29 21:45 clado.txt

# execute commands with "![command]"

sftp> !cat /etc/passwd

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
...
...
redhat:x:1001:1001::/home/redhat:/bin/bash

# exit

sftp> quit

221 Goodbye.

No comments:

Post a Comment