fedora23搭建samba-server

本文详细介绍了如何在Linux系统中配置Samba服务器,并通过Samba共享自定义目录。首先,通过安装Samba和创建Samba账户来设置基础环境。接着,创建一个新目录并使用Samba进行文件共享。此外,文章还演示了如何为Samba账户添加文件系统标签,确保SELinux安全策略的正确执行。最后,通过创建Samba账户和启动Samba服务,实现了目录的正常共享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1。首先是安装samba
2 添加samba的账户,这里添加给samba的账户需要在系统里现有这个人。
否则他会提示:
root@localhost ~]# smbpasswd -a test
New SMB password:
Retype new SMB password:
Failed to add entry for user test.

所以现在系统中添加这个账户
[root@localhost ~]# groupadd test -g 6000
[root@localhost ~]# useradd test -u 6000 -g 6000 -s /sbin/nologin -d /dev/null

当然,如果用系统现有的账户那就直接添加就行了
[root@localhost ~]# smbpasswd -a test
New SMB password:
Retype new SMB password:
Added user test.

其实这里有参考:https://docs.fedoraproject.org/en-US/Fedora/19/html/Security_Guide/sect-Managing_Confined_Services-Samba-Configuration_examples.html

10.4.4. Configuration examples
The following examples provide real-world demonstrations of how SELinux complements the Samba server and how full function of the Samba server can be maintained.
10.4.4.1. Sharing directories you create
The following example creates a new directory, and shares that directory through Samba:

Run rpm -q samba samba-common samba-client to confirm the samba, samba-common, and samba-client packages are installed. If any of these packages are not installed, install them by running yum install package-name as the root user.
Run mkdir /myshare as the root user to create a new top-level directory to share files through Samba.
Run touch /myshare/file1 as the root user to create an empty file. This file is used later to verify the Samba share mounted correctly.
SELinux allows Samba to read and write to files labeled with the samba_share_t type, as long as /etc/samba/smb.conf and Linux permissions are set accordingly. Run the following command as the root user to add the label change to file-context configuration:

semanage fcontext -a -t samba_share_t "/myshare(/.*)?"

Run restorecon -R -v /myshare as the root user to apply the label changes:

# restorecon -R -v /myshare
restorecon reset /myshare context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /myshare/file1 context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0

Edit /etc/samba/smb.conf as the root user. Add the following to the bottom of this file to share the /myshare/ directory through Samba:

[myshare]
comment = My share
path = /myshare
public = yes
writeable = no

A Samba account is required to mount a Samba file system. Run smbpasswd -a username as the root user to create a Samba account, where username is an existing Linux user. For example, smbpasswd -a testuser creates a Samba account for the Linux testuser user:

# smbpasswd -a testuser
New SMB password: Enter a password
Retype new SMB password: Enter the same password again
Added user testuser.

Running smbpasswd -a username, where username is the username of a Linux account that does not exist on the system, causes a Cannot locate Unix account for 'username'! error.
Run service smb start as the root user to start the Samba service:

service smb start
Starting SMB services:                                     [  OK  ]

Run smbclient -U username -L localhost to list the available shares, where username is the Samba account added in step 7. When prompted for a password, enter the password assigned to the Samba account in step 7 (version numbers may differ):

$ smbclient -U username -L localhost
Enter username's password:
Domain=[HOSTNAME] OS=[Unix] Server=[Samba 3.5.2-59.fc13]

Sharename       Type      Comment
---------       ----      -------
myshare         Disk      My share
IPC$            IPC       IPC Service (Samba Server Version 3.5.2-59.fc13)
username        Disk      Home Directories
Domain=[HOSTNAME] OS=[Unix] Server=[Samba 3.5.2-59.fc13]

Server               Comment
---------            -------

Workgroup            Master
---------            -------

Run mkdir /test/ as the root user to create a new directory. This directory will be used to mount the myshare Samba share.
Run the following command as the root user to mount the myshare Samba share to /test/, replacing username with the username from step 7:

mount //localhost/myshare /test/ -o user=username

Enter the password for username, which was configured in step 7.
Run ls /test/ to view the file1 file created in step 3:

$ ls /test/
file1

sudo service smb start
sudo setenforce 0

### 如何在云服务器上安装和配置Samba服务器 #### 安装前准备 为了确保顺利安装Samba,在开始之前应确认云服务器的操作系统已更新至最新版本,并且具备网络连接能力。对于基于Red Hat的企业版Linux或其他衍生发行版,可以使用`yum update`命令来完成系统的全面更新;而对于Ubuntu等Debian系操作系统,则应该执行如下操作: ```bash sudo apt-get update && sudo apt-get upgrade -y ``` #### 检查现有Samba安装状态 在正式安装Samba之前,建议先检查当前系统是否已经存在旧版本的Samba安装。这一步骤有助于避免重复安装可能引发的问题。 ```bash dpkg-query -l | grep samba # 对于Debian/Ubuntu系统 rpm -qa | grep samba # 对于RHEL/CentOS/Fedora系统 ``` 如果发现已有Samba安装,可根据实际情况决定卸载重装或是保留原有设置并在此基础上继续配置[^3]。 #### Samba软件包安装 一旦完成了上述准备工作,就可以着手安装最新的Samba套件了。针对不同类型的Linux发行版,具体的安装指令有所区别: - **Debian/Ubuntu** ```bash sudo apt-get install samba smbclient cifs-utils -y ``` - **RHEL/CentOS/Fedora** ```bash sudo yum install samba samba-client samba-common -y ``` 成功完成后,Samba服务将会自动启动并加入开机自启列表中[^2]。 #### 配置防火墙规则 考虑到安全性因素,默认情况下大多数云服务商都会启用防火墙防护机制。因此还需要适当调整防火墙策略以开放必要的端口供Samba正常使用。通常来说,至少要允许TCP协议下的139号端口以及UDP协议下的一系列辅助端口(如137, 138)。具体做法取决于所使用的防火墙管理工具,比如iptables或firewalld: - 使用`ufw`(适用于Ubuntu) ```bash sudo ufw allow 'SMB' ``` - 使用`firewalld`(适用于CentOS/RHEL) ```bash sudo firewall-cmd --add-service=samba --permanent sudo firewall-cmd --reload ``` 以上命令能够有效保障外部设备访问内部共享资源时不会受到不必要的阻碍[^1]。 #### 编辑主配置文件 接下来就是编辑位于/etc/samba/smb.conf路径下的核心配置文档了。这里可以根据实际需求定义多个共享目录及其对应的权限级别。下面给出一段简单的例子作为参考: ```ini [global] workgroup = WORKGROUP server string = %h server (Samba, Ubuntu) netbios name = ubuntu security = user map to guest = bad user dns proxy = no [homes] comment = Home Directories browseable = yes read only = no create mask = 0700 directory mask = 0700 valid users = %S [shared_folder] path = /srv/samba/shared/ available = yes browsable = yes public = yes writable = yes force create mode = 0664 force directory mode = 2775 guest ok = yes ``` 这段配置创建了一个名为`homes`的家庭用户默认共享区,同时还额外设定了一个公共可读写的`shared_folder`用于团队协作场景中的资料交换。 #### 创建共享文件夹及设定权限 按照上面提到的例子,现在需要手动建立相应的物理存储位置,并赋予恰当的所有者与组成员身份以便后续正常运作: ```bash sudo mkdir -p /srv/samba/shared/ sudo chown nobody:nogroup /srv/samba/shared/ sudo chmod 2775 /srv/samba/shared/ ``` 此外,还可以利用`smbpasswd`工具为特定账户分配密码从而增强安全保护措施: ```bash sudo smbpasswd -a username ``` 最后重启Samba服务使更改生效即可: ```bash sudo systemctl restart smbd.service nmbd.service ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值