Projekt

Allgemein

Profil

Setup samba » Historie » Version 3

Jeremias Keihsler, 17.09.2024 14:47

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
Now change the firewall to allow Samba to get through. You can use the tool in System > Administration > Firewall. Simply scan down the list to Samba and checkmark the box. Optionally checkmark IPP printer sharing. Then hit “Apply”.
155
<pre><code class="bash">
156
firewall-config
157
</code></pre>
158
or in Textmode
159
<pre><code class="bash">
160
firewall-cmd
161
</code></pre>
162
163
<pre><code class="bash">
164
firewall-cmd --permanent --zone=public --add-service=samba
165
firewall-cmd --reload
166
</code></pre>
167
168
h2. Service
169
170
enable and start of the services with
171
<pre><code class="bash">
172
systemctl enable smb.service
173
systemctl enable nmb.service
174
systemctl restart smb.service
175
systemctl restart nmb.service
176
</code></pre>
177
178
h2. Test 
179
180
following commands might be helpful:
181
<pre><code class="bash">
182
findsmb
183
smbclient //host/share -U username
184
</code></pre>
185
186
h1. Samba working with groups
187
188
Create the necessary directory and group
189
190
Before we configure Samba, let's create the necessary directory and group. We'll then add users to the group.
191
192
I'll be creating a new share called editorial. Create a new directory with the command:
193
194
sudo mkdir -p /opt/editorial
195
196
Now let's create the group editorial with the command:
197
198
sudo groupadd editorial
199
200
Now we change the group ownership and permissions of the directory with the commands:
201
202
sudo chgrp editorial /opt/editorial
203
​sudo chmod -R 770 /opt/editorial
204
205
Now we add users to the new group with the command:
206
207
sudo usermod -a -G editorial USER
208
209
Where USER is the username to add to the group.
210
211
If you ever need to remove a user from a group, this can be done with the command:
212
213
sudo userdel USER GROUP
214
215
Where USER is the username and GROUP is the group name.
216
217
Finally, we must add the users to Samba. This is done with the smbpasswd command like so:
218
219
sudo smbpasswd -a USER
220
​sudo smbpasswd -e USER
221
222
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.
223
Configure Samba
224
225
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:
226
227
sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.BAK
228
229
Now create a new configuration file with the following command:
230
231
sudo nano /etc/samba/smb.conf
232
233
In this new file, we'll add the following contents to share out our editorial directory to the group editorial (customize as needed):
234
235
[global}
236
​workgroup = WORKGROUP
237
​server string = Editorial Server
238
​netbios name = Ubuntu
239
​security = user
240
​map to guest = bad user
241
​dns proxy = no
242
243
#### SHARES ####
244
245
[editorial]
246
​path = /opt/editorial
247
​browsable = yes
248
​writable = yes
249
​guest ok = yes
250
​read only = no
251
​valid users = @editorial
252
253
Save and close that file. Restart Samba with the commands:
254
255
sudo systemctl restart smbd.service
256
​sudo systemctl restart nmbd.service
257
258
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.
259
Even more flexibility
260
261
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).
262
263
h1. Samba Migrate 
264
265
h2. Data
266
267
rsync with option -a will transfer the files with all necessary permissions
268
269
<pre><code class="shell">
270
rsync -a --verbose source target
271
</code></pre>
272
273
274
h2. User Accounts
275
276
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.
277
278
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:
279
280
    /etc/passwd (users)
281
    /etc/shadow (hashed passwords)
282
    /etc/skel (the template for /home/$newuser/)
283
    /etc/group (system groups)
284
    /var/lib/samba/passdb.tdb
285
286
Copy Users and Passwords
287
288
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.
289
Copy Samba passwords
290
291
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.
292
293
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
294
295
$ smbd -b |grep -e 'PRIVATE_DIR'
296
PRIVATE_DIR: /var/lib/samba/private
297
298
After copying the passdb.tdb file, I restarted samba
299
300
$ service smbd restart
301
302
I did not copy the /etc/group file (yet). So far, the transition went smoothly, the users did not have to reset their password.
303
Changing passwords by hand
304
305
In case you want to change a user password, it is a good idea to use
306
307
$ smbpasswd -U username
308
309
that will also update the regular password of that user on this linux machine if /etc/samba/smb.conf has the setting
310
311
unix password sync = yes