参考链接:
smb.conf
添加硬盘后自动挂载

一、无密码共享文件夹

安装

1
yum install samba -y

修改配置文件

1
cp -a smb.conf smb.conf.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
workgroup = WORKGROUP  
server string = Samba Server %v
netbios name = centos
security = user
map to guest = bad user
dns proxy = no
#============================ Share Definitions ==============================
[share]
path = /data
browsable = yes
writable = yes
guest ok = yes
read only = no

设置共享文件夹

1
2
mkdir /data
chmod 777 /data

查看端口及防火墙

1
2
3
4
[root@localhost ~]# ss -tnl 
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:445 *:*
LISTEN 0 50 *:139 *:*
1
2
3
4
5
6
7
8
[root@localhost ~]# firewall-cmd --state
running
[root@localhost data]# firewall-cmd --zone=public --add-port=445/tcp --permanent
success
[root@localhost data]# firewall-cmd --zone=public --add-port=139/tcp --permanent
success
[root@localhost data]# firewall-cmd --reload
success

配置完成

在这里插入图片描述

二、有密码共享文件夹

修改配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
workgroup = WORKGROUP  
server string = Samba Server %v
netbios name = centos
security = user
map to guest = bad user
dns proxy = no
#============================ Share Definitions ==============================
[share]
path = /data
valid users = share #添加用户
browsable = yes
writable = yes
guest ok = yes
read only = no

创建指定账户访问

1
2
3
4
5
[root@localhost ~]# useradd share
[root@localhost data]# smbpasswd -a share
New SMB password:
Retype new SMB password:
Added user share.

再次访问

在这里插入图片描述

三、添加硬盘后增加新的共享文件夹

  1. 查看服务器安装的硬盘状态(==可以看到有sdb硬盘,但是没有分区==)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    [root@localhost ~]# fdisk -l

    Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x000a9170

    Device Boot Start End Blocks Id System
    /dev/sda1 2048 8390655 4194304 82 Linux swap / Solaris
    /dev/sda2 * 8390656 104857599 48233472 83 Linux

    Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
  2. 添加新分区
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    [root@localhost ~]# fdisk /dev/sdb
    Welcome to fdisk (util-linux 2.23.2).

    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.

    Device does not contain a recognized partition table
    Building a new DOS disklabel with disk identifier 0x78047679.

    Command (m for help): n
    Partition type:
    p primary (0 primary, 0 extended, 4 free)
    e extended
    Select (default p): p
    Partition number (1-4, default 1): 1
    First sector (2048-41943039, default 2048):
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
    Using default value 41943039
    Partition 1 of type Linux and of size 20 GiB is set

    Command (m for help): W
    The partition table has been altered!

    Calling ioctl() to re-read partition table.
    Syncing disks.
    根据提示依次输入

n

p

1

两次回车

w回车

3.查看分区(可以看到/dev/sdb1这个分区)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@localhost ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a9170

Device Boot Start End Blocks Id System
/dev/sda1 2048 8390655 4194304 82 Linux swap / Solaris
/dev/sda2 * 8390656 104857599 48233472 83 Linux

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x78047679

Device Boot Start End Blocks Id System
/dev/sdb1 2048 41943039 20970496 83 Linux

4.格式化分区(mkfs -t ext4 -c /dev/sdb1)

-t 制定要把磁盘格式化成什么类型

-c 在建立文件系统之前检查坏道

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost ~]# mkfs -t ext4 -c /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242624 blocks
262131 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000

Checking for bad blocks (read-only test): done
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

5.挂载目录

1
2
mkdir /document
mount /dev/sdb1 /document
1
2
3
4
5
6
7
8
9
[root@localhost /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 46G 2.6G 44G 6% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 4.0K 1.9G 1% /dev/shm
tmpfs 1.9G 8.8M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
tmpfs 380M 0 380M 0% /run/user/0
/dev/sdb1 20G 45M 19G 1% /document

6.设置开机自动挂载(先用blkid命令查看分区的UUID,使用UUID来绑定挂载的目录)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost /]# blkid
/dev/sda1: UUID="9824e948-e873-4f85-b180-ea2d65718a38" TYPE="swap"
/dev/sda2: UUID="939f9e59-36cc-40d4-b7f6-302a85f949fb" TYPE="xfs"
/dev/sdb1: UUID="05a31bcf-1bb9-4076-b268-754a22f0da83" TYPE="ext4"
[root@baota /]# vi /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jan 10 15:18:10 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=939f9e59-36cc-40d4-b7f6-302a85f949fb / xfs defaults 0 0
UUID=9824e948-e873-4f85-b180-ea2d65718a38 swap swap defaults 0 0
UUID=05a31bcf-1bb9-4076-b268-754a22f0da83 /document ext4 defaults 0 0

7.重启系统后df -h查看是否自动挂载

1
2
[root@localhost ~]# df -h |grep /dev/sdb1
/dev/sdb1 20G 45M 19G 1% /document

8.将挂载的目录设置为新的共享文件夹

修改配置文件/etc/samba/smb.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
workgroup = WORKGROUP  
server string = Samba Server %v
netbios name = centos
security = user
map to guest = bad user
dns proxy = no
#============================ Share Definitions ==============================
[share]
path = /data
valid users = share #添加用户
browsable = yes
writable = yes
guest ok = yes
read only = no
#============================ Share Definitions ==============================
[document]
path = /document
valid users = share #添加用户
browsable = yes
writable = yes
guest ok = yes
read only = no

9.重启smb服务,配置完成

service smb restart