Projekt

Allgemein

Profil

Setup samba » Historie » Version 5

Jeremias Keihsler, 17.09.2024 21:09

1 1 Jeremias Keihsler
h1. Install Procedure for samba
2
3
h2. Requirements
4
5
To install samba you will need the following:
6
* a installed and supported operating system (e.g. CentOS 9.x)
7
* root-access
8
* a fast internet connection
9
10
h2. Preliminary Note
11
12
this is based on http://jehurst.wordpress.com/2011/01/17/rhel-6-for-the-clueless-samba-server/
13
and the samba for groups part is taken from https://www.techrepublic.com/article/how-to-set-up-samba-shares-for-groups/
14
15
I’ve found a couple of tutorials on Samba, but neither one had all the right information. After fighting with it a bit, this is what I did to get it working.
16
17
h2. Install 
18
19
Install Samba by logging into a Terminal as root:
20
<pre><code class="bash">
21 2 Jeremias Keihsler
dnf install samba
22 1 Jeremias Keihsler
</code></pre>
23
if you want to have access to samba-shares you also want to
24
<pre><code class="bash">
25 2 Jeremias Keihsler
dnf install samba-client
26 1 Jeremias Keihsler
</code></pre>
27
28
h2. Setup 
29
30
h3. Setup SeLinux
31
32
If SeLinux is active, then it might be necessary to set some samba-related variables depending on the share-location.
33
34
This and more information can be found at http://selinuxproject.org/page/SambaRecipes
35
36
<pre><code class="bash">
37
setsebool -P samba_domain_controller on
38
</code></pre>
39
40
The @samba_export_all@ Flag will allow to share any folder on the machine, use with care.
41
<pre><code class="bash">
42
setsebool -P samba_export_all_rw=1
43
</code></pre>
44
45
<pre><code class="bash">
46
setsebool -P samba_enable_home_dirs=1
47
</code></pre>
48
49
h3. Setup a shared directory
50
51 3 Jeremias Keihsler
Create shared directory; I used /mnt/shared:
52 1 Jeremias Keihsler
53
<pre><code class="bash">
54 3 Jeremias Keihsler
mkdir /mnt/shared
55
chown -R nobody:nobody /mnt/shared
56 1 Jeremias Keihsler
chmod a+w /home/shared
57 3 Jeremias Keihsler
chcon -t samba_share_t /mnt/shared
58 1 Jeremias Keihsler
</code></pre>
59
60
That last line insures the SELinux security system knows to allow outside systems to poke around in that folder. Now anyone using this computer can move files in and out of the folder, as well as the Samba users.
61
62
h3. Setup a samba user
63
64
Add a Samba user. This is a different task than simply adding a user account. There is a GUI tool for adding Linux user accounts to the machine for them to use the computer itself. However, Samba users must be handled differently, so that the system forces them to use the Samba server.
65
66
<pre><code class="bash">
67
useradd -c "Real Name" -d /home/samba-username -s /sbin/nologin samba-username
68
</code></pre>
69
70
That’s all one line. As usual, substitute the actual Real Name and samba-username in the command above. Then create the Samba password. Remember what we said about coming up with good passwords:
71
72
<pre><code class="bash">
73
smbpasswd -a samba-username
74
</code></pre>
75
76
It will prompt for the password, which you type in blindly:
77
78
<pre><code class="bash">
79
New SMB password:
80
Retype new SMB password:
81
Added user username.
82
</code></pre>
83
84
Edit smbusers:
85
<pre><code class="bash">
86
vim /etc/samba/smbusers
87
</code></pre>
88
89
This will open the default text editor. Scan down the file until you see something like this:
90
91
<pre><code class="bash">
92
root = administrator admin
93
nobody = guest pcguest smbguest
94
</code></pre>
95
96
Immediately below this, add a line with this format:
97
98
<pre><code class="bash">
99
username = samba-username
100
</code></pre>
101
102
so CentOS recognizes the person logging in from the Winbox by their samba-username.
103
104
h3. Setup a samba config
105
106
Then open: 
107
<pre><code class="bash">
108
vim /etc/samba/smb.conf
109
</code></pre>
110
Find the section headed '[global]'. Change the workgroup name to whatever your Windows computer will be seeking. Default is workgroup in lower case letters. You’ll need to remove the semicolon in front of the next line and provide a proper hostname for the netbios name, which would be the name you gave your RHEL computer during installation, again in lower case. Remove the semicolon from the next line and the IP address numbers from the sample; all we need are the two interfaces lo eth0. Below that is a line with hostsallow as a model. Below that, start a new line with the same indentation:
111
112
<pre><code class="bash">
113
hosts allow = 127. 192.168.1.
114
</code></pre>
115
116
The “127.” is the IP address for everything on your own machine. The other (192.168.1.) is the private LAN network I use for my home router; by leaving off the last section after the dot, it automatically includes every computer with that prefix, which is reserved for LANs.
117
118
If you want to bind to specific interfaces only you maybe want to consider
119
<pre><code class="bash">
120
interfaces = lo vboxnet0 192.168.56.1/24
121
bind interfaces only = yes
122
</code></pre>
123
124
Go all the way to the bottom of the file and add some lines. I named my shared directory “shared”. Thus, the section heading should be named the same:
125
126
<pre><code class="ini">
127
[shared]
128
path = /home/shared
129
writeable = yes
130
browseable = yes
131
read only = No
132
guest ok = Yes
133
public = Yes
134
valid users = username1 username2
135
create mask = 0666
136
directory mask = 0777
137
</code></pre>
138
139
if you want to have a trash-bin on the share, you might consider adding following section:
140
<pre><code class="bash">
141
vfs object = recycle
142
  recycle:repository = .deleted/%U
