Creating shared folders for Linux (centos7) using Samba

Time:2021-12-4

Add Samba service for test server

Samba service is a service for Linux file sharing, which can be understood as “shared folder of Linux”. The shared directory can be accessed directly on windows.

For example, to access the home directory of 192.168.1.3, you can directly enter it on the path of the windows file manager\\192.168.1.3\quantum。 Of course, if you’re not sure which shared directory to access, you can just enter\\192.168.1.3, view all shared directories

|Server IP | Samba account | Samba password|
|192.168.1.3 | Linux account (e.g. whqt) | 123456|
| | | |
| | | |

The benefits of using Samba are that copying logs and editing configuration files can be carried out directly under windows, or even using visual studio to open the code project on the server for development under windows. Even if the server is restarted, it will not be affected.

Installing and configuring Samba services

Installing Samba

yum install samba

Configuring Samba

Add user and password for Samba

#The user added here must be an existing Linux user on this machine
 smbpasswd -a whqt

Boot Samba service

Because centos7 adopts relatively strict permission management and access control (IPtable and SELinux), these will affect the operation of samba server. Considering that the server is located in the company’s intranet and is relatively safe, these services will be disabled directly at the startup stage

Centos7 uses chkconfig to manage services

Chkconfig script template

Each service managed by chkconfig needs to add two or more lines of comments to the script

  • Tell chkconfig the run level of the default startup and the priority of startup and shutdown. For example:# chkconfig: 2345 55 25
  • Description of the service

The service script needs to be implementedstart,stop,restart,statusParameter functions, in short, the following functions need to be realized:

case $1 in
    start)    do_start;;
    stop)     do_stop;;
    restart)  do_restart;;
    status)
            echo "Status of $DESC: "
            check_status
            exit "$?"
            ;;
   *)

A complete script

#!/bin/bash
# chkconfig: 2345 55 25
#description: the environment init script
# useage manual
# add to system config
#   chkconfig --add xyd_init.sh
#   chkconfig --level 345 xyd_init.sh on
# start the service
#   service xyd_init.sh start
#   service xyd_init.sh stop
#   service xyd_init.sh restart
#   service xyd_init.sh status

do_start_EX() {
    setenforce 0
    service iptables stop
    service smb restart
    echo "sambda service start success"
}

do_start() {
    do_start_EX
}

do_stop_EX() {
    service smb stop
    echo "sambda servoce stopped"
}

do_stop() {
    do_stop_EX $i
}

do_restart() {
    do_stop
    do_start
}

check_status() {
    echo "do nothing"
}

case $1 in
    start)    do_start;;
    stop)     do_stop;;
    restart)  do_restart;;
    status)
            echo "Status of $DESC: "
            check_status
            exit "$?"
            ;;
   *)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac

Add service to chkconfig

Copy script to/etc/init.d/Directory and add executable permissionsChmod + X service script

Execute the following command to add the service script to chkconfig for management

Chkconfig -- add service script
Chkconfig -- level 2345 service script on

This completes the configuration of service startup

Manually start the services managed by chkconfig

Just like starting any ordinary service

sudo service xyd_init.sh start

complete