Step 1 learn how to share Samba (v0.2b) files between Linux and Windows)

Source: Internet
Author: User
Tags ftp commands pkill

Author:You need to participate in the process of adding;
From:Linuxsir. org
Introduction:This article only talks about the most commonly used Linux and Windows Shared Files, mainly to guide new fingers. How to create the simplest Samba server and describe where to find a solution to the problem. The update is in progress. Thank you for your participation;

Directory

0. Prerequisites for setting up a Samba server;0.1 tools for viewing the file content and editing the file;
0.2 related to files and directories;
03 users and user groups;
04 process management;1 Samba Introduction
2 Samba functions and application scope
3. Start two Samba serversProgramClient and server configuration files;3.1 samba has two servers: smbd and nmbd;
3.2 view the port and firewall of the Samba server;
3.3 view the configuration file of the Samba server;
3.4 some samba tools (server and client) in Linux );
3.5 common tools in Linux: how to view Linux sharing in windows; 3.5.1 how to view Windows shared files in the network and Samba shared files in Linux;
3.5.2 access the file shared by Linux Samba server in windows;
3.5.3 mount the smbfs file system;

4. The simplest example is the implementation of readable and writable anonymous users;

Step 1: Change smb. conf
Step 2: create a directory and grant permissions;
Step 3: Start smbd and nmbd servers;
Step 4: Check the smbd process and check whether the Samba server is running;
Step 5: Access Samba server sharing;

5. A complicated User Sharing Model (suitable for small enterprises with around 10 persons );

5.1 shared permission design and implementation functions;
5.2 create a directory on the server;
5.3 Add a user group and Set permissions for the corresponding directory home directory; 5.3.1 Add a user group;
5.3.2 Add a user;
5.3.3 Add a samba user and set the password;
5.3.4 configure the permissions and ownership of related directories;

5.4 modify the samba configuration file;
5.5 client access;
5.5.1 Windows client access;


++ ++
Body
++ ++

0. Prerequisites for setting up a Samba server;

Linux is a multi-user operating system. Setting up any server is related to users, user groups, and permissions. This is the basis for operations. The Samba server is no exception.
It is also extremely important. Although the shared file server can also be mounted on Windows systems, its permission control is not flattering. If we use Windows to frame the network shared file system
That is, we can also be confused by clicking the mouse. But in Linux, we can easily change the configuration file and build our Samba server in less than a few minutes. Which is easier? Only you know
Path;

For the setup of Samba server, some brothers simply think that they only need to change the configuration file and create the corresponding directory. In fact, this is not the case. We need to do more in-depth work, such as directory permissions and ownership, that is, to allow users and user groups to have read and write permissions. Only by combining the configuration file with the permission of the shared directory can the Samba server be ready;

The following are common basic knowledge. We can find corresponding explanations in the list below for all the content involved in this article;


0.1 tools for viewing the file content and editing the file;

Introduction to Linux File Content viewing tools
File editor vi


0.2 related to files and directories;

Linux File Types and file extensions
Linux file and directory management: listing, deleting, copying, moving, and renaming
Linux file and directory attributes
Brief Introduction to Linux File Search


03 users and user groups;

Linux User and user group management overview
User and group configuration file details
Introduction to Linux User management tools
Linux user query


04 process management;

Linux Process Management

1 Samba Introduction

Samba (SMB) is a network server used for sharing files between Linux and Windows. Samba
That is, it can be used for shared files between Windows and Linux. It is also used for shared files between Linux and Linux. However, for shared files between Linux and Linux
For better network file systems (NFS), servers must be set up for NFS;

We all know that each machine in the Windows network can be a file sharing server or a client. Samba
The same can be done. For example, if a Linux machine has a Samba Server
It can act as a shared server, and can also be used as a client to Access Windows shared file systems in other networks, or other Linux sabmba servers;

In Windows network, we can see the shared file function, we can directly use the shared folder as a local hard disk. In Linux, Samba is used to provide a shared file system to machines on the network. It can also be used to mount the sharing of other machines on the local machine; in a sense, this is different from ftp.

The NetBIOS protocol used by Samba. If you fail to use samba,

Linux and Windows, Linux, and Linux

2 Samba functions and application scope

Samba should be mainly used in networks where Windows and Linux systems coexist. If a network environment is a Linux or UNIX system, it is better to use NFS if Samba is not necessary;

So what services does Samba provide for us? Sharing Files and printers;

3 Samba two server-related startup programs, client and server configuration files;

3.1 samba has two servers: SMB and nmb;