143
  recycle:keeptree = Yes
144
  recycle:touch = Yes
145
  recycle:versions = Yes
146
  recycle:maxsixe = 0
147
  recycle:exclude = *.tmp
148
  recycle:exclude_dir = /tmp
149
  recycle:noversions = *.bak
150
</code></pre>
151
152
h2. Firewall
153
154 4 Jeremias Keihsler
Now change the firewall to allow Samba to get through. Optionally checkmark IPP printer sharing.
155 1 Jeremias Keihsler
156
<pre><code class="bash">
157
firewall-cmd --permanent --zone=public --add-service=samba
158
firewall-cmd --reload
159
</code></pre>
160
161
h2. Service
162
163
enable and start of the services with
164
<pre><code class="bash">
165
systemctl enable smb.service
166
systemctl enable nmb.service
167
systemctl restart smb.service
168
systemctl restart nmb.service
169
</code></pre>
170
171
h2. Test 
172
173
following commands might be helpful:
174
<pre><code class="bash">
175
findsmb
176
smbclient //host/share -U username
177
</code></pre>
178
179
h1. Samba working with groups
180
181
Create the necessary directory and group
182
183
Before we configure Samba, let's create the necessary directory and group. We'll then add users to the group.
184
185
I'll be creating a new share called editorial. Create a new directory with the command:
186
187
sudo mkdir -p /opt/editorial
188
189
Now let's create the group editorial with the command:
190
191
sudo groupadd editorial
192
193
Now we change the group ownership and permissions of the directory with the commands:
194
195
sudo chgrp editorial /opt/editorial
196
​sudo chmod -R 770 /opt/editorial
197
198
Now we add users to the new group with the command:
199
200
sudo usermod -a -G editorial USER
201
202
Where USER is the username to add to the group.
203
204
If you ever need to remove a user from a group, this can be done with the command:
205
206
sudo userdel USER GROUP
207
208
Where USER is the username and GROUP is the group name.
209
210
Finally, we must add the users to Samba. This is done with the smbpasswd command like so:
211
212
sudo smbpasswd -a USER
213
​sudo smbpasswd -e USER
214
215
Where USER is the username to be added. The first command adds the user and the second command enables the user. When issuing the first of the above commands, you will be prompted to create a new Samba password for the user.
216
Configure Samba
217
218
Now we come to the actual Samba configuration. The first thing we're going to do is make a backup copy of our Samba configuration file. Issue the command:
219
220
sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.BAK
221
222
Now create a new configuration file with the following command:
223
224
sudo nano /etc/samba/smb.conf
225
226
In this new file, we'll add the following contents to share out our editorial directory to the group editorial (customize as needed):
227
228
[global}
229
​workgroup = WORKGROUP
230
​server string = Editorial Server
231
​netbios name = Ubuntu
232
​security = user
233
​map to guest = bad user
234
​dns proxy = no
235
236
#### SHARES ####
237
238
[editorial]
239
​path = /opt/editorial
240
​browsable = yes
241
​writable = yes
242
​guest ok = yes
243
​read only = no
244
​valid users = @editorial
245
246
Save and close that file. Restart Samba with the commands:
247
248
sudo systemctl restart smbd.service
249
​sudo systemctl restart nmbd.service
250
251
You can now point one of your machines to the newly configured Samba share. So long as the user is a member of the editorial group, they'll be able to log on with their username and samba password.
252
Even more flexibility
253
254
By working with groups in Samba, you can make your admin life slightly easier, while making Samba more flexible. With this way you can add and remove users to the group with ease (which, in turn, would revoke their access to the Samba share).
255
256
h1. Samba Migrate 
257
258
h2. Data
259
260
rsync with option -a will transfer the files with all necessary permissions
261
262
<pre><code class="shell">
263
rsync -a --verbose source target
264
</code></pre>
265
266
267
h2. User Accounts
268
269
I run a Samba file server on Ubuntu in my company. Moving from an old to a new server means I have to copy the share-data and the samba config file /etc/smb.conf.
270
271
Moving the user accounts is not hard if you know where to look. I'm not running a domain controller active directory, ldap or anything. That means, my user data is in these files:
272
273
    /etc/passwd (users)
274
    /etc/shadow (hashed passwords)
275
    /etc/skel (the template for /home/$newuser/)
276
    /etc/group (system groups)
277
    /var/lib/samba/passdb.tdb
278
279
Copy Users and Passwords
280
281
Don't just copy /etc/passwd and /etc/shadow. Instead copy paste the lines of the users to the corresponding files on your new server. In my case those where the users with in id of 1000 and higher. Don't copy paste the system users.
282
Copy Samba passwords
283
284
Samba can't use /etc/shadow because it uses a different hash algorithm. So each user has an entry in passdb.tdb. That file is binary but in my case it was okay to copy it completely.
285
286
The location has changed. On my old server passdb.tdb was in /var/lib/samba/ and the new server has it in /var/lib/samba/private/. If you want to find the right location this command might help
287
288
$ smbd -b |grep -e 'PRIVATE_DIR'
289
PRIVATE_DIR: /var/lib/samba/private
290
291
After copying the passdb.tdb file, I restarted samba
292
293
$ service smbd restart
294
295
I did not copy the /etc/group file (yet). So far, the transition went smoothly, the users did not have to reset their password.
296
Changing passwords by hand
297
298
In case you want to change a user password, it is a good idea to use
299
300
$ smbpasswd -U username
301
302
that will also update the regular password of that user on this linux machine if /etc/samba/smb.conf has the setting
303
304
unix password sync = yes