Upload File Remote Server Command Line Windows
SFTP stands for secure file transfer program similar to ftp. The simply difference is that Unix or Linux SFTP control performs all operations over an encrypted SSH ship. It may besides utilise many features of ssh, such equally public key hallmark and pinch.
I have already written another article with the steps to setup SFTP server with passwordless login using authorized_keys in Unix and Linux. So I will employ the aforementioned SFTP server to demonstrate single line SFTP commands to transfer files and directory from local to remote and remote to local server.
Some more articles on related topic to transfer files and folders between two servers:
- 2 commands to copy folder from local to remote server or vice versa in Linux
- five commands to copy file from one server to another in Linux or Unix
- Securely transfer files betwixt two hosts using HTTPS in Linux
Lab Environment
SFTP Server:
Hostname: sftp-server
IP Address: 192.168.43.83
SFTP Client:
Hostname: sftp-client
IP Address: 192.168.43.82
1. Copy file from remote server to local auto windows
Nosotros can utilize connect to our SFTP Server from Windows auto using whatever SFTP Customer such equally WinSCP, FileZilla etc. The SFTP syntax for destination may be specified either every bit [user@]host[:path]
or as a URI in the form
sftp://[user@]host[:port][/path]
I will apply WinSCP on my Windows Laptop to connect to our Linux SFTP Server. Provide the IP Accost and other User credential to login to SFTP server using WinSCP
Once connected you lot tin at present copy file from remote server to local auto windows or vice versa.
Assuming the user deepak
has enough privilege you should be able to re-create file from remote server to local car windows or vice versa.
2. Single line SFTP get command to download file from remote to local server
Employ below SFTP syntax to download file from remote to local server using SFTP become command.
sftp [user]@host[:port][/dest_path/file] [/local_path]
I take dest_dir
folder on sftp-server
on which I will create a new file (sftp-server_file
) and then we volition download this file on sftp-client
using SFTP go command.
[root@sftp-server dest_dir]# pwd /opt/sftp-jails/deepak/dest_dir [root@sftp-server dest_dir]# touch sftp-server_file
Connect to sftp-server
using sftp-client
and download sftp-server_file
to /tmp
on sftp-client
using SFTP get command.
[root@sftp-customer ~]# sftp deepak@sftp-server:dest_dir/sftp-server_file /tmp/ Continued to sftp-server. Fetching /dest_dir/sftp-server_file to /tmp/sftp-server_file
Since I had configured passwordless sftp, nosotros did non got any SFTP command line password prompt
3. Single line SFTP put control to upload file from local to remote server
Apply below SFTP syntax with to upload file from local to remote server using SFTP put command.
sftp [user]@host[:port][/dest_path] <<< $'put /local_path/file'
I will create a file sftp-client_file
under /tmp
on sftp-client
[root@sftp-client ~]# touch on /tmp/sftp-client_file
Upload /tmp/sftp-client_file
to sftp-server
using single line SFTP put command
[root@sftp-client ~]# sftp deepak@sftp-server:dest_dir/ <<< $'put /tmp/sftp-client_file' Connected to sftp-server. Irresolute to: /dest_dir/ sftp> put /tmp/sftp-client_file Uploading /tmp/sftp-client_file to /dest_dir/sftp-client_file /tmp/sftp-client_file
As y'all see the one line SFTP control line file upload was successful and since nosotros are using SFTP authorized_keys, we did not got any SFTP command line password prompt
4. Single line SFTP commands to download directory from remote to local server
Nosotros will use the same SFTP syntax every bit we used for SFTP get command to download file from remote to local server with an additional SFTP argument
sftp -r [user]@host[:port][/dest_path] [/local_path]
I have created sftp-server_dir
on sftp-server
[root@sftp-server dest_dir]# pwd /opt/sftp-jails/deepak/dest_dir [root@sftp-server dest_dir]# mkdir sftp-server_dir
Next nosotros will use unmarried line SFTP get command line to download directory from remote to local server under /tmp
[root@sftp-client ~]# sftp -r deepak@sftp-server:dest_dir/sftp-server_dir /tmp/ Continued to sftp-server. Fetching /dest_dir/sftp-server_dir/ to /tmp/sftp-server_dir Retrieving /dest_dir/sftp-server_dir
As you see the unmarried line SFTP command line to download directory from remote to local server was successful and since nosotros are SFTP authorized_keys, we did not got whatsoever SFTP command line countersign prompt.
Verify the directory on sftp-client
nosotros only downloaded nether /tmp
using SFTP command:
[root@sftp-client ~]# ls -ld /tmp/sftp-server_dir/ drwx------ 2 root root 4096 April 2 17:42 /tmp/sftp-server_dir/
Where does SFTP download to?
In the above SFTP usage example we had explicitly provided the path to download files and directory. By default the local directory under which you lot accept executed SFTP will be the "local path" where SFTP will by default download the files to if you have not given any source directory.
SFTP Usage Example:
Below SFTP usage example will articulate your incertitude on "where does sftp download to"
[root@sftp-client ~]# sftp deepak@sftp-server Connected to deepak@sftp-server. <-- Connected to sftp-server sftp> cd exchange/ <-- Modify destination directory on sftp-server to exchange sftp> get file1 <-- SFTP get control to download sftp-server:exchange/file1 to sftp-customer Fetching /exchange/file1 to file1 <-- This will get sftp-server:commutation/file1 under /root/ on sftp-client every bit nosotros executed sftp from /root/ sftp> become file1 /tmp/ <-- SFTP get control to download exchange/file1 from sftp-server to /tmp/ on sftp-client Fetching /exchange/file1 to /tmp/file1 sftp> lcd /dwelling house/rahul/ <-- SFTP lcd command to modify local directory path to /domicile/rahul on sftp-client sftp> go file1 <-- SFTP go control. This will download sftp-server:commutation/file1 under /domicile/rahul on sftp-customer Fetching /exchange/file1 to file1 sftp> lmkdir test_dir <-- SFTP lmkdir control to create directory on local server sftp> mkdir sftp-server_dir <-- SFTP mkdir command to create directory on sftp-server sftp> ls -fifty <-- List the files and directories on sftp-server -rw------- 1 deepak 1003 800 Mar 31 17:twenty checksum_datababse.out -rw-r--r-- one deepak 1003 0 Apr 2 16:10 file1 -rw-r--r-- 1 deepak 1003 0 April ii 16:10 file2 -rw-r--r-- i deepak 1003 0 Apr 2 16:08 file6 -rw-r--r-- ane deepak 1003 0 April 2 sixteen:08 file7 -rw------- 1 deepak 1003 24 Mar 31 17:20 howdy.txt drwx------ 2 deepak 1003 4096 Apr three 17:27 sftp-server_dir -rw------- one deepak 1003 59 Mar 31 17:20 upload_dir.ba sftp> lpwd <-- SFTP lpwd command to impress nowadays working directory on local server Local working directory: /home sftp> pwd <-- SFTP pwd command to print present working directory on remote server Remote working directory: /exchange sftp> df -h <-- SFTP df control to bank check disk usage for shared path on remote server Size Used Avail (root) %Capacity 13.3GB six.3GB 6.3GB 7.0GB 47% sftp> exit
We have used below commands in this SFTP usage example:
lcd -> To change directory on local seever i.e. SFTP Client lpwd -> Listing present working directory on local server i.east. SFTP Client lmkdir -> Create directory on local server i.eastward. SFTP Customer cd -> Modify directory on remote server i.e. SFTP Server go -> Download file from remote to local server i.e. SFTP Server ls -> List files and directories on remote server i.e. SFTP Server pwd -> Impress present working directory on remote server i.east. SFTP Server df -> Print deejay usage for destination directory on remote server i.east. SFTP Server
5. SFTP commands to upload directory from local to remote server
I could non notice a unmarried line SFTP put command to upload directory from local to remote server, if y'all come across whatsoever such one liner SFTP put command do let me know via comment section
I accept used an alternate approach to combine all the SFTP commands by using EOF
tag.
Of import NOTE:
While trying to upload directory from local to remote server information technology is important that the directory name which you program to upload, a directory with the same name also must be on your SFTP server. For instance I have mydir
on sftp-customer
, before I attempt to transfer I also take to create "mydir
" on sftp-server
earlier I attempt the upload from local to remote server.
[root@sftp-client ~]# sftp deepak@sftp-server <<EOF > cd dest_dir > mkdir sftp-client_dir > put -r /tmp/sftp-client_dir <-- SFTP put command to upload /tmp/sftp-client_dir to sftp-server > quit > EOF Continued to deepak@sftp-server. sftp> cd dest_dir sftp> mkdir sftp-client_dir sftp> put -r /tmp/sftp-client_dir Uploading /tmp/sftp-client_dir/ to /dest_dir/sftp-client_dir Entering /tmp/sftp-client_dir/ sftp> quit
Annotation:
Information technology is possible you may go "Couldn't canonicalize: No such file or directory; Unable to canonicalize path XXX
". This ways that the directory does non exist on SFTP server which you are planning to upload. Using SFTP CLI this is a pre-requisite to have a directory already be with the same name before uploading directory.
sftp> put -r /tmp/sftp-client_dir sftp-client_dir Uploading /tmp/sftp-client_dir/ to /sftp-client_dir Couldn't canonicalize: No such file or directory Unable to canonicalize path "/sftp-client_dir"
6. SFTP commands to transfer file using batch file
You can automate SFTP file transfer in Unix and Linux using batch file. I accept explained more than nigh batch file in another article so I will not cover this role again. utilize the below SFTP Syntax to utilize batch file and automate file transfers
sftp -b <batch_file> [user]@server:[port]
I accept created below SFTP batch file to upload directory (sftp-client_dir
) from local to remote server.
[root@sftp-client ~]# cat upload_dir.ba cd dest_dir mkdir sftp-client_dir put -r /tmp/sftp-client_dir quit
This batch file will create sftp-client_dir
on sftp-server
and and then upload directory sftp-client_sir
from sftp-client
to the SFTP server.
[root@sftp-client ~]# sftp -b upload_dir.ba deepak@sftp-server sftp> cd dest_dir sftp> mkdir sftp-client_dir sftp> put -r /tmp/sftp-client_dir Entering /tmp/sftp-client_dir/ sftp> quit
Verify if the sftp-client_dir
was successfully uploaded to sftp-server
. Since we have configured SFTP authorized_keys to perform passwordless sftp, did not got any SFTP command line password prompt
[root@sftp-server dest_dir]# ls -fifty full 4 drwxr-xr-x ii deepak deepak 4096 Mar 31 20:14 sftp-client_dir
7. Unmarried line SFTP commands to remove directory on remote server
The SFTP syntax to remove directory on sftp-server
using 1 liner SFTP command line is same every bit we used to upload file from local to remote server
Using the beneath one liner SFTP rmdir command we will remove sftp-client_dir
on sftp-server
. Since this directory is under exchange
binder of sftp-server
hence I have provided this path while connecting to sftp-server
.
[root@sftp-client ~]# sftp deepak@sftp-server:exchange/ <<< $'rmdir sftp-client_dir' Connected to sftp-server. Irresolute to: /substitution/ sftp> rmdir sftp-client_dir
We have successfully removed sftp-client_dir
from our sftp-server
which was inside exchange
folder. Since nosotros have configured SFTP authorized_keys to perform passwordless sftp, did not got whatsoever SFTP command line countersign prompt
eight. Transfer files with SFTP commands using ssh_config
You tin can create a config file with SSH Client configuration values which volition exist used past SFTP command to transfer files. Use beneath SFTP syntax to utilise ssh_config
sftp -F <config_file> [user]@server:[port]
I take created beneath ssh config files with some values which will exist used by our sftp-client
[root@sftp-client ~]# cat /tmp/ssh_config AddressFamily inet ConnectionAttempts x ForwardAgent no ForwardX11 no ForwardX11Trusted no GatewayPorts no HostBasedAuthentication no PubkeyAuthentication yep HostKeyAlias sftp-alias PasswordAuthentication no Compression yes ServerAliveCountMax iii ServerAliveInterval fifteen TCPKeepAlive no User deepak
We will use this ssh_config
file with SFTP control line in combination with -F
argument every bit shown below
[root@sftp-client ~]# sftp -F /tmp/ssh_config sftp-server Connected to sftp-server. sftp> leave
You can combine this with batch file which I used in above SFTP usage examples to automate SFTP transfer files between local and remote servers. Since nosotros have configured SFTP authorized_keys to perform passwordless sftp, did non got any SFTP control line password prompt.
9. Use SSH options with SFTP single commands
If y'all do not wish to create additional SSH config file then you can also pass those arguments using -o
argument with SFTP control. Utilise below SFTP syntax:
sftp [-o <ssh_option_1> -o <ssh_option_2> -o <ssh_option_3> ..] [user]@server:[port]
I have used some of the SSH customer options in the below SFTP usage example to utilize Public Cardinal Authentication with our SFTP.
[root@sftp-client ~]# sftp -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -o "PreferredAuthentications=publickey" -o "StrictHostKeyChecking=no" -o User=deepak sftp-server Connected to sftp-server. sftp> exit
Since we have configured SFTP authorized_keys to perform passwordless sftp, we did not got whatever SFTP command line password prompt
10. Apply dissimilar Port with SFTP Commands to transfer files
By default SFTP uses the same port as SSH i.eastward. 22. Merely you can besides configure SSH and SFTP to use a different port by using Port=<value>
in /etc/ssh/sshd_config
on SFTP server. In this SFTP usage example I have setup SFTP to use port 2200 instead of port 22.
We tin can connect using a custom SFTP port using -o Port=<value>
as shown below
[root@sftp-client ~]# sftp -o Port=2200 -o User=deepak sftp-server Connected to sftp-server. sftp> exit
or alternatively we tin likewise utilize SFTP argument -P <value>
to ascertain a custom port for SFTP connection.
[root@sftp-customer ~]# sftp -P 2200 -o User=deepak sftp-server Continued to sftp-server. sftp> exit
Lastly I promise the SFTP usage examples from the article to apply one liner SFTP commands in Unix and Linux to transfer files was helpful. Then, allow me know your suggestions and feedback using the comment department.
References:
man page for sftp
Related Searches: linux sftp command line example, sftp become command, sftp usage, sftp syntax, sftp put command, sftp upload files, copy file from remote server to local machine windows, Where does sftp download to, sftp commands
freemanforneved1945.blogspot.com
Source: https://www.golinuxcloud.com/single-line-unix-linux-sftp-commands-example/
ارسال یک نظر for "Upload File Remote Server Command Line Windows"