SMB is the main startup server of Samba, allowing other machines to know what the machine shares. If the nmb server is not enabled, it can only be accessed through IP addresses, for example, enter the following entry in Windows IE browser for access;

\ 192.168.1.5 \ shared directory
\ 192.168.1.5 \ OPT

Nmb is used for parsing. What does it resolve? Is to shareWorking GroupAndNetBIOS NameResolved;

In general, in the RPM package system, if Samba is installed in the RPM package, you can generally start the Samba server in the following ways;

[Root @ localhost ~] #/Etc/init. d/SMB start
Start the smb service: [OK]
Start nmb service: [OK]

What if it stops? Add stop after SMB; restart is restart

[Root @ localhost ~] #/Etc/init. d/SMB stop
[Root @ localhost ~] #/Etc/init. d/SMB restart

For all systems, the common method is to run SMB and nmb directly. Of course, you need to know the directory where SMB and nmb are located. If Samba is compiled by yourself, you should know where you put Samba;

[Root @ localhost ~] #/Usr/sbin/smbd
[Root @ localhost ~] #/Usr/sbin/nmbd

Run the following command to check whether the server is running;

[Root @ localhost ~] # Pgrep smbd
[Root @ localhost ~] # Pgrep nmbd

To disable the Samba server, you can also use the following methods, which are mostly common. You need the root permission to execute them;

[Root @ localhost ~] # Pkill smbd
[Root @ localhost ~] # Pkill nmbd

3.2 view the port and firewall of the Samba server;

How can this be used? Sometimes your firewall may block the port of the smbd server, so we should use the port occupied by the smbd server. In the following view, we know that the ports occupied by smbd are 139 and 445;

[Root @ localhost ~] # Netstat-tlnp | grep SMB
TCP 0 0 0.0.0.0: 139 0.0.0.0: * Listen 10639/smbd
TCP 0 0 0.0.0.0: 445 0.0.0.0: * Listen 10639/smbd

If you have a firewall, you must open these two ports. If you do not know how to open it. Maybe you are a newbie like me, or clear firewall rules;

[Root @ localhost ~] # Iptables-F
Or
[Root @ localhost ~] #/Sbin/iptables-F

3.3 view the configuration file of the Samba server;

If we use the samba software package that comes with the Linux release, the configuration files of the Samba server are generally located in the/etc/samba directory.
The file is smb. conf; there are also user configuration files
Smbpasswd, smbusers, and LmHosts (you 'd better check the content of these files); there is also a file secrets. TDB, which is
The Samba server is automatically generated by the startup. Let's add the descriptions of these files according to the progress of the tutorial. All of them are said at once, and there is too much content, so we can only generate;

3.4. Some Samba tools (server and client) in Linux );

Smbcacls smbcontrol smbencrypt smbmount smbprint smbstatus smbtree
Smbclient smbcquotas smbmnt smbpasswd smbspool smbtar smbumount
Smbd nmbd Mount

Among them, the server side is smbd, nmbd, smbpasswd, and most others are clients. These are not all well-versed, but at least a few must be used; for example, smbmount (that is, the usage of the Mount parameter) and smbclient;

3.5 common tools in Linux: Mount (smbmount) and smbclient; how to view Linux sharing in windows;

3.5.1 view Windows shared files in the network and Samba shared files in the Linux system;

In general, we need to use smbclient; common usage is nothing more than the following;

[Root @ localhost ~] # Smbclient-l // ip address or computer name

Smbclient is a Linux client of Samba. It is used on a Linux machine to view shared resources on the server. Like FTP, users can log on to the Samba server or upload put and download get files, unfortunately, Chinese support is unfriendly;

View resources on the server;

Smbclient-l // ip [-u user name]

If your samba server is configured in user mode, add "-u username". If it is in share mode, skip this step;

For example:

[Root @ localhost ~] # Smbclient-l // 192.168.1.3-u sir01
Password: enter the password of sir01.

If you use the share mode, you can directly use it without paying attention to the user and password;

[Root @ localhost ~] # Smbclient-l // 192.168.1.3
Password: Press enter.

Logon user identity Samba server sharing

After logging on to and sharing as a user, you can upload and download files in the same way as an FTP user. You can use put to upload files and get to download files;

Smbclient // ip address/Shared Folder-u user

Note: IP addresses are all known. If you do not know your own IP addresses, you can use/sbin/ifconfig to view them. Shared Folders are stored in SMB. [shared folder] defined in Conf, such as [sir01]. -U user name indicates the samba user;

For example:

[Root @ localhost ~] # Smbclient // 192.168.1.3/sir01-u sir01
Password:
Domain = [linuxsir] OS = [UNIX] Server = [Samba 3.0.21b-2]
SMB: \> ls

Note:After logging on to the Samba server, you can use commands of smbclient to upload and download files like FTP commands;

Smbclient command description

Command description

? Or help [command] provides help for help or a command
! [Shell Command] Run the shell command used, or let the user enter the shell prompt
Switch the CD [Directory] to the specified directory on the server. If not, smbclient returns the current local directory.
Switch the LCD [Directory] to the specified directory on the client;
DIR or ls to list files in the current directory;
Exit or quit to exit smbclient
Get file1 file2 downloads file1 from the server and stores it on the local machine with the file name file2. If you do not want to change the name, you can omit file2.
Mget file1 file2 file3 filen downloads multiple files from the server;
The MD or mkdir directory is used to create a directory on the server.
Rd or rmdir directory Delete the directory on the server
Put file1 [file2]: upload a file file1 to the server and upload it to the server;
Mput file1 file2 filen upload multiple files to the server

3.5.2 access the file shared by Linux Samba server in windows;

In this case, you can view the workgroup on the Internet, or enter the following in your browser:

\ IP address or computer name

In this way, you can see what is shared on this machine, and click the mouse to complete the operation. If you cannot access the machine, do not forget to clear the Linux Firewall planning, or let the corresponding port pass;

3.5.3 mount the smbfs File System in Linux;

Mount usage: load the shared folder in the network to the local machine. Mount is used to mount the file system. SMB can also be used as a network file system; smbmount is also a variant of mount;

Mount mounting smbfs usage;

Mount-T smbfs-O codePage = cp936, username = username, password = password,-l // ip address/Shared Folder name mount point
Or
Mount-T smbfs-O codePage = cp936, username = username, password = password,-l // computer name/Shared Folder name mount point
Or
Mount-T smbfs-O codePage = cp936 // ip address or computer name/Shared Folder name mount point

Usage of smbmount:Smbmount-O username = user name, password = password,-l // ip address or computer name/Shared Folder name mount point
Smbmount // ip address or computer name/Shared Folder name mount point

Note:

If your server is shared by share, you can mount the server without the user name or password. If a password prompt is displayed, press Enter. You can also mount the server with smbmount, in this way, you do not need to use Mount-T smbfs to specify the file system type;

For the mount point, we need to create a folder by ourselves. For example, we can create a folder in/opt/smbhd. You can say this;

In the mount command, we found that there is such a parameter codePage = cp936, which is the encoding of the server-side file system, cp936 is simplified Chinese, of course you can use utf8, etc, try it.

If you mount a remote smbfs File System with garbled characters in simplified Chinese, you must specify the encoding when mounting the file system;

4. The simplest example is the implementation of readable and writable anonymous users;

Step 1: Change smb. conf

Let's implement a simple function so that all users can read and write a folder shared by a Samba server. We need to modify smb. conf. First, back up the smb. conf file;

[Root @ localhost ~] # Cd/etc/samba
[Root @ localhost Samba] # mv smb. conf SMB. confbak

Then we will recreate an smb. conf file;

[Root @ localhost Samba] # Touch smb. conf

Then we write the following section into smb. conf;

[Global]
Workgroup = linuxsir
NetBIOS name = linuxsir05
Server String = Linux Samba server testserver
Security = Share

[Linuxsir]
Path =/opt/linuxsir
Writeable = Yes
Browseable = Yes
Guest OK = Yes

Note:

[Global] This section is a global configuration and is required. There are the following rows;

Workgroup is the working group displayed in windows. Here I set linuxsir (in upper case );
NetBIOS name is the name of the computer displayed in windows;
Server String is the description of the Samba server, which can be defined by yourself. This is not important;
Security is the authentication and login method. Here we use share. There are many authentication methods, one of which is also commonly used as the user authentication method. If share is used, you do not need to set the user and password;

[Linuxsir] the shared directory is displayed in windows;
Path = You can set where the directory to be shared is stored;
Whether writeable can be written. Here I set it to writable;
Whether browseable can be viewed or not. If browsed, we can see shared folders under the Working Group. If you do not want to display it, set it to browseable = No.

Guest OK anonymous users log on as guest;

Step 2: create a directory and grant permissions;

[Root @ localhost ~] # Mkdir-P/opt/linuxsir
[Root @ localhost ~] # ID nobody
Uid = 99 (nobody) gid = 99 (nobody) groups = 99 (nobody)

[Root @ localhost ~] # Chown-r nobody: Nobody/opt/linuxsir

Note:For nobody authorization, we first use the id command to view the information of the nobody user. It is found that the user group is also nobody. We must take this as the standard. In some systems, the nobody user group is not the nobody;

Step 3: Start smbd and nmbd servers;

[Root @ localhost ~] # Smbd
[Root @ localhost ~] # Nmbd

Step 4: Check the smbd process and check whether the Samba server is running;

[Root @ localhost ~] # Pgrep smbd
13564
13568

Step 5: Access Samba server sharing;

In Linux, you can use the following command to access;[Root @ localhost ~] # Smbclient-l // linuxsir05
Password: Note: Press enter directly.

In Windows, you can use the following method to access;

\ Linuxsir05 \


5. A complicated User Sharing Model (suitable for small enterprises with around 10 persons );

For example, a company has five departments: linuxsir, sir01, sir02, sir03, and sir04. We want to design a secure shared file for this company.
Model. Each user has his/her own network disk, and sir01 to sir04 also have a shared network hard disk. All users (including anonymous users) have a shared database, which is read-only for security purposes; all
Users (including anonymous users) must have a folder to which the temporary files are finally converted .......


5.1 shared permission design and implementation functions;

1) The linuxsir department has the permission to manage all SMB spaces;
2) sir01 to sir04 have their own space and have absolute privacy for other users except themselves and linuxsir;
3) linuxsir01 to linuxsir04 have a space for common read and write permissions;
4) all users (including anonymous users) have a space with read permission for the database, so data writing is not required.
5) There is also a common space between sir01 and sir04, which is private to users from sir01 to sir04 and cannot be accessed by other users.
6) There is also a space for all users to write, delete, and other functions, and there is no restriction on permissions for the final transfer of temporary documents for all users of the company;


5.2 create a directory on the server;

[Root @ localhost ~] # Mkdir-P/opt/linuxsir
[Root @ localhost ~] # Cd/opt/linuxsir
[Root @ localhost linuxsir] # mkdir sir01 sir02 sir03 sir04 sirshare sir01_rw sirallrw
[Root @ localhost linuxsir] # ls

Sir01 sir01_rw sir02 sir03 sir04 sirallrw sirshare

Note: The functions are as follows:

