Skip to content

Instantly share code, notes, and snippets.

@LJFloor
Last active December 24, 2025 11:44
Show Gist options
  • Select an option

  • Save LJFloor/a931fefc01e2569b1251d22cbbf135fd to your computer and use it in GitHub Desktop.

Select an option

Save LJFloor/a931fefc01e2569b1251d22cbbf135fd to your computer and use it in GitHub Desktop.
Disable write caching to external drives in Linux, so you don't need to safely eject
#!/bin/bash
# Script to enable direct writes for USB drives in Linux Mint
# This uses udisks2 configuration instead of udev rules
echo "=========================================="
echo "USB Direct Write Setup Script (v2)"
echo "=========================================="
echo ""
echo "This script will configure udisks2 to write directly to USB drives"
echo "without caching, similar to modern Windows behavior."
echo ""
echo "Trade-off: Slower copy speeds, but no need to wait after copying."
echo ""
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "This script must be run as root. Please run with sudo."
exit 1
fi
# Confirm with user
read -p "Do you want to continue? (y/n): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Setup cancelled."
exit 0
fi
echo ""
echo "Creating udisks2 configuration..."
# Create udisks2 config directory if it doesn't exist
mkdir -p /etc/udisks2
# Create the configuration file
CONFIG_FILE="/etc/udisks2/mount_options.conf"
cat > "$CONFIG_FILE" << 'EOF'
# Mount options for USB drives - enable sync for direct writes
[defaults]
# Default mount options for removable devices
vfat_defaults=sync,uid=$UID,gid=$GID,shortname=mixed,dmask=0077,utf8=1,showexec,flush
exfat_defaults=sync,uid=$UID,gid=$GID,iocharset=utf8,errors=remount-ro
ntfs_defaults=sync,uid=$UID,gid=$GID,windows_names
ext4_defaults=sync
ext3_defaults=sync
ext2_defaults=sync
EOF
if [ $? -eq 0 ]; then
echo "✓ Configuration file created at $CONFIG_FILE"
else
echo "✗ Failed to create configuration file"
exit 1
fi
echo ""
echo "Restarting udisks2 service..."
# Restart udisks2 to apply changes
systemctl restart udisks2
if [ $? -eq 0 ]; then
echo "✓ udisks2 service restarted successfully"
else
echo "⚠ Warning: Could not restart udisks2 service"
echo " Changes will apply after reboot"
fi
echo ""
echo "=========================================="
echo "Setup Complete!"
echo "=========================================="
echo ""
echo "Next steps:"
echo "1. Unplug any currently connected USB drives"
echo "2. Plug them back in"
echo "3. All USB drives will now use direct writes (sync mode)"
echo ""
echo "To verify, after plugging in a USB drive, run:"
echo " mount | grep /media"
echo ""
echo "You should see 'sync' in the mount options."
echo ""
echo "To UNDO this change, run:"
echo " sudo rm $CONFIG_FILE"
echo " sudo systemctl restart udisks2"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment