Setup Samba on CentOS-7 (on LXD)

Previously I had written an article about setting up a file server using FreeBSD and Samba I still think that FreeBSD is a great choice for a file server. That said I am now using LXC and LXD containers on Ubuntu and samba works very much the same way on CentOS-7 which I can run as a container on Ubuntu to get a very similar result. Setting Samba up on a base CentOS-7 install should work much the same way if you have a CentOS-7 server you can skip the Container Stuff.

I am assuming that your starting point is an up to date Ubuntu 16.04 server.

Networking Setup
for this use case we are assuming that our system is on a local network (not internet exposed) We want our container to have a LAN IP so that other computers on our LAN can talk to it there are a couple different ways to set this up. The way that I have found to be the easiest in my case has been to change the default container profile so that the nictype of the containers is set to macvlan.

run ifconfig which should give you something like this


enp0s25   Link encap:Ethernet  HWaddr d0:50:99:79:98:dd
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::d250:99ff:fe79:98dd/64 Scope:Link
          RX packets:1354182 errors:0 dropped:132 overruns:0 frame:0
          TX packets:704181 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1724747998 (1.7 GB)  TX bytes:598961741 (598.9 MB)
          Interrupt:20 Memory:efd00000-efd20000

You may have more adapters find the one that has the IP you ssh’d into and take note of its name in my case its enp0s25

Changing nictype to macvlan, lxc profile edit default will open the profile with nano.

Lxc profile edit default

nictype: bridged To -> nictype: macvlan

parent: whatever To -> parent: your primary nic name (enp0s25 in my case)

Container Stuff: Build a CentOS-7 Container

lxc launch images:centos/7/amd64 smbserver

drop into the containers shell

lxc exec smbserver /bin/bash

Setting up SAMBA on CentOS-7
If your not using containers then you should be SSH’d into your server now regardless go ahead and install updates,  If you have an Active Directory environment you can also setup samba to use AD for authentication using my notes here if not continue on.

install samba

yum install samba

start samba

systemctl enable smb.service
systemctl start smb.service

Adding users (both to the system and to samba)

# adding a user with useradd (-m tells useradd to create a home directory)
useradd -m joe

# set the users password so they can login
passwd joe

# set the samba password so they can login using samba
smbpasswd -a joe

If all you wanted is for users to be able to store things in their home folder than your done the default smb.conf configuration shares out home folders by default. if you want to create a different file share you will need to edit your smb.conf file.

vi /etc/samba/smb.conf

A basic file share for users bob, joe and the group @sysadmins

comment = My shared folder
path = /path to the shared folder
public = no
writable = yes
guest ok = no
valid users = joe, bob, @sysadmins

Group Setup for the sysadmins group and sysadmins share

groupadd sysadmins
mkdir /opt/sysadmins
chgrp sysadmins /opt/sysadmins
chmod -R 770 /opt/sysadmins
sudo usermod -a -G sysadmins jeff

Now all that’s left to do is go to a client system and try to access the share.


Replacing Windows file servers with CentOS7, SSSD, and Samba

This is very much still a work in progress.

These are some misc notes on how I’v gotten CentOS to host smb shares over the network and use active directory to authenticate users to them.

Before going any further get yourself a CentOS minimal install on whatever system you plan on using for your file server run updates and configure networking (give your VM/Container/Server a hostname that makes sense) grab a cup of coffee and open your favorite terminal emulator/ssh client, login and su yourself to root.

Disable selinux by setting SELINUX=disabled in /etc/sysconfig/selinux

vi /etc/sysconfig/selinux

If you want to keep selinux enabled you’ll have to run 

chcon -t samba_share_t /your_smb_share_dir

on every share you make. 

Install Samba and its friends

yum install realmd samba samba-common oddjob oddjob-mkhomedir sssd ntpdate ntp

Make sure samba can talk threw the firewall

firewall-cmd --permanent --add-service=samba
firewall-cmd --reload

setup system time

systemctl enable ntpd.service
ntpdate yourdomaincontroller.yourdomain.tld
systemctl start ntpd.service

Some have noted that in order for things to work right you might need to add your DC as a server entry to /etc/ntp.conf, I have not yet needed to do this.

CentOS meet Windows active directory

realm join --user=domainadmin@yourdomain.tld yourdomain.tld

Confirm that things went well and CentOS is joined to AD

realm list

If everything went well you should see the output that lists your domain you can now use your favorite terminal emulator to ssh into your centos using your AD login, it should look something like this


If that worked your good to go, if not google is your friend

Before the nextstep you will need to login to one of your domain controllers and create a security group you’ll specify in your samba config members of this AD group will be able to access the files in the file share specified.

