Setup samba » Historie » Version 2
Jeremias Keihsler, 17.09.2024 13:59
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 | Create shared directory; I used /home/shared: |
||
52 | |||
53 | <pre><code class="bash"> |
||
54 | mkdir /home/shared |
||
55 | chmod a+w /home/shared |
||
56 | chcon -t samba_share_t /home/shared |
||
57 | </code></pre> |
||
58 | |||
59 | 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. |
||
60 | |||
61 | h3. Setup a samba user |
||
62 | |||
63 | 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. |
||
64 | |||
65 | <pre><code class="bash"> |
||
66 | useradd -c "Real Name" -d /home/samba-username -s /sbin/nologin samba-username |
||
67 | </code></pre> |
||
68 | |||
69 | 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: |
||
70 | |||
71 | <pre><code class="bash"> |
||
72 | smbpasswd -a samba-username |
||
73 | </code></pre> |
||
74 | |||
75 | It will prompt for the password, which you type in blindly: |
||
76 | |||
77 | <pre><code class="bash"> |
||
78 | New SMB password: |
||
79 | Retype new SMB password: |
||
80 | Added user username. |
||
81 | </code></pre> |
||
82 | |||
83 | Edit smbusers: |
||
84 | <pre><code class="bash"> |
||
85 | vim /etc/samba/smbusers |
||
86 | </code></pre> |
||
87 | |||
88 | This will open the default text editor. Scan down the file until you see something like this: |
||
89 | |||
90 | <pre><code class="bash"> |
||
91 | root = administrator admin |
||
92 | nobody = guest pcguest smbguest |
||
93 | </code></pre> |
||
94 | |||
95 | Immediately below this, add a line with this format: |
||
96 | |||
97 | <pre><code class="bash"> |
||
98 | username = samba-username |
||
99 | </code></pre> |
||
100 | |||
101 | so CentOS recognizes the person logging in from the Winbox by their samba-username. |
||
102 | |||
103 | h3. Setup a samba config |
||
104 | |||
105 | Then open: |
||
106 | <pre><code class="bash"> |
||
107 | vim /etc/samba/smb.conf |
||
108 | </code></pre> |
||
109 | 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: |
||
110 | |||
111 | <pre><code class="bash"> |
||
112 | hosts allow = 127. 192.168.1. |
||
113 | </code></pre> |
||
114 | |||
115 | 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. |
||
116 | |||
117 | If you want to bind to specific interfaces only you maybe want to consider |
||
118 | <pre><code class="bash"> |
||
119 | interfaces = lo vboxnet0 192.168.56.1/24 |
||
120 | bind interfaces only = yes |
||
121 | </code></pre> |
||
122 | |||
123 | 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: |
||
124 | |||
125 | <pre><code class="ini"> |
||
126 | [shared] |
||
127 | path = /home/shared |
||
128 | writeable = yes |
||
129 | browseable = yes |
||
130 | read only = No |
||
131 | guest ok = Yes |
||
132 | public = Yes |
||
133 | valid users = username1 username2 |
||
134 | create mask = 0666 |
||
135 | directory mask = 0777 |
||
136 | </code></pre> |
||
137 | |||
138 | if you want to have a trash-bin on the share, you might consider adding following section: |
||
139 | <pre><code class="bash"> |
||
140 | vfs object = recycle |
||
141 | recycle:repository = .deleted/%U |
||
142 | recycle:keeptree = Yes |
||
143 | recycle:touch = Yes |
||
144 | recycle:versions = Yes |
||
145 | recycle:maxsixe = 0 |
||
146 | recycle:exclude = *.tmp |
||
147 | recycle:exclude_dir = /tmp |
||
148 | recycle:noversions = *.bak |
||
149 | </code></pre> |
||
150 | |||
151 | h2. Firewall |
||
152 | |||
153 | 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”. |
||
154 | <pre><code class="bash"> |
||
155 | firewall-config |
||
156 | </code></pre> |
||
157 | or in Textmode |
||
158 | <pre><code class="bash"> |
||
159 | firewall-cmd |
||
160 | </code></pre> |
||
161 | |||
162 | <pre><code class="bash"> |
||
163 | firewall-cmd --permanent --zone=public --add-service=samba |
||
164 | firewall-cmd --reload |
||
165 | </code></pre> |
||
166 | |||
167 | h2. Service |
||
168 | |||
169 | enable and start of the services with |
||
170 | <pre><code class="bash"> |
||
171 | systemctl enable smb.service |
||
172 | systemctl enable nmb.service |
||
173 | systemctl restart smb.service |
||
174 | systemctl restart nmb.service |
||
175 | </code></pre> |
||
176 | |||
177 | h2. Test |
||
178 | |||
179 | following commands might be helpful: |
||
180 | <pre><code class="bash"> |
||
181 | findsmb |
||
182 | smbclient //host/share -U username |
||
183 | </code></pre> |
||
184 | |||
185 | h1. Samba working with groups |
||
186 | |||
187 | Create the necessary directory and group |
||
188 | |||
189 | Before we configure Samba, let's create the necessary directory and group. We'll then add users to the group. |
||
190 | |||
191 | I'll be creating a new share called editorial. Create a new directory with the command: |
||
192 | |||
193 | sudo mkdir -p /opt/editorial |
||
194 | |||
195 | Now let's create the group editorial with the command: |
||
196 | |||
197 | sudo groupadd editorial |
||
198 | |||
199 | Now we change the group ownership and permissions of the directory with the commands: |
||
200 | |||
201 | sudo chgrp editorial /opt/editorial |
||
202 | sudo chmod -R 770 /opt/editorial |
||
203 | |||
204 | Now we add users to the new group with the command: |
||
205 | |||
206 | sudo usermod -a -G editorial USER |
||
207 | |||
208 | Where USER is the username to add to the group. |
||
209 | |||
210 | If you ever need to remove a user from a group, this can be done with the command: |
||
211 | |||
212 | sudo userdel USER GROUP |
||
213 | |||
214 | Where USER is the username and GROUP is the group name. |
||
215 | |||
216 | Finally, we must add the users to Samba. This is done with the smbpasswd command like so: |
||
217 | |||
218 | sudo smbpasswd -a USER |
||
219 | sudo smbpasswd -e USER |
||
220 | |||
221 | 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. |
||
222 | Configure Samba |
||
223 | |||
224 | 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: |
||
225 | |||
226 | sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.BAK |
||
227 | |||
228 | Now create a new configuration file with the following command: |
||
229 | |||
230 | sudo nano /etc/samba/smb.conf |
||
231 | |||
232 | In this new file, we'll add the following contents to share out our editorial directory to the group editorial (customize as needed): |
||
233 | |||
234 | [global} |
||
235 | workgroup = WORKGROUP |
||
236 | server string = Editorial Server |
||
237 | netbios name = Ubuntu |
||
238 | security = user |
||
239 | map to guest = bad user |
||
240 | dns proxy = no |
||
241 | |||
242 | #### SHARES #### |
||
243 | |||
244 | [editorial] |
||
245 | path = /opt/editorial |
||
246 | browsable = yes |
||
247 | writable = yes |
||
248 | guest ok = yes |
||
249 | read only = no |
||
250 | valid users = @editorial |
||
251 | |||
252 | Save and close that file. Restart Samba with the commands: |
||
253 | |||
254 | sudo systemctl restart smbd.service |
||
255 | sudo systemctl restart nmbd.service |
||
256 | |||
257 | 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. |
||
258 | Even more flexibility |
||
259 | |||
260 | 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). |
||
261 | |||
262 | h1. Samba Migrate |
||
263 | |||
264 | h2. Data |
||
265 | |||
266 | rsync with option -a will transfer the files with all necessary permissions |
||
267 | |||
268 | <pre><code class="shell"> |
||
269 | rsync -a --verbose source target |
||
270 | </code></pre> |
||
271 | |||
272 | |||
273 | h2. User Accounts |
||
274 | |||
275 | 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. |
||
276 | |||
277 | 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: |
||
278 | |||
279 | /etc/passwd (users) |
||
280 | /etc/shadow (hashed passwords) |
||
281 | /etc/skel (the template for /home/$newuser/) |
||
282 | /etc/group (system groups) |
||
283 | /var/lib/samba/passdb.tdb |
||
284 | |||
285 | Copy Users and Passwords |
||
286 | |||
287 | 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. |
||
288 | Copy Samba passwords |
||
289 | |||
290 | 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. |
||
291 | |||
292 | 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 |
||
293 | |||
294 | $ smbd -b |grep -e 'PRIVATE_DIR' |
||
295 | PRIVATE_DIR: /var/lib/samba/private |
||
296 | |||
297 | After copying the passdb.tdb file, I restarted samba |
||
298 | |||
299 | $ service smbd restart |
||
300 | |||
301 | I did not copy the /etc/group file (yet). So far, the transition went smoothly, the users did not have to reset their password. |
||
302 | Changing passwords by hand |
||
303 | |||
304 | In case you want to change a user password, it is a good idea to use |
||
305 | |||
306 | $ smbpasswd -U username |
||
307 | |||
308 | that will also update the regular password of that user on this linux machine if /etc/samba/smb.conf has the setting |
||
309 | |||
310 | unix password sync = yes |