smbclient用法参考
2012-10-09 14:59:03 阿炯

smbclient主要用在Linux访问windows的共享文件,或者是访问Linux的samba服务器。

功能说明:可存取SMB/CIFS服务器的用户端程序。

语法:smbclient [网络资源][密码][-EhLN][-B<IP地址>][-d<排错层级>][-i<范围>][-I<IP地址>][-l<记录文件>][-M<NetBIOS名称>][-n<NetBIOS名称>][-O<连接槽选项>][-p<TCP连接端口>][-R<名称解析顺序>][-s<目录>][-t<服务器字码>][-T<tar选项>][-U<用户名称>][-W<工作群组>]

补充说明:SMB与CIFS为服务器通信协议,常用于Windows95/98/NT等系统。smbclient可让Linux系统存取Windows系统所分享的资源。

参数:
[网络资源] [网络资源]的格式为//服务器名称/资源分享名称。
[密码] 输入存取网络资源所需的密码。

-B<IP地址> 传送广播数据包时所用的IP地址。
-d<排错层级> 指定记录文件所记载事件的详细程度。
-E 将信息送到标准错误输出设备。
-h 显示帮助。
-i<范围> 设置NetBIOS名称范围。
-I<IP地址> 指定服务器的IP地址。
-l<记录文件> 指定记录文件的名称。
-L 显示服务器端所分享出来的所有资源。
-M<NetBIOS名称> 可利用WinPopup协议,将信息送给选项中所指定的主机。
-n<NetBIOS名称> 指定用户端所要使用的NetBIOS名称。
-N 不用询问密码。
-O<连接槽选项> 设置用户端TCP连接槽的选项。
-p<TCP连接端口> 指定服务器端TCP连接端口编号。
-R<名称解析顺序> 设置NetBIOS名称解析的顺序。
-s<目录> 指定smb.conf所在的目录。
-t<服务器字码> 设置用何种字符码来解析服务器端的文件名称。
-T<tar选项> 备份服务器端分享的全部文件,并打包成tar格式的文件。
-U<用户名称> 指定用户名称。
-W<工作群组> 指定工作群组名称。

常用方法:
smbclient //192.168.18.3/hto -U hto
输入密码

# smbclient //192.168.18.3/hto -U hto
Enter hto's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 3.5.10-0.110.el5_8]
smb: \> ?
?              allinfo        altname        archive        blocksize      
cancel         case_sensitive cd             chmod          chown          
close          del            dir            du             echo           
exit           get            getfacl        hardlink       help           
history        iosize         lcd            link           lock           
lowercase      ls             l              mask           md             
mget           mkdir          more           mput           newer          
open           posix          posix_encrypt  posix_open     posix_mkdir    
posix_rmdir    posix_unlink   print          prompt         put            
pwd            q              queue          quit           readlink       
rd             recurse        reget          rename         reput          
rm             rmdir          showacls       setmode        stat           
symlink        tar            tarmode        translate      unlock         
volume         vuid           wdel           logon          listconnect    
showconnect    ..             !              
smb: \>

命令 说明
?或help [command] 提供关于帮助或某个命令的帮助
![shell command]   执行所用的SHELL命令,或让用户进入 SHELL提示符
cd [目录]  切换到服务器端的指定目录,如未指定,则 smbclient 返回当前本地目录
lcd [目录]  切换到客户端指定的目录;
dir 或ls   列出当前目录下的文件;
exit 或quit    退出smbclient
get file1  file2 从服务器上下载file1,并以文件名file2存在本地机上;如果不想改名,可以把file2省略
mget file1 file2 file3  filen 从服务器上下载多个文件;
md或mkdir 目录 在服务器上创建目录
rd或rmdir 目录 删除服务器上的目录
put file1 [file2] 向服务器上传一个文件file1,传到服务器上改名为file2;
mput file1 file2 filen 向服务器上传多个文件

使用说明
1、列出某个IP地址所提供的共享文件夹
smbclient –L IP或者NETBIOS名称,作用是显示某台提供samba服务的服务器上的共享资源。命令执行效果如下:
#查看linux服务器的共享。
# smbclient -L 192.168.18.3 -U hto%passwd
Domain=[MYGROUP] OS=[Unix] Server=[Samba 3.5.10-0.110.el5_8]
    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       IPC Service (Samba Server Version 3.5.10-0.110.el5_8)
    hto             Disk      Home Directories
Domain=[MYGROUP] OS=[Unix] Server=[Samba 3.5.10-0.110.el5_8]
    Server               Comment
    ---------            -------
    Workgroup            Master
    ---------            -------

#查看windows服务器的共享。
# smbclient -L 192.168.18.98 -U hto%passwd
Domain=[IMO-PC] OS=[Windows 7 Ultimate 7600] Server=[Windows 7 Ultimate 6.1]
    Sharename       Type      Comment
    ---------       ----      -------
    ADMIN$          Disk      远程管理
    C$              Disk      默认共享
    D$              Disk      默认共享
    E$              Disk      默认共享
    IPC$            IPC       远程 IPC
    sd              Disk      