Configuring Samba to play well with windows file sharing
time to edit /etc/samba/smb.conf

sudo vi /etc/samba/smb.conf
server string = Samba Server Version %v
security = ads
encrypt passwords = yes
passdb backend = tdbsam

# in my test network I could not get AD authentication for smb shares to work 
# without adding "kerberos method = secrets and keytab"
kerberos method = secrets and keytab

# Add the IPs / network ranges / subnets allowed acces to the server in general.
# this is not a nesessary entry but in general a good idea.
# hosts allow = 127. your local network info

# log files split per-machine:
log file = /var/log/samba/log.%m

# enable the following line to debug:
# log level =3
# maximum size of 50KB per log file, then rotate:
max log size = 50

# Not interested in printers
load printers = no
cups options = raw

# This stops an annoying message from appearing in logs
printcap name = /dev/null

# File Shares under valid users I put a group if you create a group for samba in # active directory and add users to it those users will be able to access smb 
# shares from this server over the network.

[home directory]
comment = My shared folder
path = /home
public = no
writable = yes
guest ok = no
valid users = @"agroup@youraddomain.tld"

Make sure Samba is enabled

systemctl enable smb.service
systemctl start smb.service

Now that samba is setup to share /home you’ll need to edit permissions on /home so users can access their home folders, its also worth noting that in order for those home folders to be created you may need to su to them so the home folder is created or have another way to automatically create their home folders with the correct permissions.

chown root:adgroupyoumade@yourdomain.tld /home

This next part I feel could be tightened up permissions wise, in ubuntu the default permission for home is 755, in CentOS it seems to be 700 by default.

chmod 775 /home

After configuring my CentOS install and setting up samba to share out home folders this way my permissions look like

drwxrwxr-x.   6 root domain  177 Oct 11 14:33 home
drwx------   3   domain   215 Oct 11 17:05

If your concerned about the file permissions on a root directory being messed with its likely you have bigger issues. Since what samba is sharing is only modifiable by the owner this seems ok to me, this is not a server out on the internet it should be behind a firewall and being accessed by your users via a VPN or over the internal LAN. As such we are not hardening this server its in a trusted zone and if we really want to protect access we should do that with something like ZeroTier One.

All thats left to do is test, from your ssh session as root on your CentOS7 server log in as an ad user, this will create a home folder for them.

su someadname@yourdomain.tld

Then try and hit that smb share over the network if all went well you should see a home folder named the full AD username your domain and all, youll be able to see other network home folders as well but not access them, samba wont let you (which is perfect)

Something I’v not yet figured out is how to stop someone who’s authenticated from being able to write anything to the /home folder, I suspect that it has something to do with chmod 775 /home

This guide is my notes with some modifications that worked for me in my environment to get things working for me I found the guide at hexblot very helpful and some of the commands are direct copy and paste from their guide the credit for this post goes to them.

Backing up User Homes on Windows with Powershell

If you want to backup an entire windows system Veeam Endpoint free is your best friend, but if you want to Backup only the users actual data (their home folder) your options are a bit more limited there are lots of programs that do this but sometimes installing yet another program on a computer to do something seemingly small as this just seems redundant, for things like that scripts are awesome.

I wrote a quick script that works for me to do this, Robocopy is a great built in tool for windows that allows you to generate a report of what successfully copied.  The way I’v used it is to make a copy of a users home folder and copy it to an external source that that user has access to.  This script does not require admin rights and runs as the user which allows it to be run as a startup script and is super versatile.

### Azulpine User Home Folder Backup Script
### Changes in this version: target of backups was changed so that the backups now point to the users home directory
### Created by Austin Janey on 6/13/17
### This script is designed to backup the users home folder on logon and send the logs of the backup job to the backup location's log folder.

### Creating the file in the users shared folder so that we have a place to dump the log files
mkdir \\\DATA\U\$env:UserName\Backup
mkdir \\\DATA\U\$env:UserName\Backup\logs

### Editing this determines what is backed up
robocopy "C:\Users\$env:UserName" "\\\DATA\U\$env:UserName\Backup" /E /XA:SH /XD /log:\\\DATA\U\$env:UserName\Backup\logs\newlogfile.txt "Appdata" "My Music" "PrintHood" "MY Pictures" "My Documents" "Recent" "Searches" "Saved Games" "Templates" "SendTo" "NetHood" "Local Settings" "My Videos" "Cookies" "Application Data" "Dropbox" "Start Menu" /XF /R:1 /W:5 *.pst *.vir *.js *.jar *.jse *.lnk *.LOG1 *.exe *.msi *.DAT

