欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > Linux和Windows共享的samba服务

Linux和Windows共享的samba服务

2025/4/19 17:52:01 来源:https://blog.csdn.net/qq_51688785/article/details/144503381  浏览:    关键词:Linux和Windows共享的samba服务

Samba 的用法

  1. Linux 共享文件夹给 Windows 用户 你可以通过配置 Samba 使得 Linux 系统中的某个文件夹能够被 Windows 用户访问和操作。

  2. Windows 用户访问 Linux 上的共享文件夹 Samba 还允许 Windows 用户访问和修改 Linux 系统中共享的文件夹。

一、samba服务器端部署

1、安装samba软件

yum install -y samba

(11:16:12)[nfs root ~] # yum install -y samba
Loaded plugins: fastestmirror
Determining fastest mirrors* base: mirrors.aliyun.com* extras: mirrors.aliyun.com* updates: mirrors.aliyun.com
base                                                                    | 3.6 kB  00:00:00     
epel                                                                    | 4.3 kB  00:00:00     
extras                                                                  | 2.9 kB  00:00:00     
updates                                                                 | 2.9 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package samba.x86_64 0:4.10.16-25.el7_9 will be installed
--> Processing Dependency: samba-libs = 4.10.16-25.el7_9 for package: samba-4.10.16-25.el7_9.x86_64
......
Installed:samba.x86_64 0:4.10.16-25.el7_9                                                              Dependency Installed:avahi-libs.x86_64 0:0.6.31-20.el7             cups-libs.x86_64 1:1.6.3-52.el7_9             gnutls.x86_64 0:3.3.29-9.el7_6                libldb.x86_64 0:1.5.4-2.el7                   libtalloc.x86_64 0:2.1.16-1.el7               libtdb.x86_64 0:1.3.18-1.el7                  libtevent.x86_64 0:0.9.39-1.el7               libwbclient.x86_64 0:4.10.16-25.el7_9         nettle.x86_64 0:2.7.1-9.el7_9                 pyldb.x86_64 0:1.5.4-2.el7                    pytalloc.x86_64 0:2.1.16-1.el7                python-tdb.x86_64 0:1.3.18-1.el7              samba-client-libs.x86_64 0:4.10.16-25.el7_9   samba-common.noarch 0:4.10.16-25.el7_9        samba-common-libs.x86_64 0:4.10.16-25.el7_9   samba-common-tools.x86_64 0:4.10.16-25.el7_9  samba-libs.x86_64 0:4.10.16-25.el7_9          trousers.x86_64 0:0.3.14-2.el7                Complete!

2、创建共享文件夹

(11:31:44)[nfs root ~] # mkdir /my_smb/
(11:32:04)[nfs root ~] # 

3、修改samba配置文件(设置一个共享文件夹)

(11:20:02)[nfs root ~] # ls /etc/samba/
lmhosts  smb.conf  smb.conf.example在配置文件最后添加如下配置
(11:20:45)[nfs root ~] # vim /etc/samba/smb.conf
(11:20:14)[nfs root ~] # tail -7 /etc/samba/smb.conf
[print$]comment = Printer Driverspath = /var/lib/samba/driverswrite list = @printadmin rootforce group = @printadmincreate mask = 0664directory mask = 0775

4、创建samba用户,并设置用户密码

注:samba也有用户认证机制,需要通过pdbedit命令设置samba的用户信息

