Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
01-09-2017, 01:30 PM
|
#1
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Rep:
|
Using SFTP to transfer from linux to Windows
How can I, in one line transfer a file from linux to Windows using SFTP?
|
|
|
01-09-2017, 01:34 PM
|
#2
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
Well first you need an SFTP server on your Windows box with the necessary firewall exceptions added. After that, you need an sftp uploading tool on your Linux box since the built-in sftp won't let you do it all in one line (I don't think). There are many options, my preference is "lftp".
|
|
|
01-09-2017, 01:36 PM
|
#3
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
To initiate the sftp from the Linux side to the Windows side you'd have to setup an sshd to listen for sftp connections on the Windows side first. You can do this with Cygwin. Cygwin can be found at:
https://www.cygwin.com/
There are many articles on how to configure sshd for sftp in Cygwin - just do a web search.
To initiate the sftp from the Windows side to the Linux side there are various tools you can use including WinSCP. WinSCP can be found at:
https://winscp.net/eng/download.php
WinSCP uses an explorer style menu to drag files from one system to the other. There is also a WinSCP console you can run that gives you a command line interface to run sftp commands manually.
|
|
|
01-09-2017, 01:39 PM
|
#4
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,598
|
I'd make the opposite assumption from the previous two posts. Since you wish to transfer a file from linux to Windows using SFTP, I'd say put the OpenSSH server on the Linux box. It will be many thousands of times easier. Then you'll still have to figure out a client for the Windows side of things, but at least the server will be done the easy way.
Either way, SFTP can 'put' as well as 'get'
|
|
|
01-09-2017, 01:50 PM
|
#5
|
Moderator
Registered: Aug 2002
Posts: 26,353
|
From the OP's previous threads there is a ssh server running on Windows and can use scp and smbclient to transfer files. What are you trying to accomplish with sftp?
|
|
|
01-09-2017, 01:52 PM
|
#6
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Original Poster
Rep:
|
Well I was told that on the Winodws side they have Globescape EFT. I'm tricky to do this through the command line,. Here's my code:
sftp username@hostname
cd dir/dir
lchdir/export/home/fileishere/file.txt
put file.xlsx
I need to get this to work in one line. Is this possible?
|
|
|
01-09-2017, 01:56 PM
|
#7
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,598
|
Quote:
Originally Posted by trickydba
I need to get this to work in one line. Is this possible?
|
If you use keys for authentication, you can use batch mode -b for that. Batch mode can take SFTP commands via stdin or via a file to be read.
|
|
|
01-09-2017, 01:58 PM
|
#8
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
sftp was designed to prompt humans for detailes such as user name, password, local directory, remote directory, file name, ...
scp was designed for automated machines to transfer data among each other (as long as public key encryption is enabled).
regardless of client or server operating system, as long as the ssh client and server are configured correctly, the general syntax would be:
Code:
scp user1@host1:/whatever/floats/your/boat user2@host2:whatever/grinds/your/gears
Last edited by schneidz; 01-09-2017 at 01:59 PM.
|
|
1 members found this post helpful.
|
01-09-2017, 02:10 PM
|
#9
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Original Poster
Rep:
|
@Turbocapitaist..... Can you please give me an example
|
|
|
01-09-2017, 02:22 PM
|
#10
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,598
|
Here is an example using a file.
Code:
$ cat <<EOF > cmds.sftp
> cd /home/tc/Uploads/
> lcd /home/tc/
> put file.ods
> EOF
cd /home/tc/Uploads/
lcd /home/tc/
put file.ods
$ ssh-add ~/.ssh/some_key_rsa
$ sftp -b cmds.sftp -i ~/.ssh/some_key_rsa tc@server.example.com
If you use - instead of a file name it will read from stdin and then you can use echo via pipe.
Code:
$ ssh-add ~/.ssh/some_key_rsa
$ echo -e "cd /home/tc/Uploads/\nlcd /home/tc/\nput file.ods\n" \
| sftp -b - -i ~/.ssh/some_key_rsa tc@server.example.com
Either way, you have to have the key set up so that it is in the agent if it has a passphrase (recommended) or without a passphrase (not recommended). Because batch mode only works when there is no interactive session.
Last edited by Turbocapitalist; 01-09-2017 at 02:25 PM.
|
|
1 members found this post helpful.
|
01-09-2017, 02:24 PM
|
#11
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
Quote:
Originally Posted by schneidz
sftp was designed to prompt humans for detailes such as user name, password, local directory, remote directory, file name, ...
scp was designed for automated machines to transfer data among each other (as long as public key encryption is enabled).
|
Actually you can do sftp as well as scp with ssh trusts in place. Most of my automated scripts do sftp rather than scp.
|
|
2 members found this post helpful.
|
01-09-2017, 02:29 PM
|
#12
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Original Poster
Rep:
|
@Turbocapitalist............I was kinda hoping to get a one-liner that would handle moving a file from linux to windows. The destination server does not need a password
|
|
|
01-09-2017, 02:32 PM
|
#13
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
Quote:
Originally Posted by Turbocapitalist
Either way, you have to have the key set up so that it is in the agent if it has a passphrase (recommended) or without a passphrase (not recommended). Because batch mode only works when there is no interactive session.
|
ssh keys are definitely the most secure way to go and should always be what you try to get in place for automated processes.
However, there is a utility that allows you to send the password, called sshpass. Obviously this has a security risk as it requires you to store your password in clear text somewhere to be passed. But it does work well if you absolutely can't use keys for some reason and has simple syntax:
shpass -p <password> sftp <target server name or IP>
If you're using RHEL or CentOS you can find the package in Fedora's EPEL.
|
|
|
01-09-2017, 02:35 PM
|
#14
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Original Poster
Rep:
|
@Turbocapitalist.....Im sorry, your code seems a little confusing
And no password is needed, keys already installed
|
|
|
01-09-2017, 02:38 PM
|
#15
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,598
|
Both examples are from any non-Windows OS with the usual OpenSSH SFTP client to whatever SFTP server you have, as long as it follows the standards.
The stdin example is one line. The backslash is not necessary and is added for readability. The file-based example is also one line, but does need the file filled with SFTP commands. Both work as long as you can use keys for authentication. If you are using expect instead of keys then you are probably SOL.
|
|
|
All times are GMT -5. The time now is 02:13 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|