How to Copy File from Linux to Windows using Samba smbclient
This is an article where the content specified in the article is specifically written to show how to copy file from Linux operating system to Windows operating system. In order to achieve it, smbclient command tool which is actually a command based on Samba protocol is utilized. Samba itself based on the description given in Wikipedia, it is a free software re-implementation of the SMB/CIFS networking protocol, and was originally developed by Andrew Tridgell. Samba provides file and print services for various Microsoft Windows clients and can integrate with a Microsoft Windows Server domain, either as a Domain Controller (DC) or as a domain member. As of version 4, it supports Active Directory and Microsoft Windows NT domains. So, how can a file is transferred using smbclient ?. Below is the actual command performed to do it given the pattern at first to know the command format :
smbclient '//xxx.xxx.xxx.xxx/tmp' -U Administrator -c "put file_source file_target" -m SMB2 Description : smbclient : it is a command tool for connecting to SMB/CIFS resource in a certain server //xxx.xxx.xxx.xxx/tmp : it is a resource URL of SMB/CIFS location which exist in a server with the IP Address of xxx.xxx.xxx.xxx and 'tmp' as the name of the sharing folder source. -U Administrator : it is an additional parameter for specifying username to connect to the SMB/CIFS resource -c : it is an additional parameter for specifying command string which to be executed "put file_source file_target" : the command string which is going to be executed where in this context is put -m : it is an additional parameter to specify max-protocol or the level of SMB protocol used SMB2 : the level of SMB protocol used which in this context, it is SMB version 2 or SMB2
The above command pattern is used because the previous command used without having the -m SMB2 additional parameter, it cannot be executed successfully. It is because, it will stuck and it generates error as shown below :
root@hostname:/mnt# smbclient '//xxx.xxx.xxx.xxx/tmp' -U Administrator -c "put /home/user/notes.txt home-folder-user" -m SMB2 WARNING: The "syslog" option is deprecated Enter Administrator's password: protocol negotiation failed: NT_STATUS_CONNECTION_RESET root@hostname:/mnt#
It is facing the same situation with the one described in the article titled ‘How to Solve Error Message : negotiation failed: NT_STATUS_CONNECTION_RESET upon connecting to share folder using smbclient protocol’ in this link. So, the above command execution need to be modified first before further execution by adding additional parameter -m SMB2 as shown below :
root@hostname:/mnt# smbclient '//xxx.xxx.xxx.xxx/tmp' -U Administrator -c "put /home/user/notes.txt home-folder-user" -m SMB2 WARNING: The "syslog" option is deprecated Enter Administrator's password: Domain=[WORKGROUP] OS=[] Server=[] putting file /home/user/notes.txt as \notes.txt (23,3 kb/s) (average 23,3 kb/s) root@hostname:/mnt#
Linux smbclient command
On Unix-like operating systems, the smbclient command launches an ftp-like client to access SMB/CIFS resources on servers.
Description
smbclient is a client that is part of the Samba software suite. It communicates with a LAN Manager server, offering an interface similar to that of the ftp program. Operations include things like getting files from the server to the local machine, putting files from the local machine to the server, retrieving directory information from the server and so on.
Syntax
smbclient servicename [ password ] [ -A ] [ -E ] [ -L host ] [ -M host ] [ -I IP number ] [ -N ] [ -P ] [ -U username ] [ -d debuglevel ] [ -l log basename ] [ -n netbios name ] [ -W workgroup ] [ -O socket options ] [ -p port number ] [ -c command string ] [ -T tar options ] [ -D initial directory ]
Options
There is no default password. If no password is supplied on the command line (either here or using the -U option (see below)) and -N is not specified, the client prompts for a password, even if the desired service does not require one. (If no password is required, press Enter to provide a null password.)
smbclient -L ftp -I ftp.microsoft.com
If the receiving computer is running WinPopup the user will receive the message and probably a beep. If they are not running WinPopup the message is lost, and no error message occurs.
The message is also automatically truncated if the message is over 1600 bytes, as this is the limit of the protocol.
One useful trick is to cat the message through smbclient. For example:
cat mymessage.txt | smbclient -M FRED
sends the message in the file «mymessage.txt» to the machine FRED.
You may also find the -U and -I options useful, as they allow you to control the FROM and TO parts of the message.
See the message command section of smb.conf(5) for a description of how to handle incoming WinPopup messages in Samba.
Normally the client attempts to locate the specified LAN Manager server by looking it up: that is, broadcasting a request for the given server to identify itself. Using this parameter will force the client to assume that the server is on the machine with the specified IP number.
Some servers are fussy about the case of this name, and some insist that it must be a valid netbios name.
If no username is supplied, it defaults to an uppercase version of the environment variable USER or LOGNAME in that order. If no username is supplied and neither environment variable exists the username will be empty.
If the service you are connecting to requires a password, it can be supplied using the -U option, by appending a percent symbol («%«) then the password to username. For example, to attach to a service as user «fred» with password «secret«, you would specify -U fred%secret on the command line. Note that there are no spaces around the percent symbol.
If you specify the password as part of username then the -N option (suppress password prompt) is assumed.
If you specify the password as a parameter AND as part of username then the password as part of username takes precedence. Putting nothing before or nothing after the percent symbol causes an empty username or an empty password to be used, respectively.
Note: Some servers (including OS/2 and Windows for Workgroups) insist on an uppercase password. Lowercase or mixed case passwords may be rejected by these servers.
The default value if this parameter is not specified is zero.
The higher this value, the more detail will be logged to the log files about the activities of the client. At level 0, only critical errors and serious warnings will be logged. Level 1 is a reasonable level for day to day running — it generates a small amount of information about operations carried out.
The default base name is specified at compile time.
The base name is used to generate actual log file names. For example, if the name specified was «log«, the following files would be used for log data:
log.client.debug (containing debugging information)
log.client.in (containing inbound transaction data)
log.client.out (containing outbound transaction data)
The default value if this parameter is not specified is 139.
This number is the port number that will be used when making connections to the server. The standard (well-known) port number for the server is 139, hence the default.
smbclient '\\server\share' -TcxIXbgNa [ blocksize ] [ newer-file ] tarfile [ file names. ]
c: Create a tar file on UNIX. Must be followed by the name of a tar file, tape device or «—» for standard output. (May be useful to set debugging low (-d0)) to avoid corrupting your tar file if using «—«). Mutually exclusive with the x flag.
x: Extract (restore) a local tar file back to a share. Unless the -D option is given, the tar files will be restored from the top level of the share. Must be followed by the name of the tar file, device or «—» for standard input. Mutually exclusive with the c flag.
I: Include files and directories. Is the default behavior when file names are specified above. Causes tar files to be included in an «extract» or «create» (and therefore everything else to be excluded). See example below. File name globbing does not work for included files for extractions (yet).
X: Exclude files and directories. Causes tar files to be excluded from an extract or create. See example below. File name globbing does not work for excluded files (yet).
b: Blocksize. Must be followed by a valid (greater than zero) blocksize. Causes tar file to be written out in blocksize*TBLOCK (usually 512 byte) blocks.
g: Incremental. Only back up files that have the archive bit set. Useful only with the c flag.
N: Newer than. Must be followed by the name of a file whose date is compared against files found on the share during a create. Only files newer than the file specified are backed up to the tar file. Useful only with the c flag.
a: Set archive bit. Causes the archive bit to be reset when a file is backed up. Useful with the g (and c) flags.
smbclient '\\mypc\myshare' "" -N -Tx backup.tar
Restore from tar file backup.tar into myshare on mypc (no password on share).
smbclient '\\mypc\myshare' "" -N -TXx backup.tar users/docs
Restore everything except users/docs
smbclient '\\mypc\myshare' "" -N -Tc backup.tar users/docs
Operations
Once the client is running, the user is presented with a prompt, «smb: \>«. The backslash («\«) indicates the current working directory on the server, and changes if the current working directory is changed.
The prompt indicates that the client is ready and waiting to carry out a user command. Each command is a single word, optionally followed by parameters specific to that command. Command and parameters are space-delimited unless these notes specifically state otherwise. All commands are case-insensitive. Parameters to commands may or may not be case sensitive, depending on the command.
You can specify file names which have spaces in them by quoting the name with double quotes, for example «a long file name».
Parameters shown in square brackets (eg., «[parameter]») are optional. If not given, the command uses suitable defaults. Parameters shown in angle brackets (eg., «parameter>») are required.
Note that all commands operating on the server are actually performed by issuing a request to the server. Thus the behavior may vary from server to server, depending on how the server was implemented.
The commands available are given here in alphabetical order.
The masks specified to the mget and mput commands act as filters for directories rather than files when recursion is toggled ON.
The mask specified with the mask command is necessary to filter files within those directories. For example, if the mask specified in an mget command is «source*» and the mask specified with the mask command is «*.c» and recursion is toggled ON, the mget command will retrieve all files matching «*.c» in all directories below and including all directories matching «source*» in the current working directory.
Notes
Some servers are fussy about the case of supplied usernames, passwords, share names (aka service names) and machine names. If you fail to connect try giving all parameters in uppercase.
It is often necessary to use the -n option when connecting to some types of servers. For example, OS/2 LANManager insists on a valid netbios name being used, so you need to supply a valid name that would be known to the server.
smbclient supports long file names where the server supports the LANMAN2 protocol.
Environment Variables
The variable USER may contain the username of the person using the client. This information is used only if the protocol level is high enough to support session-level passwords.
Examples
Lists available shares on host. Using this command would display information similar to the below output:
Domain=[WORKGROUP] OS=[Windows NT 3.51] Server=[NT LAN Manager 3.51] Server=[HOPE] User=[] Workgroup=[WORKGROUP] Domain=[] Sharename type Comment --------- ---- ------- ADMIN$ Disk Remote Admin C$ Disk Default share IPC$ IPC Remote IPC
smbclient '\\hope\ch' password
Connects to the ch share on the networked computer hope using the login password «password«.
Related commands
findsmb — List information about machines networked with the Samba protocol.