Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Last active May 3, 2024 17:07
Show Gist options
  • Save ThinGuy/1e485d63ecd6af189503011f3f9605e8 to your computer and use it in GitHub Desktop.
Save ThinGuy/1e485d63ecd6af189503011f3f9605e8 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 -regex "${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