37

I was wondering if there is a way to use Samba to send items to a client machine via the command line (I need to send the files from the Samba server). I know I could always use scp but first I was wondering if there is a way to do it with Samba. Thanks!

t0xic
  • 475
  • 1
  • 4
  • 5

7 Answers7

34

curl supports the smb v1 protocol since v7.40:

curl --upload-file /path/to/file.ext  -u 'DOMAIN\Username' smb://172.16.17.52/ShareName/

SMB v1 is not available by default in Windows anymore so this won't work with current Windows shares in their default configuration.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
eadmaster
  • 1,573
  • 4
  • 18
  • 30
  • 1
    This worked great for me: `curl --upload-file /home/me/local_file.txt --user "OurWindowsDomain\UserName:thePassword" smb://172.16.17.52/ShareName/Path/To/Remote/Dir`. I got a list of all the available sharenames with `smbclient -L //172.16.17.52 -U UserName%thePassword -W OurWindowsDomain` – Matthias Braun Feb 04 '19 at 11:38
  • this is perfect! – Psychozoic Nov 04 '19 at 18:54
  • Can I use `curl --upload-file` and create a new directory on samba? `--create-dirs` doesn't work. – Corey Jun 18 '20 at 10:21
32

Use smbclient, a program that comes with Samba:

$ smbclient //server/share -c 'cd c:/remote/path ; put local-file'

There are many flags, such as -U to allow the remote user name to be different from the local one.

On systems that split Samba into multiple binary packages, you may have the Samba servers installed yet still be missing smbclient. In such a case, check your package repository for a package named smbclient, samba-client, or similar.

Warren Young
  • 71,107
  • 16
  • 178
  • 168
  • I keep getting `Connection to [IP] failed (Error NT_STATUS_CONNECTION_REFUSED)` – t0xic May 30 '15 at 00:18
  • When I wrote `//server` above, I mean what you are calling the client machine, which in this case is *acting* as a server. It needs to have a folder or drive shared via SMB for this to work. – Warren Young May 30 '15 at 00:27
  • Oh... that won't really work for me. I guess I'll just use `scp`. Thanks anyway though! – t0xic May 30 '15 at 11:09
  • 1
    @fleebow8: You can install a third-party SCP server on the client machines, but you can't right-click a folder in Windows Explorer and say "Share"? – Warren Young May 30 '15 at 17:43
  • I have a specific project I'm doing where I want everything to be automatic. – t0xic May 30 '15 at 22:11
  • @fleebow8: Once a share is established, it stays established. Any process that can install an SCP server can enable an SMB share. In fact, the latter is easier, since it's built into Windows. – Warren Young May 30 '15 at 22:15
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/24305/discussion-between-fleebow8-and-warren-young). – t0xic May 30 '15 at 22:17
  • 2
    how would you `put` a `local-file` that wasn't in your current directory? – waspinator Jan 11 '19 at 03:15
  • When trying programatically perform ops, //hostname/sharename are mandatory parts of having an address to connect with the smb server, after these two directories of upload would be considered e.g.: //hostname/sharename/parentFolder/childFolder, to perform CD on directories on a string one must remove hostname and sharename initially. – simultsop Mar 23 '23 at 07:15
5

The easiest way I found is using smbget

smbget smb://PATH/TO/FILE/test.txt

If authentication is needed (if password is not provided, it will prompt for password):

smbget smb://PATH/TO/FILE/test.txt -U "[email protected]%myPassword"

You can also specify where to save the file and the name of the file after copy:

smbget smb://PATH/TO/FILE/test.txt -U "[email protected]%myPassword" -o /LINUX/PATH/remote_test_file
LoMaPh
  • 495
  • 6
  • 10
3

Really working will be this:

$ smbclient //server/share -c 'cd c:/remote/path ; put local-file remote-file'

local-file - file from local machine

remote-file - copy to this file on remote machine

Matthias Braun
  • 7,797
  • 7
  • 45
  • 54
2

I did lot of research and finally I was successful to transfer file using below command through shell file in one shot.

smbclient -m smb2 '//xx.xxx.xxx.xx/share1/' -U domxyz/xyz%password123 -c 'cd "FOLDER 1/FOLDER 2/" ; put FILE1.xlsx'
Archemar
  • 31,183
  • 18
  • 69
  • 104
1

smbclient Version 4.9.5-Debian and curl 7.64.0 didn't work for me (Linux kali 4.19.0-kali4-amd64)

This did:

smbmap -H server -u username -p password --upload local-filename share\\remote-filename
chefarov
  • 330
  • 3
  • 14
0

Another way if the share is already mounted by fuse

If you are running some desktop with shares already mounted by nautilus, caja or any other file manager, you could be using fuse (instead of smbclient).

If so, you may find some mountpoints at:

ls -l /run/user/$UID/gvfs/
drwx------ 1 charlie charlie 0 Feb  2 10:04 smb-share:server=hostname,share=documents

Yes this is a mountpoint!

df -h /run/user/$UID/gvfs/*
Filesystem      Size   Used  Avail  Use% Mounted on
gvfsd-fuse      16.2T  3.6T  12.6T   59% /run/user/1000/gvfs

And you could use it as a regular filesystem.

cp $HOME/myfile \
    /run/user/$UID/gvfs/smb-share:server=hostname,share=documents/destpath/
techno
  • 130
  • 4