session request to 192.168.18.98 failed (Called name not present)
session request to 192 failed (Called name not present)
session request to *SMBSERVER failed (Called name not present)
NetBIOS over TCP disabled -- no workgroup available

第一段列举了该机器上的共享资源,第二段列举了其所在的工作组里面所有提供samba服务的机器,第三列举了其他工作组提供 browse服务的Master机器。

2、像FTP客户端一样使用smbclient
使用网络资源:
smbclient //IP或者NETBIOS名称/共享资源名 [-U 用户名]
命令执行效果如下:
smbclient //192.168.18.98/sd/

接下来的操作和ftp的时候命令一样,用get下载文件而用put上传文件,命令可以用help查看。还有一种方法是用文件系统的方式(这种方式要确定你的内核支持smbfs,或通过安装'smbfs'软件来实现。

smbmount //IP或者NETBIOS名称/共享资源名 /本地挂接点 [-o option]

常用的的option有username=<用户名>,password=<密码>,guest(指定为用guest访问,不用提供密码,前面的即使用username=guest参数的话也会要求输入密码),ro(有时候为了系统安全要指定为只读模式),rw,同时多个 option的话用逗号隔开。对于使用了认证密码的挂载,可将密码写入文件中,如(/etc/fstab):
//192.168.18.98/sd/    /home/sd    smbfs    rw,iocharset=utf8,credentials=/root/pri/smb.passwd    0    0

文件'/root/pri/smb.passwd'格式如下:
username=freeoa
password=passwd

注意要设定好该文件的权限,以防被他人看到内容。

或者可以用mount –t smbfs [–o option] //IP或者NETBIOS名称/共享资源名 /本地挂接点 来实现同样的功能。

例子如下:
smbmount //ip/littlep /test -o guest
或者
mount –t smbfs –o guest //ip/littlep /test,

然后就能通过访问/test来使用网络上的资源了。如果不需要使用的时候,可以简单地使用smbumount /test或者umount /test来解除这个挂接。

3、直接一次性使用smbclient命令
smbclient -c "ls"  //192.168.18.3/tmp  -U username%password

smbclient //192.168.18.3/tmp  -U username%password
smb:\>ls
功能一样的
 
例,创建一个共享文件夹
smbclient -c "mkdir share1"  //192.168.18.3/tmp  -U username%password
如果用户共享//192.168.18.3/tmp的方式是只读的,会提示
NT_STATUS_ACCESS_DENIED making remote directory \share1

# mount -t smbfs -o username="administrator",password="" //192.168.18.3/cp /mnt/ntfs

[Mount a CIFS share via fstab]
通过'fstab'文件来挂载samba共享,在系统启动时自动挂载cifs共享,而不是手动挂载,可在'/etc/fstab'文件记下相关的记录行,大致如下:

//<ip.address>/<share_name>       /mount/point       cifs       credentials=/path/to/.credentials_file,_netdev,iocharset=utf8,uid=<numeric user id>,gid=<numeric group id>,file_mode=0750,dir_mode=0750,rw     0 0

解析如下:

_netdev -- this option tells fstab that we don't want to try mounting this share until the network manager is running, probably a good idea since a cifs share is, naturally, mounted over the network!

iocharset -- the above might differ depending on the language your files are named with, play with this if you use a different character set.

uid and gid -- setting these for the share determines who appears as the owner when you browse to the mount point, if you don't set these options root will appear to own the mount point and all the files in it, which is most likely not what you want! To find your user and group id's you can type "id <username>" in the terminal and it will print out the numeric id for your username and all the groups your id is member of

file_mode and dir_mode -- these determine the permissions of the mount point locally and they work just like any other user permissions on a unix system, the owner permissions will be relative to the uid you set and the group permissions will be relative to the gid you set. This means if you use the above example and have 5 for the group permissions and then, as a user other than uid and who isn't a part of gid, you try to write to the share, you'll get permission denied.

Note: even if you set permissions here to give you write access but the share source doesn't give you write permissions, you still won't be able to write to the share!

credentials -- this is a nice way to avoid posting your username and password directly in your fstab! For example, instead of using a credentials files you could put username=SomeUser,password=SomePassword right into the options and it would work, but that would expose your password to anyone who had viewing rights to the fstab file, which may not matter in the scheme of things for a private server, but it's still poor practice.

The contents of a credentials file is very simple, consider the below
 username=SomeUser
 password=SomePassword

Note: CIFS can be very sensitive to extra white spaces, so make sure each line ends with the end of your username / password and not a white space, also make sure that your file ends on the password line and not a blank line, this can be a good place to check if your mount is failing and you're sure that your login information is correct. Also, do not use quotation marks, this will cause login failures.


Samba使用过程中的问题集

该文章最后由 阿炯 于 2013-02-02 13:34:04 更新,目前是第 2 版。