Skip to content

Instantly share code, notes, and snippets.

@shaneholloman
Forked from ThinGuy/fix-usb2serial.sh
Created February 11, 2024 02:18
Show Gist options
  • Save shaneholloman/d369dc6ddbb7f550dc682198e511a57f to your computer and use it in GitHub Desktop.
Save shaneholloman/d369dc6ddbb7f550dc682198e511a57f to your computer and use it in GitHub Desktop.
Function create UDEV rules for all USB to Serial Devices so that apps such as minicom, screen, gtkterm can access device as normal user
fix-usb2serial() {
printf "\n\e[1mFix USB to Serial device permissions\e[0m\n"
local DEVREGX='.*tty[UA].*' W='s'
declare -ag TTYUSB_DEVS=($(find /sys/class/tty -type l -iregex "${DEVREGX}" -printf "%P\n" 2>&1))
[[ ${#TTYUSB_DEVS[@]} -ge 1 ]] && { local W=""; } || { printf "\nNo USB to Serial devices found using regex \x22${DEVREGX}\x22.\nQuitting\n\n";return 3; }
[[ -n $(id -Gn|grep 2>/dev/null -oE dialout) ]] && { printf "\e[2G - User $USER already a member of \x22dialout\x22 group \e[3m(Good\x21)\e[0m\n"; } || { printf "\e[2G -- Adding User $USER to the \x22dialout\x22 group\n"; sudo usermode -aG dialout $USER; }
printf "\e[2G - Creaing udev rules file: /etc/udev/rules.d/50-ttyUSB.rules\n"
sudo install -o0 -g0 -m0644 /dev/null /etc/udev/rules.d/50-ttyUSB.rules
printf "\e[2G - Adding ${#TTYUSB_DEVS[@]} udev rule${W}:\n"
(printf "%s\n" ${TTYUSB_DEVS[@]}|xargs -I{} printf "KERNEL==\x22{}\x22,\x20MODE=\x270666\x22\n")|sudo tee -a /etc/udev/rules.d/50-ttyUSB.rules|sed -r 's/^/ /g'
printf "\e[2G - Reloading rules and databases for udev daemon...\n"
sudo udevadm control --reload
printf "\e[2G - Listing rule${W} for ${#TTYUSB_DEVS[@]} device${W}\n"
printf "%s\n" ${TTYUSB_DEVS[@]}|xargs -rn1 bash -c 'sudo udevadm test $(udevadm info -q path -n ${0}) 2>&1|awk '"'"'/^'"'"'${0}'"'"'.*rules/'"'"''|sed -r 's/^/ /g'
echo
};export -f fix-usb2serial
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment