如何在 Linux 和 Windows 电脑之间共享文件
共享 Samba

> 
> 使用 Samba 设置跨平台文件共享。
> 
> 
> 


![](/data/attachment/album/202205/02/233859oqqjvfr6tqz9bfqp.jpg "Blue folders flying in the clouds above a city skyline")


如果你使用不同的操作系统，能够在它们之间共享文件会让你倍感方便。这篇文章介绍如何使用 [Samba](https://www.samba.org/) 和 [mount.cifs](https://linux.die.net/man/8/mount.cifs) 在 Linux （[Fedora 33](https://getfedora.org/en/workstation/download/)）和 Windows 10 之间设置文件共享。


Samba 是 [SMB/CIFS](https://en.wikipedia.org/wiki/Server_Message_Block) 协议的 Linux 实现，允许通过网络连接直接访问共享的文件夹和打印机。 mount.cifs 是 Samba 套件的一部分，可让你在 Linux 下挂载 [CIFS](https://en.wikipedia.org/wiki/Server_Message_Block) 文件系统。



> 
> **注意**: 这些说明适用于在你的私有本地网络内，或在 Linux 宿主机和虚拟化的 Windows 访客机之间的虚拟主机专用网络中共享文件。不要将本文视为你公司网络的操作指南，因为本文没有实现必要的网络安全考虑。
> 
> 
> 


### 从 Windows 访问 Linux


本节介绍从 Windows 文件资源管理器访问用户的 Linux 主目录。


#### 1、安装和配置 Samba


进入你的系统安装 Samba:



```
dnf install samba

```

Samba 是一个系统守护进程，其配置文件位于 `/etc/samba/smb.conf`。它的默认配置应该就可以工作。如果不行，下面这个最小化配置应该可以解决问题：



```
[global]
        workgroup = SAMBA
        server string = %h server (Samba %v)
        invalid users = root
        security = user
[homes]
        comment = Home Directories
        browseable = no
        valid users = %S
        writable = yes

```

你可以在该项目网站的 [smb.conf](https://www.samba.org/samba/docs/current/man-html/smb.conf.5.html) 部分找到参数的详细说明。


#### 2、修改 LinuxSE


如果你的 Linux 发行版受 [SELinux](https://www.redhat.com/en/topics/linux/what-is-selinux) 保护（比如 Fedora），必须通过以下命令才能通过 Samba 共享主目录：



```
setsebool -P samba_enable_home_dirs on

```

通过以下命令查看这个值：



```
getsebool samba_enable_home_dirs

```

输出如下：


![Sebool](/data/attachment/album/202205/02/233906mxw5xkxgupp56xk8.png "Enabling Samba to enable user directory access")


#### 3、启用你的用户


Samba 使用一组用户/密码来管理连接权限。通过键入以下命令将你的 Linux 用户添加到该集合中：



```
smbpasswd -a <你的用户名>

```

系统提示你输入密码。这是一个 *全新* 的密码；而不是你账户的当前密码。请输入你想用来登录 Samba 的密码。


键入以下命令得到有 Samba 使用权限的用户列表：



```
pdbedit -L -v

```

键入以下命令删除一个用户：



```
smbpasswd -x <用户名>

```

#### 4、开启 Samba


既然 Samba 是一个系统守护进程，你可以在 Fedora 上键入以下命令启动它：



```
systemctl start smb

```

这将为当前会话开启 Samba 服务。如果想让它自启动，键入以下命令：



```
systemctl enable smb

```

在某些系统上，Samba 守护进程注册为 `smbd`。


#### 4、配置防火墙


你的防火墙会默认阻拦 Samba。通过配置防火墙允许 Samba 能永久访问网络。


你可以在命令行执行如下操作：



```
firewall-cmd --add-service=samba --permanent

```

或者，你可以使用 `firewall-config` 工具以图形化方式进行操作：


![firewall-config](/data/attachment/album/202205/02/233907adbagpspbvrkv2az.png "firewall-config tool")


#### 5、从 Windows 访问 Samba


在 Windows 中，打开文件资源管理器。在地址栏中，键入两个反斜杠（`\\`），紧跟你的 Linux 机器的地址（IP 地址或主机名）：


![从 Windows 访问 Linux](/data/attachment/album/202205/02/233907x04jd7i8v8tcuxkn.png "Accessing Linux machine from Windows")


系统将提示你输入登录信息。输入第 3 步中的用户名和密码组合。你现在应该可以访问 Linux 机器上的主目录：


![从 Windows 访问 Linux](/data/attachment/album/202205/02/233908sonkofsvzsqzvkeh.png "Accessing Linux machine from Windows")


### 从 Linux 访问 Windows


以下步骤说明了如何从 Linux 访问共享的 Windows 文件夹。要实现这一点，需要你的 Windows 用户帐户具有管理员权限。


#### 1、启用文件共享


通过点击 “Windows 按钮” > “<ruby> 设置 <rt>  Settings </rt></ruby>” > “<ruby> 网络和 Internet <rt>  Network &amp; Internet </rt></ruby>” ，或者右键单击任务栏右下角的小监视器图标，<ruby> 打开网络和共享中心 <rt>  Open Network an d Sharing Center </rt></ruby>：


![打开网络和共享中心](/data/attachment/album/202205/02/233908jd5syp3s24od8jsp.png "Open network and sharing center")


在打开的窗口中，找到你要使用的连接并记下其配置文件。我使用了 **以太网 3**，它被标记为 <ruby> 公用网络 <rt>  Public Network </rt></ruby>。



> 
> **注意**：如果你的 PC 经常连接公用网络，请考虑将本地计算机的连接配置文件更改为 **私有**。
> 
> 
> 


记住你的网络配置，然后单击 <ruby> 更改高级共享设置 <rt>  Change advanced sharing settings </rt></ruby>：


![更改高级共享设置](/data/attachment/album/202205/02/233909kece9tjceo9ctcs8.png "Change advanced sharing settings")


选择与你的连接对应的配置文件并打开 <ruby> 网络发现 <rt>  network discovery </rt></ruby> 和 <ruby> 文件和打印机共享 <rt>  file and printer sharing </rt></ruby>：


![网络共享设置](/data/attachment/album/202205/02/233909eg8q000o5yn6io58.png "Network sharing settings")


#### 2、定义一个共享文件夹


通过右键单击你要共享的文件夹打开上下文菜单，导航到 <ruby> 授予访问权限 <rt>  Give access to </rt></ruby>，然后选择 <ruby> 特定用户... <rt>  Specific people... </rt></ruby>：


![授予访问权限](/data/attachment/album/202205/02/233909nx9dldkjtktl5xzk.png "Give access")


检查你当前的用户名是否在列表中。点击 <ruby> 共享 <rt>  Share </rt></ruby> 将此文件夹标记为共享：


![标记为共享](/data/attachment/album/202205/02/233910wawhjxv727nvxvnr.png "Tag as shared")


你可以通过在文件资源管理器的地址栏中输入 `\\localhost` 来显示所有共享文件夹的列表：


![共享文件夹](/data/attachment/album/202205/02/233910e9lzmi8pt9st9iyi.png "Shared folders")


![共享文件夹](/data/attachment/album/202205/02/233911isxutx5npu3nwn25.png "Shared folders")


#### 3、在 Linux 下挂载共享文件夹


回到你的 Linux 系统，打开一个命令行，然后创建一个新文件夹，用于挂载 Windows 共享：



```
mkdir ~/WindowsShare

```

挂载 Windows 共享是使用 `mount.cifs` 完成的，它应该被默认安装。使用如下命令临时挂载你的共享文件夹：



```
sudo mount.cifs //<address-of-windows-pc>/MySharedFolder ~/WindowsShare/ -o user=<Windows-user>,uid=$UID

```

在这个命令里：


* `<address-of-windows-pc>` 是 Windows PC 的地址信息（IP 或主机名）
* `<Windows-user>` 是允许访问共享文件夹的用户（见步骤 2）


系统将提示你输入 Windows 密码。之后，你将能够使用普通 Linux 用户访问 Windows 上的共享文件夹。


要卸载共享文件夹：



```
sudo umount ~/WindowsShare/

```

你还可以在系统启动时挂载 Windows 共享文件夹。按照 [这些步骤](https://timlehr.com/auto-mount-samba-cifs-shares-via-fstab-on-linux/) 相应地配置你的系统。


### 总结


在这里展示了如何建立临时的文件夹共享访问权限，每次重启后都要重新设置，因此修改成永久访问会更便利。我经常在不同的系统之间来回切换，对我而言设置直接文件访问非常实用。




---


via: <https://opensource.com/article/21/4/share-files-linux-windows>


作者：[Stephan Avenwedde](https://opensource.com/users/hansic99) 选题：[lujun9972](https://github.com/lujun9972) 译者：[hanszhao80](https://github.com/hanszhao80) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
If you work with different operating systems, it's handy to be able to share files between them. This article explains how to set up file access between Linux ([Fedora 33](https://getfedora.org/en/workstation/download/)) and Windows 10 using [Samba](https://www.samba.org/) and [mount.cifs](https://linux.die.net/man/8/mount.cifs).

Samba is the Linux implementation of the [SMB/CIFS](https://en.wikipedia.org/wiki/Server_Message_Block) protocol, allowing direct access to shared folders and printers over a network. Mount.cifs is part of the Samba suite and allows you to mount the [CIFS](https://en.wikipedia.org/wiki/Server_Message_Block) filesystem under Linux.


Caution: These instructions are for sharing files within your private local network or in a virtualized host-only network between a Linux host machine and a virtualized Windows guest. Don't consider this article a guideline for your corporate network, as it doesn't implement the necessary cybersecurity considerations.

## Access Linux from Windows

This section explains how to access a user's Linux home directory from Windows File Explorer.

### 1. Install and configure Samba

Start on your Linux system by installing Samba:

`dnf install samba`


Samba is a system daemon, and its configuration file is located in `/etc/samba/smb.conf`

. Its default configuration should work. If not, this minimal configuration should do the job:

```
[global]
workgroup = SAMBA
server string = %h server (Samba %v)
invalid users = root
security = user
[homes]
comment = Home Directories
browseable = no
valid users = %S
writable = yes
```


You can find a detailed description of the parameters in the [smb.conf](https://www.samba.org/samba/docs/current/man-html/smb.conf.5.html) section of the project's website.

### 2. Modify LinuxSE

If your Linux distribution is protected by [SELinux](https://www.redhat.com/en/topics/linux/what-is-selinux) (as Fedora is), you have to enable Samba to be able to access the user's home directory:

`setsebool -P samba_enable_home_dirs on`


Check that the value is set by typing:

`getsebool samba_enable_home_dirs`


Your output should look like this:

![Enabling Samba to enable user directory access Sebool](https://opensource.com/sites/default/files/uploads/sebool.png)

(Stephan Avenwedde, CC BY-SA 4.0)

### 3. Enable your user

Samba uses a set of users and passwords that have permission to connect. Add your Linux user to the set by typing:

`smbpasswd -a <your-user>`


You will be prompted for a password. This is a *completely new* password; it is not the current password for your account. Enter the password you want to use to log in to Samba.

To get a list of allowed user types:

`pdbedit -L -v`


Remove a user by typing:

`smbpasswd -x <user-name>`


### 4. Start Samba

Because Samba is a system daemon, you can start it on Fedora with:

`systemctl start smb`


This starts Samba for the current session. If you want Samba to start automatically on system startup, enter:

`systemctl enable smb`


On some systems, the Samba daemon is registered as `smbd`

.

### 4. Configure the firewall

By default, Samba is blocked by your firewall. Allow Samba to access the network permanently by configuring the firewall.

You can do it on the command line with:

`firewall-cmd --add-service=samba --permanent`


Or you do it graphically with the firewall-config tool:

![firewall-config tool firewall-config](https://opensource.com/sites/default/files/uploads/firewall_configuration.png)

(Stephan Avenwedde, CC BY-SA 4.0)

### 5. Access Samba from Windows

In Windows, open File Explorer. On the address line, type in two backslashes followed by your Linux machine's address (IP address or hostname):

![Accessing Linux machine from Windows Accessing Linux machine from Windows](https://opensource.com/sites/default/files/uploads/windows_access_shared_1.png)

(Stephan Avenwedde, CC BY-SA 4.0)

You will be prompted for your login information. Type in the username and password combination from step 3. You should now be able to access your home directory on your Linux machine:

![Accessing Linux machine from Windows Accessing Linux machine from Windows](https://opensource.com/sites/default/files/uploads/windows_acess_shared_2.png)

(Stephan Avenwedde, CC BY-SA 4.0)

## Access Windows from Linux

The following steps explain how to access a shared Windows folder from Linux. To implement them, you need Administrator rights on your Windows user account.

### 1. Enable file sharing

Open the** Network and Sharing Center** either by clicking on the

**Windows Button > Settings > Network & Internet**

or by right-clicking the little monitor icon on the bottom-right of your taskbar:

![Open network and sharing center Open network and sharing center](https://opensource.com/sites/default/files/uploads/open_network_and_sharing_center.png)

(Stephan Avenwedde, CC BY-SA 4.0)

In the window that opens, find the connection you want to use and note its profile. I used **Ethernet 3**, which is tagged as a **Public network**.


Caution: Consider changing your local machine's connection profile toPrivateif your PC is frequently connected to public networks.

Remember your network profile and click on **Change advanced sharing settings**:

![Change advanced sharing settings Change advanced sharing settings](https://opensource.com/sites/default/files/uploads/network_and_sharing_center_2.png)

(Stephan Avenwedde, CC BY-SA 4.0)

Select the profile that corresponds to your connection and turn on **network discovery** and **file and printer sharing**:

![Network sharing settings Network sharing settings](https://opensource.com/sites/default/files/uploads/network_sharing.png)

(Stephan Avenwedde, CC BY-SA 4.0)

### 2. Define a shared folder

Open the context menu by right-clicking on the folder you want to share, navigate to **Give access to**, and select **Specific people...** :

![Give access Give access](https://opensource.com/sites/default/files/pictures/give_access_to.png)

(Stephan Avenwedde, CC BY-SA 4.0)

Check whether your current username is on the list. Click on **Share** to tag this folder as shared:

![Tag as shared Tag as shared](https://opensource.com/sites/default/files/pictures/tag_as_shared.png)

(Stephan Avenwedde, CC BY-SA 4.0)

You can display a list of all shared folders by entering `\\localhost`

in File Explorer's address line:

![Shared folders Shared folders](https://opensource.com/sites/default/files/uploads/show_shared_folder_1.png)

(Stephan Avenwedde, CC BY-SA 4.0)

![Shared folders Shared folders](https://opensource.com/sites/default/files/uploads/show_shared_folder_2.png)


<p class="rtecenter"><sup>(Stephan Avenwedde, <a href="https://opensource.com/%3Ca%20href%3D"https://creativecommons.org/licenses/by-sa/4.0/" rel="ugc">https://creativecommons.org/licenses/by-sa/4.0/" target="_blank">CC BY-SA 4.0</a>)</sup></p>

### 3. Mount the shared folder under Linux

Go back to your Linux system, open a command shell, and create a new folder where you want to mount the Windows share:

`mkdir ~/WindowsShare`


Mounting Windows shares is done with mount.cifs, which should be installed by default. To mount your shared folder temporarily, use:

`sudo mount.cifs //<address-of-windows-pc>/MySharedFolder ~/WindowsShare/ -o user=<Windows-user>,uid=$UID`


In this command:

`<address-of-windows-pc>`

is the Windows PC's address info (IP or hostname)`<Windows-user>`

is the user that is allowed to access the shared folder (from step 2)

You will be prompted for your Windows password. Enter it, and you will be able to access the shared folder on Windows with your normal Linux user.

To unmount the shared folder:

`sudo umount ~/WindowsShare/`



You can also mount a Windows shared folder on system startup. Follow [these steps](https://timlehr.com/auto-mount-samba-cifs-shares-via-fstab-on-linux/) to configure your system accordingly.

## Summary

This shows how to establish temporary shared folder access that must be renewed after each boot. It is relatively easy to modify this configuration for permanent access. I often switch back and forth between different systems, so I consider it incredibly practical to set up direct file access.

## 2 Comments