/Opt/linuxsir this is the Administrator directory, responsible for managing all the directories under it;
/Opt/linuxsir/sir01 is the home directory of sir01 and is used for private use. Except for the user itself and linuxsir, other users cannot read or write;
/Opt/linuxsir/sir02 is the home directory of sir02 and is used for private use. Except for the user itself and linuxsir, other users cannot read or write;
/Opt/linuxsir/sir03 is the home directory of sir03 and is used for private use. Except for the user itself and linuxsir, other users cannot read or write;
/Opt/linuxsir/sir04 is the home directory of sir04 and is used for private use. Except for the user itself and linuxsir, other users cannot read or write;
/Opt/linuxsir/sirshare users (except linuxsir has permission to write) read-only directory
/Opt/linuxsir/sir01_rw is used to read and write shared directories from sir01 to sir04, But anonymous users cannot read and write;
/Opt/linuxsir/sirallrw is used to read and write all users (including anonymous users;


5.3 Add a user group and set the permission for the home directory of the corresponding directory;


5.3.1 Add a user group;

[Root @ localhost ~] #/Usr/sbin/groupadd linuxsir
[Root @ localhost ~] #/Usr/sbin/groupadd sir01
[Root @ localhost ~] #/Usr/sbin/groupadd sir02
[Root @ localhost ~] #/Usr/sbin/groupadd sir03
[Root @ localhost ~] #/Usr/sbin/groupadd sir04
[Root @ localhost ~] #/Usr/sbin/groupadd sir0104


5.3.2 Add a user;

[Root @ cuc03 ~] # Adduser-G sir01-G sir0104-D/opt/linuxsir/sir01-S/sbin/nologin sir01
[Root @ cuc03 ~] # Adduser-G sir02-G sir0104-D/opt/linuxsir/sir02-S/sbin/nologin sir02
[Root @ cuc03 ~] # Adduser-G sir03-G sir0104-D/opt/linuxsir/sir03-S/sbin/nologin sir03
[Root @ cuc03 ~] # Adduser-G sir04-G sir0104-D/opt/linuxsir/sir04-S/sbin/nologin sir04
[Root @ cuc03
~] # Adduser-G linuxsir-D/opt/linuxsir-G
Linuxsir, sir01, sir02, sir03, sir04, sir0104-D/opt/linuxsir-S
/Sbin/nologin linuxsir

Why add a user like this? See:

Linux file and directory attributes
Introduction to Linux User management tools

Of course, we have to learn how to use tools to view user information, such as using finger and ID to view user information, mainly to see whether the user is correctly added; for example;SeeLinux user query

[Root @ localhost ~] # ID linuxsir
[Root @ localhost ~] # Finger linuxsir


5.3.3 Add a samba user and set the password;

The method we use is to add users first, but these users are all virtual users, because these users cannot log on to the system through shell. In addition, it is worth noting that the System user password and
The passwords of Samba users are different. If you have set a system user to be able to log on to the shell, you can set the samba password of the user to be different from the password of the System user to log on through the shell.

We use smbpasswd to add Samba users and Set passwords. The principle is to read the username in the/etc/passwd file.

[Root @ localhost sir01] # smbpasswd-A linuxsir
New smb password: add the password of Samba user linuxsir here;
Retype new smb password: Note: Enter it again;

Use the same method to add passwords for sir01, sir02, sir03, and sir04;


5.3.4 configure the permissions and ownership of related directories;

[Root @ cuc03 ~] # Chmod 755/opt/Linux
[Root @ cuc03 ~] # Chown linuxsir: linuxsir/opt/linuxsir

[Root @ cuc03 ~] # Cd/opt/linuxsir
[Root @ cuc03 ~] # Chmod 2770 sir0 *

[Root @ cuc03 ~] # Chown sir01.linuxsir sir01

[Root @ cuc03 ~] # Chown sir02.linuxsir sir02

[Root @ cuc03 ~] # Chown sir03.linuxsir sir03

[Root @ cuc03 ~] # Chown sir04.linuxsir sir04

[Root @ cuc03 ~] # Chown linuxsir. sir0104 sir01_rw

[Root @ cuc03 ~] # Chown linuxsir. linuxsir sirshare
[Root @ cuc03 ~] # Chmod 755 sirshare

[Root @ cuc03 ~] # Chown linuxsir: linuxsir sirallrw
[Root @ cuc03 ~] # Chmod 3777 sirallrw


5.4 modify the samba configuration file smb. conf;

The configuration file is as follows. After modifying/etc/samba/smb. conf, do not forget to restart the smbd and nmbd servers;

[Global]
Workgroup = linuxsir
NetBIOS name = linuxsir
Server String = Linux Samba test Server
Security = Share

[Linuxsir]
Comment = linuxsiradmin
Path =/opt/linuxsir/
Create mask = 0664
# Create mask is the permission mask used to create files. It is readable and writable to users, readable to user groups, and readable to other users;
Directory mask = 0775
# Directory mask is used to set the permission mask when a user creates a directory, which means that the user and user group can be read and written, and other users can be read and executed;
Writeable = Yes
Valid users = linuxsir
Browseable = Yes

[Sirshare]
Path =/opt/linuxsir/sirshare
Writeable = Yes
Browseable = Yes
Guest OK = Yes

[Sirallrw]
Path =/opt/linuxsir/sirallrw
Writeable = Yes
Browseable = Yes
Guest OK = Yes

[sir01_rw]
comment = sir01_rw
Path =/opt/linuxsir/sir01_rw
Create mask = 0664
directory mask = 0775
writeable = Yes
Valid users = linuxsir, @ sir0104
#@ sir0104 is a user group.
browseable = Yes

[Sir01]
Comment = sir01
Path =/opt/linuxsir/sir01
Create mask = 0664
Directory mask = 0775
Writeable = Yes
Valid users = sir01, @ linuxsir
Browseable = Yes

[Sir02]
Comment = sir02
Path =/opt/linuxsir/sir02
Create mask = 0664
Directory mask = 0775
Writeable = Yes
Valid users = sir02, @ linuxsir
Browseable = Yes

[sir03]
comment = sir03
Path =/opt/linuxsir/sir03
Create mask = 0664
directory mask = 0775
writeable = Yes
Valid users = sir03, @ linuxsir
browseable = Yes

[Sir04]
Comment = sir04
Path =/opt/linuxsir/sir04
Create mask = 0664
Directory mask = 0775
Writeable = Yes
Valid users = sir04, @ linuxsir
Browseable = Yes


5.5 client access;


5.5.1 windows access;

Open Windows's IE browser and access it with an IP address. The format is similar to \ 192.168.1.3. You can also use the shared folder locally. For example, if the sir01 folder is mounted locally, it should be mounted as a sir01 user. The Mount flowchart is as follows;

6. Update logs;

2006/01/23 v0.1b
2006/01/24 v0.2b

7. references;

Samba-howto

7. related documents;

Samba File Server user Complex permission model design and implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.