### Timestamps the log file produced by robocopy by renaming it with the timestamp
Rename-Item \\\DATA\U\$env:UserName\Backup\logs\newlogfile.txt "$env:UserName-$((get-date).toString('backup_dd-MM-yyyy')).txt"

### moves the log to the logs folder in the U drive
Move-Item \\\DATA\U\$env:UserName\Backup\logs\"$env:UserName-$((get-date).toString('backup_dd-MM-yyyy')).txt" \\\DATA\U\logs

Hashing Files

File hashes are awesome,

A hash is a one way algorithm that when data is put into spits out changed data.

Hash rules…
1. Hashes are one way, you cant feasibly take a hash and convert it back into what it was before it got hashed.
2. A hash will always give you the same length of characters as a result if you hash a 1TB file with MD5 it will spit out 32 characters, if you hash a single letter string it will still give you 32 characters.
3. Hashes are not fool proof, it is possible that 2 files could have the same hash but it is extremely unlikely.
4. Changing any part of a file will result in a completely different file hash, if you change 1 byte of a 1PB file it will give you a completely different set of 32 characters.

So with that being said hashing a file is a great way to…
– figure out if the file you downloaded is identical the file the server on the other end of the connection was trying to send you.
– find out if a file has changed over time (assuming that you had a hash of it).

getting a hash, There are lots of different types of hashes one can get, MD5 is very common for file verification and although one could argue that its not cryptographically strong as it once was forging a file to get an identical hash is still beyond the capabilities of anyone on earth.

Some of the tools hosted here and on other sites have hashes those hashes may be MD5 or SHA1 by confirming the hash after downloading the file you can be reasonably certain that you have a bit for bit copy of the original which can help diagnose issues with installers or verify the security of the linux ISO you downloaded, keep in mind if a site is compromised that the attacker could easily upload his own file and hash in which case the hash would match the new malicious file.  It is best to google the hash string after hashing the file to see if any mirrors or other people have gotten it before you. somebody may have found that a certain hash is malicious.  In some cases verifying the hash of the file over time is useful and if your lucky the wayback machine can help with that.

Powershell: Get-FileHash pathtofile -Algorithm MD5

Screen Shot 2017-04-27 at 10.08.45 AM.png

Bash: MD5 pathtofile

Screen Shot 2017-04-27 at 10.14.36 AM.png

Creating a Windows 10 bootable VHDX

So Windows 10 is kind of a disaster we have known that for some time, This is a guide to make windows 10 a little bit less of a disaster by enabling your windows 10 system to boot directly from a VHDX file doing so has the fallowing advantages. Or in short it lets you make an easily transportable windows 10 image

– you get to have windows 10 in a file, easy to back up or transport to another machine.
– might make imaging systems very easy
– allows you to do seemingly risky things in windows without fear of totally bricking your OS
– would allow you to have different bootable windows files for different people or rather for different uses

– may not be stable, its designed for developers Microsoft recommends not using this in production.
– you wont be able to use bitlocker

So with that out of the way heres how this works

You’ll need a windows 10 iso, mount it and extract the sources file to your desktop

use the windows disk management utilities to create a blank VHDX
Note: microsoft seems to recommend making your VHDX file a static not dynamic vhdx file, I have run into some trouble booting in to dynamic files with errors about there not being enough drive space, consider yourself warned.
– initialize the vhdx file format as gpt and ntfs name it whatever you would like.
– mount your new vhdx file

open a admin powershell to the sources directory

dism /apply-image /imagefile:install.esd /index:1 /ApplyDir:vhdxdriveletter:\

Wait for windows to write all the files to the VHDX…

once thats done all that needs to be done is to add the VHDX to the boot record like so

bcdboot driveletterofmountedvhdx:\Windows

Reboot and your good to go just select your vhdx on boot, worth noting that you can edit the description of the VHDX file using this little gem the visual bccd editor

The cost of doing email wrong

So, true story (from about a year ago)… it was a normal day in the office I grabbed coffee at the Keurig to return to my desk, signed into my notebook and looked at the Helpdesk que, nothing super new my boss walked in I said hi like normal.  About an hour later I get a weird message from our CTO saying that he received a message from a Law Office with a bill attached in a zip file he wanted me to take a look at it.

Now would be a good time to point out everything wrong with this message. First of all it has an attached .zip file,
– nobody should put a bill in an attached zip file unless they are trying to encrypted what they are sending and even if that were the case my boss should have gotten an out of band message with the decryption key (out of band meaning the law office should have contacted him and told him what the password was).
– mentioned above, our CTO was not expecting the message… if they had contacted him it would have been less fishy, he would have expected the bill and known what caused it.
– misspelled or unprofessional looking email, dead give away that whoever wrote it was probably not the law firm in question.

I opened up a CentOS based VM with a desktop user interface from a snapshot, opened my my mail, download the zipped attachment, cut the virtual network the vm was on and unzipped the message. Sure enough in libreoffice the document said in big red letters “in order to display this document you need to enable macros” yep, its malware… surprise?

So lets recap, CTO gets a email from a law firm that looks fishy, sends it to me, i grab the attachment, loaded with malware….

Heres the disturbing part at the beginning, it was sent from that law firms email server. The origin of the message was made blatantly obvious by the original message header. so I proceeded to the next step call the law firm.

(paraphrasing ahead)

“ring ring ring”
me – “hi this is austin janey from company I worked for previously I just received an email from you thats got a nasty malware attachment”
law firm – “were sorry we think our servers been compromised we have an IT contractor looking into it”

took their domain name went to mxtoolbox found out that they didn’t have SPF or DMARC setup at all. This is something I see fairly often, if you don’t have SPF setup then anybody can basically send email as you thats not what was happening here but might have been a contributing factor, and not having any reporting enabled also means when you are being spoofed theres no way for you to know about it.

Heres what is happening here, one way or another the firms exchange server was compromised and the attacker/bot/malware infection was using their server to send mail to all their clients, mail that had malware called ransomware. This is the cost of doing email wrong, a lot of companies think that because they don’t harbor sensitive data they don’t need to take basic security measures this is negligence. The most valuable thing you or your company owns is your name, the second most valuable thing you have are your customers and friends, the third most valuable thing you have to a hacker is your ability to exploit the first 2 things for their own personal gain.

I doubt this law firm is still conducting business, and it sounds like they might have quite a legal battle ahead of them if any of their clients were to receive said email and become subsequently infected.

So how would someone prevent this from happening.
1. make sure you have correctly configured SPF and DMARC records.
2. make sure your exchange server has a strong password set (and 2fa if its supported) and outgoing spam rules so that if you do get compromised the impact is minimized.
3. if possible enable mail attachment scanning and prohibit certain types of files from being emailed all together.
4. user training, teaching users of your mail system to identify what bad email looks like can go a long way, if nothing else teach them to question anything that they receive that calls them to do something they didn’t expect to receive or do.

Network level advertisement blocking

Its been a while thought I would throw this out there.  You hate ads, I hate ads, we don’t trust advertisers with our privacy or increasingly our security because of advertising campaigns.  So lets take a moment and block that network  wide.

So there’s this great project for the raspberry pi called PI-hole,  it lets you run a DNS server on your raspberry pi and block ads on your network, its pretty great whats even better is that you can install it on CentOS7 and use it as a DNS server with a web UI for a small business network,  Lets hammer this out

Your going to need a centos7 vm

Log in and run the below command as root (look at the code first on their website to make sure your comfy with this)

curl -L | bash

Congratz you now have Pi-hole installed.

Now lets lock down those adlists, the Pi-holes defaults I have found are not so great but you can edit those, once again as root run this command to copy the defaults to the adlists.list file

cp /etc/pihole/adlists.default /etc/pihole/adlists.list

Now using your text editor of choice edit the adlists.list file (I used nano)

nano /etc/pihole/adlists.list

Uncomment any lists that you might want and save.

One final note on security….  Pi-hole was not really designed for a business network case and point of this is that you can go to the ip address of the Pi-hole server and add items to the blacklist or whitelist with no username or password required,  That said the web frontend seems to be the only vulnerable part of the Pi-hole install and since were on CentOS7 I don’t have any other reservations on security here.  So lets fix that one hole shall we.

Go setup ZeroTier one if you havent already and create a network.

Install zerotier on your centos vm and join your network making note of your new zerotier IP address.

Open /etc/lighttpd/lighttpd.conf with your favorite text editor and add a line right above server.document-root that reads…

server.bind  = “” (use your zerotier ip)

But with your VMs ZeroTier ip address.

Then restart lighttpd.conf

service lighttpd restart

And you should be good to go, go to your original network IP to confirm the webui is no longer being broadcast over your lan and then go to the zerotier ip (make sure you have zerotier installed on your workstation and are on the same network as the Pi-hole) you can still use the LAN ip of your VM for DNS but the webui  should no longer be displayed or available to the local network.  This means only ZeroTier connected clients that are on your zerotier network can manage the Pi-hole server.  Now just point your router at your Pi-hole if you haven’t already and you should be good to go.