Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Samba under Linux

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use Samba under Linux. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Samba is a free software that implements SMB protocol on Linux and UNIX systems, which is composed of server and client programs. SMB (Server Messages Block, Information Service Block) is a communication protocol for sharing files and printers on the LAN. It provides sharing services for files, printers and other resources between different computers in the LAN.

Step 1: install Samba on Fedora and CentOS

First, install Samba and do some basic configuration.

Verify that Samba is installed on your system:

$rpm-Q samba samba-common samba-client

If the above command has no output, this means that Samba is not installed. At this point, use the following command to install Samba.

$sudo yum install samba samba-common samba-client

Next, create a local folder for sharing on the network. This folder should be exported to remote users as Samba shares. In this guide, we will create this folder in the top-level folder'/', so make sure you have the appropriate permissions.

$sudo mkdir / shared

If you want to create a shared folder within your home folder (for example, ~ / shared), you must activate the home folder sharing option for Samba in SELinux, which will be mentioned later.

After creating the / shared folder, set the folder permissions to ensure that the rest of the users can access it.

$sudo chmod o+rw / shared

If you do not want other users to have write access to the folder, you need to remove the'w' option from the command.

$sudo chmod otakr / shared

Next, create an empty file to test. This file can be used to verify that the Samba share has been mounted.

$sudo touch / shared/file1 step 2: configure SELinux for Samba

Next, we need to configure SELinux again. SELinux is enabled by default in Fedora and CentOS distributions. SELinux allows Samba to read and modify files or folders only in the correct security configuration. (for example, add the 'sambasharet' attribute tag).

The following command adds the necessary tags to the configuration of the file:

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

Will be replaced with the local folder we previously created for the Samba share (for example, / shared):

$sudo semanage fcontext-a-t samba_share_t "/ shared (/. *)?"

We must execute the restorecon command to activate the modified tag, as follows:

$sudo restorecon-R-v / shared

In order to share folders within our home folder through Samba, we must turn on the option of sharing home folders in SELinux, which is turned off by default. The following command can achieve this effect. If you are not sharing your home folder, you can skip this step.

$sudo setsebool-P samba_enable_home_dirs 1 step 3: configure a firewall for Samba

The following command is used to open the TCP/UDP port required for Samba sharing in the firewall.

If you are using firewalld (for example, under Fedora and CentOS7), the following commands will permanently modify the firewall rules related to Samba.

$sudo firewall-cmd-permanent-add-service=samba

If you use iptables in your firewall (for example, CentOS6 or earlier), you can use the following command to open the necessary outbound ports for Samba.

$sudo vi / etc/sysconfig/iptables-A RH-Firewall-1-INPUT-m state-- state NEW-m tcp-p tcp-- dport 445-j ACCEPT-A RH-Firewall-1-INPUT-m state-- state NEW-m udp-p udp-- dport 445-j ACCEPT-A RH-Firewall-1-INPUT-m state-- state NEW-m udp-p udp-dport 137-j ACCEPT-A RH-Firewall-1-INPUT-m state-state NEW-m udp-p udp-- Dport 138j ACCEPT-A RH-Firewall-1-INPUT-m state-- state NEW-m tcp-p tcp-- dport 139j ACCEPT

Then restart the iptables service:

$sudo service iptables restart step 4: change the Samba configuration

The following steps are used to configure Samba to export the local folder as a Samba shared folder.

Open the Samba configuration file using the file editor and add the following line to the end of the file.

$sudo nano / etc/samba/ smb.confs [myshare] comment=my shared filespath=/sharedpublic=yeswriteable=yes

The text above in parentheses (for example, "myshare") is the name of the resource shared by Samba, which is used to access the Samba share from a remote host.

Create a Samba user account, which is necessary to mount and export the Samba file system. We can use the smbpasswd tool to create a Samba user. Note that the Samba user account must be an existing Linux user. If you try to add a user that does not exist using smbpasswd, it returns an error message.

If you do not want to use any existing Linux users as Samba users, you can create a new user in your system. For security reasons, set the login script for the new user to / sbin/nologin, and do not create the home folder for that user.

In this example, we created a user named "sambaguest" as follows:

$sudo useradd-M-s / sbin/nologin sambaguest$ sudo passwd sambaguest

After creating a new user, use the smbpasswd command to add the Samba user. When this command asks for a password, you can type a password that is different from its user's password.

$sudo smbpasswd-a sambaguest

Activate the Samba service and detect whether the Samba service is running.

$sudo systemctl enable smb.service$ sudo systemctl start smb.service$ sudo systemctl is-active smb

Use the following command to view a list of shared folders in Samba.

$smbclient-U sambaguest-L localhost

Next is a screenshot of accessing the Samba shared folder and copying the file1 in the Thunar File Manager. Note that the shared content of Samba can be accessed through the address smb:///myshare in Thunar.

This is the end of this article on "how to use Samba under Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report