pdbedit命令是给linux以及存在的用户,设置一个密码
(11:32:13)[nfs root ~] # useradd  samba01
(11:32:15)[nfs root ~] # 使用pdbedit命令,给samba的用户设置密码
-a 添加smb用户
-u 指定用户名
(11:32:29)[nfs root ~] # pdbedit -a -u samba01
new password:
retype new password:
Unix username:        samba01
NT username:          
Account Flags:        [U          ]
User SID:             S-1-5-21-3180717152-3660557076-3311746906-1000
Primary Group SID:    S-1-5-21-3180717152-3660557076-3311746906-513
Full Name:            
Home Directory:       \\nfs\samba01
HomeDir Drive:        
Logon Script:         
Profile Path:         \\nfs\samba01\profile
Domain:               NFS
Account desc:         
Workstations:         
Munged dial:          
Logon time:           0
Logoff time:          Wed, 06 Feb 2036 23:06:39 CST
Kickoff time:         Wed, 06 Feb 2036 23:06:39 CST
Password last set:    Mon, 16 Dec 2024 11:32:33 CST
Password can change:  Mon, 16 Dec 2024 11:32:33 CST
Password must change: never
Last bad password   : 0
Bad password count  : 0
Logon hours         : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
(11:32:33)[nfs root ~] # 

5、修改smb共享文件夹权限

(11:32:51)[nfs root ~] # chown -R samba01:samba01   /my_smb/

6、共享目录下创建测试数据

(11:33:05)[nfs root ~] # cd /my_smb/
(11:33:10)[nfs root /my_smb] # touch test.txt
(11:33:22)[nfs root /my_smb] # echo "this is test" > test.txt 

7、启动smb服务

(11:34:09)[nfs root ~] # systemctl start smb
(11:34:10)[nfs root ~] # systemctl status smb
● smb.service - Samba SMB DaemonLoaded: loaded (/usr/lib/systemd/system/smb.service; disabled; vendor preset: disabled)Active: active (running) since Mon 2024-12-16 11:34:10 CST; 6s agoDocs: man:smbd(8)man:samba(7)man:smb.conf(5)Main PID: 1629 (smbd)Status: "smbd: ready to serve connections..."CGroup: /system.slice/smb.service├─1629 /usr/sbin/smbd --foreground --no-process-group├─1631 /usr/sbin/smbd --foreground --no-process-group├─1632 /usr/sbin/smbd --foreground --no-process-group└─1634 /usr/sbin/smbd --foreground --no-process-groupDec 16 11:34:10 nfs systemd[1]: Starting Samba SMB Daemon...
Dec 16 11:34:10 nfs smbd[1629]: [2024/12/16 11:34:10.947574,  0] 

8、验证进程、端口

(11:34:28)[nfs root ~] # netstat -tunlp|grep smb
tcp        0      0 0.0.0.0:445             0.0.0.0:*               LISTEN      1629/smbd           
tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN      1629/smbd           
tcp6       0      0 :::445                  :::*                    LISTEN      1629/smbd           
tcp6       0      0 :::139                  :::*                    LISTEN      1629/smbd           
(11:34:29)[nfs root ~] # 
(11:34:44)[nfs root ~] # ps -ef|grep smb
root       1629      1  0 11:34 ?        00:00:00 /usr/sbin/smbd --foreground --no-process-group
root       1631   1629  0 11:34 ?        00:00:00 /usr/sbin/smbd --foreground --no-process-group
root       1632   1629  0 11:34 ?        00:00:00 /usr/sbin/smbd --foreground --no-process-group
root       1634   1629  0 11:34 ?        00:00:00 /usr/sbin/smbd --foreground --no-process-group
root       1644   1213  0 11:34 pts/0    00:00:00 grep --color=auto smb

二、samba客户端认证

1、Linux端安装

(14:08:08)[master root ~] # yum -y install samba-client
Loaded plugins: fastestmirror
Determining fastest mirrors* base: mirrors.aliyun.com* extras: mirrors.aliyun.com* updates: mirrors.aliyun.com
base                                                                        | 3.6 kB  00:00:00     
epel                                                                        | 4.3 kB  00:00:00     
extras                                                                      | 2.9 kB  00:00:00     
updates                                                                     | 2.9 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package samba-client.x86_64 0:4.10.16-25.el7_9 will be installed
--> Processing Dependency: samba-common-libs = 4.10.16-25.el7_9 for package: samba-client-4.10.16-25.el7_9.x86_64
......
Installed:samba-client.x86_64 0:4.10.16-25.el7_9                                                           Dependency Installed:avahi-libs.x86_64 0:0.6.31-20.el7             cups-libs.x86_64 1:1.6.3-52.el7_9                 gnutls.x86_64 0:3.3.29-9.el7_6                libarchive.x86_64 0:3.1.2-14.el7_7                libldb.x86_64 0:1.5.4-2.el7                   libsmbclient.x86_64 0:4.10.16-25.el7_9            libtalloc.x86_64 0:2.1.16-1.el7               libtdb.x86_64 0:1.3.18-1.el7                      libtevent.x86_64 0:0.9.39-1.el7               libwbclient.x86_64 0:4.10.16-25.el7_9             nettle.x86_64 0:2.7.1-9.el7_9                 samba-client-libs.x86_64 0:4.10.16-25.el7_9       samba-common.noarch 0:4.10.16-25.el7_9        samba-common-libs.x86_64 0:4.10.16-25.el7_9       trousers.x86_64 0:0.3.14-2.el7               Complete!

2、命令连接samba

smbclient //10.0.0.31/smb_share   -U samba01

# 输入samba01的密码即可
#进入后,输入 ? 查看samba提供的命令,也就是作用

(14:09:23)[master root ~] # smbclient //10.0.0.31/smb_share   -U samba01
Enter SAMBA\samba01's password: 
Try "help" to get a list of possible commands.
smb: \> 
smb: \> ?
?              allinfo        altname        archive        backup         
blocksize      cancel         case_sensitive cd             chmod          
chown          close          del            deltree        dir            
du             echo           exit           get            getfacl        
geteas         hardlink       help           history        iosize         
lcd            link           lock           lowercase      ls             
l              mask           md             mget           mkdir          
more           mput           newer          notify         open           
posix          posix_encrypt  posix_open     posix_mkdir    posix_rmdir    
posix_unlink   posix_whoami   print          prompt         put            
pwd            q              queue          quit           readlink       
rd             recurse        reget          rename         reput          
rm             rmdir          showacls       setea          setmode        
scopy          stat           symlink        tar            tarmode        
timeout        translate      unlock         volume         vuid           
wdel           logon          listconnect    showconnect    tcon           
tdis           tid            utimes         logoff         ..             
!              
smb: \> exit
(14:10:41)[master root ~] # 

上传文件和下载文件

客户端下载文件到本地
(14:22:10)[master root ~] # smbclient //10.0.0.31/smb_share   -U samba01
Enter SAMBA\samba01's password: 
Try "help" to get a list of possible commands.
smb: \> get test.txt 
getting file \test.txt of size 13 as test.txt (0.7 KiloBytes/sec) (average 0.7 KiloBytes/sec)
smb: \> 
smb: \> exit
(14:22:54)[master root ~] # ls
anaconda-ks.cfg  network.sh  test.txt  电影.mv
(14:22:55)[master root ~] # ll
total 16
-rw-------. 1 root root 1349 Dec  2 16:13 anaconda-ks.cfg
-rw-r--r--  1 root root  719 Dec  3 14:19 network.sh
-rw-r--r--  1 root root   13 Dec 16 14:22 test.txt
-rw-r--r--  1 root root    9 Dec  6 15:32 电影.mv
(14:22:57)[master root ~] # cat test.txt 
this is test客户端上传文件到服务端
(14:23:03)[master root ~] # smbclient //10.0.0.31/smb_share   -U samba01
Enter SAMBA\samba01's password: 
Try "help" to get a list of possible commands.
smb: \> put 电影.mv
putting file 电影.mv as \电影.mv (0.2 kb/s) (average 0.2 kb/s)
smb: \> 
smb: \> exit
(14:25:27)[master root ~] # (14:23:55)[nfs root /my_smb] # ls
test.txt
(14:23:56)[nfs root /my_smb] # ls
test.txt  电影.mv
(14:25:30)[nfs root /my_smb] # 

3、Windows端连接samba

使用 windows的win快捷键+ r,打开运行窗口

输入\\10.0.0.31\smb_share

输入账号密码登录即可

可以看到已经访问到服务端共享目录了

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词