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 Drivers
	path = /var/lib/samba/drivers
	write list = @printadmin root
	force group = @printadmin
	create mask = 0664
	directory 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 Daemon
   Loaded: loaded (/usr/lib/systemd/system/smb.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2024-12-16 11:34:10 CST; 6s ago
     Docs: 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-group

Dec 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

输入账号密码登录即可

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值