Skip to content

Instantly share code, notes, and snippets.

@taavi223
Forked from niieani/no_automount.bash
Last active December 19, 2025 16:29
Show Gist options
  • Select an option

  • Save taavi223/1ea7a87ef235d03cd010114825e7b920 to your computer and use it in GitHub Desktop.

Select an option

Save taavi223/1ea7a87ef235d03cd010114825e7b920 to your computer and use it in GitHub Desktop.
Simple shell utility to configure an external drive to not auto mount on macOS
#!/usr/bin/env bash
# Modified from: https://akrabat.com/prevent-an-external-drive-from-auto-mounting-on-macos/
# Prevent external drives from auto-mounting on macOS
FSTAB=/etc/fstab
# Add a volume as not auto-mounted to the /etc/fstab file
# by its identifier. Also pass in the volume name to add a
# comment on that line so that we can identify it later.
function add_identifier {
ID=$1
VOLUME_NAME=$2
if [ -z "$VOLUME_NAME" ] ; then
echo "add_identifier() takes two parameters: ID and VOLUME_NAME"
exit 2
fi
# get UUID and TYPE from `diskutil info $ID`
UUID=`diskutil info "$ID" | grep "Volume UUID" | awk '{print $NF}'`
TYPE=`diskutil info "$ID" | grep "Type (Bundle)" | awk '{print $NF}'`
# Remove this UUID from fstab file
sudo sed -i '' "/$UUID/d" $FSTAB
# Add this UUID to fstab file
LINE="UUID=$UUID none $TYPE rw,noauto # $VOLUME_NAME"
printf '%s\n' "$LINE" | sudo tee -a "$FSTAB" > /dev/null
printf 'Added "%s" to %s\n' "$LINE" "$FSTAB"
}
# Parse df output to find mounted external volumes under /Volumes/ (not /System/Volumes/)
declare -a IDENTIFIERS=()
declare -a TYPES=()
declare -a SIZES=()
declare -a VOLUMES=()
INDEX=0
while IFS=$'\t' read -r id type size volume; do
IDENTIFIERS+=("$id")
TYPES+=("$type")
SIZES+=("$size")
VOLUMES+=("${volume##*/}")
((INDEX++))
done < <(
df -aYIH | awk -v OFS='\t' '
{ tail = substr($0, index($0, $7)) }
tail ~ "^/Volumes/" { print $1, $2, $3, tail }
'
)
# Check if we found any volumes
if [ $INDEX -eq 0 ]; then
echo "No external volumes found mounted under /Volumes/"
exit 0
fi
# Display selection menu
echo "Select volume to disable auto-mounting:"
echo ""
for i in "${!VOLUMES[@]}"; do
NUM=$((i + 1))
printf "%2d. %-25s %-7s %-7s (%s)\n" "$NUM" "${VOLUMES[$i]}" "${SIZES[$i]}" "${TYPES[$i]}" "${IDENTIFIERS[$i]}"
done
echo ""
read -p "Enter number: " SELECTION
# Validate selection is a number
if ! [[ "$SELECTION" =~ ^[0-9]+$ ]]; then
echo "Invalid selection: must be a number"
exit 1
fi
# Convert to array index (0-based)
IDX=$((SELECTION - 1))
# Validate range
if [ $IDX -lt 0 ] || [ $IDX -ge $INDEX ]; then
echo "Invalid selection: $SELECTION"
exit 1
fi
# Process the selected volume
echo ""
add_identifier "${IDENTIFIERS[$IDX]}" "${VOLUMES[$IDX]}"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment