Skip to content

Instantly share code, notes, and snippets.

@srsgores
Created September 13, 2025 03:53
Show Gist options
  • Select an option

  • Save srsgores/295a91a8abc15cd3716877bcd1c4bedb to your computer and use it in GitHub Desktop.

Select an option

Save srsgores/295a91a8abc15cd3716877bcd1c4bedb to your computer and use it in GitHub Desktop.
setup samba
#!/bin/bash
# A script to expose a directory on Arch Linux via Samba for a specific user.
#
# Replace 'sambauser' with your desired username throughout the script.
#
# --- IMPORTANT ---
# Run this script with sudo privileges.
# sudo ./setup_samba_share.sh
set -e # Exit immediately if a command exits with a non-zero status.
# --- Configuration ---
# The username for the Samba share.
# PLEASE CHANGE THIS to your desired username.
SAMBA_USER="samba"
# The directory to be shared.
SHARED_DIR="/Work"
# --- 1. Install Samba ---
echo "--- Installing Samba package ---"
pacman -Syu --noconfirm samba
# --- 2. Create the Directory to Share ---
echo "--- Creating shared directory at $SHARED_DIR ---"
mkdir -p "$SHARED_DIR"
# --- 3. Create the System User ---
# Samba users must first exist as system users on Linux.
# For security, we create a user with no login shell.
echo "--- Creating system user '$SAMBA_USER' with no login shell ---"
useradd -r -d "$SHARED_DIR" -s /usr/bin/nologin "$SAMBA_USER" || echo "User $SAMBA_USER may already exist. Continuing..."
# --- 4. Set Directory Ownership and Permissions ---
echo "--- Setting ownership and permissions for $SHARED_DIR ---"
chown -R "$SAMBA_USER:$SAMBA_USER" "$SHARED_DIR"
chmod -R 775 "$SHARED_DIR"
# --- 5. Create the Samba User ---
# This command adds the system user to Samba's password database and prompts you to set a password.
echo "--- Adding '$SAMBA_USER' to Samba. You will be prompted to create a password. ---"
smbpasswd -a "$SAMBA_USER"
# --- 6. Configure Samba ---
echo "--- Backing up and configuring smb.conf ---"
# Create a backup of the original configuration file
cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
# Append the new share configuration to the smb.conf file
cat <<EOF >> /etc/samba/smb.conf
# --- Shared Directory Configuration ---
[$SHARED_DIR]
comment = Work Directory
path = $SHARED_DIR
read only = no
writable = yes
guest ok = no
valid users = $SAMBA_USER
create mask = 0664
directory mask = 0775
EOF
# --- 7. Verify the Configuration ---
echo "--- Verifying Samba configuration syntax ---"
testparm -s
# --- 8. Enable and Start Samba Services ---
echo "--- Enabling and starting Samba services (smb and nmb) ---"
systemctl enable --now smb
systemctl enable --now nmb
# --- 9. Firewall Configuration (Optional) ---
# If you are running a firewall like ufw, you need to allow Samba traffic.
if command -v ufw &> /dev/null && ufw status | grep -q "Status: active"; then
echo "--- UFW firewall detected. Allowing Samba traffic. ---"
ufw allow 'Samba'
ufw reload
fi
echo "--- Samba share setup is complete! ---"
echo "You should now be able to connect to the '$SHARED_DIR' share on this server"
echo "using the username '$SAMBA_USER' and the password you set."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment