How to install Samba on Linux

Posted by Dvector on Wed, 17 Nov 2021 01:55:44 +0100

Linux development is usually coded on Windows, and then the files are transmitted to Linux for compilation. When the project is a little large or multiple people develop at the same time, the whole process will become very cumbersome. If there is a tool that can be implemented, the code written will be synchronized to Linux in real time

Samba can solve this problem well. It is a free open source software developed by a student of Australian National University. It allows simple and easy file sharing between windows and Linux systems. It can create network shares for the selected Linux directories (including all subdirectories), so that Windows users can access these Linux directories through the network like accessing folders under normal windows. The installation and configuration process of samba server will be described in detail below

install

Use the following command to install

yum install samba samba-common samba-client

After the installation is completed, execute the smbd -V command to output the version information of the samba server, indicating that the installation is successful

[root@cghost21 ~]# smbd -V
Version 4.10.16

Server configuration

After installing the samba server, you need to adjust some configuration parameters of the server. The configuration file is located in / etc/samba/smb.conf by default. Before modifying, you can back up the original configuration file

[root@cghost21 ~]# cp /etc/samba/smb.conf  /etc/samba/smb.conf.bak

If a line of smb.conf starts with "#" or ";", it means that this line is a comment

  • New Linux user

Add a new user and set a password

[root@cghost21 ~]# useradd smbusr1 -s /sbin/nologin
[root@cghost21 ~]# passwd smbusr1

Note: the user smbusr1 added here is only used to create a samba login user, because when creating a samba user, you need to ensure that a Linux user with the same name exists and has a password set. Otherwise, creating a samba user will fail

Since the user smbusr1 is only used to create a new samba user, - s /sbin/nologin is added during the creation, which means that this user is prohibited from logging in to the Linux system. This is also for security reasons

  • New samba user

Execute the following command to create a new samba user smbusr1 and set the password

[root@cghost21 home]# smbpasswd -a smbusr1
New SMB password:
Retype new SMB password:
Added user smbusr1.
  • Add user shared directory

Create a new shared directory for samba user smbusr1 and grant directory permissions

[root@cghost21 home]# mkdir -p /samba/smbusr1
[root@cghost21 home]# chown -R smbusr1:smbusr1 /samba/smbusr1
  • Configure smb.conf

There are many configurable items for samba server. Only some common configuration items are introduced here. For more configuration items, please refer to smb.conf.org

#Global configuration
[global]
        # samba Working Group
        workgroup = mygroup
        
        # Description string of the server,% V indicates the version number of the server
        server string=smb server %V
        
        # Security mode, user indicates that authentication is required
        security = user
        
        # samba server name, 15 characters maximum
        netbios name = myserver
        
        #Deny illegal user login
        map to guest = bad user
        
        # samba server log
        log file = /var/log/samba/smblog
        
        # Maximum size of server log in KB
        # The server will check the size regularly. If it exceeds, it will rename the file and add the. old extension
        # 0 means there is no limit
        max log size = 10000
        
        
[smbusr1]
        # smbusr1 user's shared directory
        path=/samba/smbusr1
        # Prohibit anonymous access
        public=no
        # Is it writable
        writable=yes
        # Directory writable user groups
        write list=@smbusr1
        # Users accessing the directory
        valid users=smbusr1
        

After modifying the configuration, execute the testarm command to check the syntax error of smb.conf

[root@cghost21 home]# testparm
Load smb config files from /etc/samba/smb.conf
Loaded services file OK.
Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

As can be seen from the above results, smb.conf has no syntax errors

  • Set up firewall

After the samba server starts, it will listen to ports 139 and 445 by default. You can view the ports that the samba server listens to through the following command

[root@cghost21 home]# netstat -an4p | grep smbd | grep LISTEN
tcp     0    0 0.0.0.0:139     0.0.0.0:*      LISTEN      23370/smbd          
tcp     0    0 0.0.0.0:445     0.0.0.0:*      LISTEN      23370/smbd      

If the firewall service is enabled on the machine where the samba server is located, you need to open ports 139 and 445, and then restart the firewall service

[root@localhost ~]# firewall-cmd --zone=public --add-port=139/tcp --permanent
success
[root@localhost ~]# firewall-cmd --zone=public --add-port=445/tcp --permanent
success
[root@localhost ~]# systemctl restart firewalld
  • Start the samba server

After the above steps, finally start the samba server

[root@cghost21 home]# systemctl start smb

Client configuration

samba client configuration is also very simple. It maps a network drive from Linux to Windows. It looks like there is an additional disk in Windows, but this disk corresponds to a directory in the Linux machine in the network. Writing data to this disk is equivalent to writing data to the Linux directory

1. Open my computer, click map network drive, enter the address of shared folder on Linux, and click finish, as shown in the figure below

2. Enter the samba user name and password (the samba user name and password were added in the previous steps) and click OK, as shown in the figure below

3. After the samba server successfully verifies the user name and password, Windows will enter the shared folder. It looks like the drive letter of a hard disk on Windows. See the figure below

So far, Windows has completed mapping the shared directory on the Linux machine. Next, you can write data to the network drive in the way of Windows operation, and the written data will be synchronized to the shared directory / samba/smbusr1 of the Linux machine in real time

common problem

In the process of configuring samba server and client, some problems will occur more or less. Several common errors are listed below

  • Error 1

Check whether the access configuration of the corresponding user is not configured in smb.conf, or the corresponding samba user does not exist

  • Error 2

Windows local has mapped a network drive on the same remote machine. You need to disconnect the previous one before you can map a new one

  • Error 3

There is a problem with the permission setting of the shared directory. You need to modify the permission of the shared directory / samba/smbusr2

Summary

Linux servers usually have no graphical interface, which is not as simple and intuitive as Windows. samba server realizes the operation of converting Linux command line into Windows graphical interface, which greatly facilitates the development of Linux and improves the development efficiency. Combined with version management tool SVN, samba is also very suitable for personal learning and cooperation between small development teams

Topics: Linux Operation & Maintenance server