Wiki/linux.md

10 KiB

Linux

Application

  • Update MINE database: update-mime-database /usr/share/mime
  • Get MINE Type of specific file: xdg-mime query filetype <file>
  • Get default application for a specific MINE type: xdg-mime query default <minetype>
  • Find list of MIME types in:: /usr/share/applications
  • Music
    • Download video or playlist from YouTube: youtube-dl --extract-audio --audio-format mp3 <VideoOrPlaylistURL>
  • Markdown
    • Grip is required pip install grip

Office

  • Remove certain pages from PDF: pdft <input.pdf> cat <pages or range> output <output.pdf>
    • Single pages: n m
    • Range of pages: n-m
    • Special pages: start end
  • Merge several PDF files: gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -dDetectDuplicateImages -dCompressFonts=true -r150 -sOutputFile=<output.pdf> <input1.pdf input2.pdf>
  • PDF to PNG in high quality
    • pdftoppm <input.pdf> <outputname> -png
    • pdftoppm <input.pdf> <outputname> -png -f {page} -singlefile
    • pdftoppm <input.pdf> <outputname> -png -r 300
  • Rotate PDF page: pdftk <input.pdf> cat <page>-<rotationtype> output <output.pdf>
    • Rotation type: endeast
  • Add Margin to PDF: pdfcrop --margin <lMargin> <tMargin> <rMargin> <bMargin> <input> <output>
    • Margin of 27 corresponds to approximately $1$cm
    • If one argument if provided to --margin it is applied to all 4 sides
  • Compress PDF: gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/<option> -dNOPAUSE -dQUIET -dBATCH -sOutputFile=<output.pdf> <input.pdf>

    • dPDFSETTINGS=/screen lower quality, smaller size. (72 dpi)
    • dPDFSETTINGS=/ebook for better quality, but slightly larger PDFs. (150 dpi)
    • dPDFSETTINGS=/prepress output similar to Acrobat Distiller Prepress Optimized setting (300 dpi)
    • dPDFSETTINGS=/printer selects output similar to the Acrobat Distiller Print Optimized setting (300 dpi)
    • dPDFSETTINGS=/default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file
  • Copy file and show progress: Use rsync as: rsync -ah --progress <src> <dest>
  • Untar Archive
    • .tar: tar -C <destFolder> -xvf <myfile>.tar
    • .tar.gz: tar -C <destFolder> -zxvf <myfile>.tar.gz
    • .tar.xz: tar -C <destFolder> -xf <myfile>.tar.xz
    • Add status
      • Simple method: add option --checkpoint=.100 to add a . for every 100 files extracted
      • More complex: pv <myfile> | tar -xz

Pacman

  • Install package and update: pacman -Syu <package>
  • Remove package and all dependencies: pacman -Rns <package>
  • List all packages: pacman -Qe
  • Get info about package: pacman -Qi <package>
  • Error failed to commit transaction (conflicting files)
    • Check if conflicting file is required by another package: pacman -Qo <fullFilePath>
    • Remove or rename file
  • Clear pacman cache pacman -Sc or pacman -Scc
  • One or more PGP signatures could not be verified: gpg --recv-keys <key>

System

  • Keyboard
    • Change X11 Layout: setxkbmap <layout>
    • Define layouts in: /usr/share/X11/xkb/symbols/<layoutname>
    • Make layout visible to the menu by adding them to: /usr/share/X11/xkb/rules/evdev.xml
    • Change directly in Xorg via /etc/X11/xorg.conf.d/00-keyboard.conf
  • Xresources
    • Reload config file: xrdb ~/.Xresources
    • Read config and merge with currently active: xrdb -merge ~/.Xresources
  • Other
    • Sudo config: sudo visudo
      • When editor not exported correctly: sudo EDITOR=vim visudo
  • Boot Hooks: cat /proc/cmdline
  • System Event Handler
    • Basic systemd event options are in /etc/systemd/logind.conf
    • Get acpid key info acpi_listen
    • Create custom event
      • Create listener
        1. Create event listener file at /etc/acpi/events/<someName>
        2. Insert the following content
          event=<eventName>
          action=/etc/acpi/handlers/<handlerName> <parms>
          
        3. Create handler script at /etc/acpi/handlers/<handlerName>
        4. Restart acpid service
  • Sound:
    • Mic test: arecord -vv -f dat /dev/null
  • BIOS
    • Display detected devices fwupdmgr get-devices
    • Download latest metadata fwupdmgr refresh
    • List available updates fwupdmgr get-updates
    • Install updates fwupdmgr update
  • Mount fat drive with user permissions mount <device> <mountPath> -o dmask=000,fmask=111
  • Show which process hold a mount: sudo lsof | grep <mountPath>
  • Bootable
    • Copy image to drive dd if=<isoPath> bs=4M of=<drive> status=progress

Wifi

nmcli connection add \
    type wifi \
    con-name "eduroam" \
    ifname "wl" \
    ssid "eduroam"
    wifi-sec.key-mgmt "wpa-eap" \
    802-1x.eap "ttls" \
    802-1x.identity "[CUT]@student-net.ethz.ch" \
    802-1x.phase2-auth "mschapv2" \
    802-1x.domain-suffix-match "radius-service.ethz.ch"
  • EPFL
nmcli connection add \
    type wifi \
    con-name "eduroam-epfl" \
    ifname "wl" \
    ssid "eduroam" \
    wifi-sec.key-mgmt "wpa-eap" \
    802-1x.eap "peap" \
    802-1x.identity "jegraf@student-net.ethz.ch" \
    802-1x.phase2-auth "mschapv2" \
    802-1x.domain-suffix-match "radius-service.ethz.ch" \
    802-1x.system-ca-certs "yes" \


nmcli connection add type wifi con-name eth ifname wlp3s0  ssid "eth" -- wifi-sec.key-mgmt wpa-eap 802-1x.eap ttls 802-1x.identity [CUT]@student-net.ethz.ch 802-1x.phase2-auth  mschapv2 802-1x.domain-suffix-match radius-service.ethz.ch


# activate with
nmcli connection up eduroam --ask
nmcli connection up eth --ask
  • Connect to ETH VPN: sudo openconnect https://sslvpn.ethz.ch

Mount luks encrypted lvm partition from live boot

  • lvm2 and cryptsetup are required
  • Probe required modules sudo modprobe dm-crypt
  • Determine the disk path using sudo fdisk -l, lsblk, blkid etc.
  • Decrypt volume and mount it to /dev/mapper/<someName> using sudo cryptsetup luksOpen <diskPath> <someName>
  • Scan for the LVM volumes sudo vgscan
  • Activate volume sudo vgchange -ay <lvmVolume>
  • Find the volume sudo lvs
  • Mount the volume sudo mount /dev/<lvmVolume> /mnt/
  • Mount required volumes and pass the shell to the main system
sudo mount --bind /dev /mnt/dev 
sudo mount --bind /dev/pts /mnt/dev/pts
sudo mount --bind /proc /mnt/proc
sudo mount --bind /sys /mnt/sys
sudo chroot /mnt

Access critical stuff from inside chroot

for i in dev dev/pts sys tmp run proc
do
    mount --bind /$i /mnt/<newChroot>/$i
done

chroot /mnt/<newChroot>

Network not working inside chroot

cp /etc/resolv.conf /mnt/<chroot>/resolv.conf

Forward device to internet

  • Add route on device which matches the ip of the host: ip route add default via <HostIP>
  • Enable forwarding: sysctl net.ipv4.ip_forward=1
  • Add iptables rules:
    • iptables -t nat -A POSTROUTING -o <InternetInterface> -j MASQUERADE
    • iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
    • iptables -A FORWARD -i <DeviceInterface> -o <InternetInterface> -j ACCEPT

Restore Raid of LVM volumes

  • lvm2 is required
  • Required modules: dm-mod, raid1
  • Assemble RAID mdadm --assemble /dev/md0 /dev/<member1> /dev/<member2>
  • Scan for the LVM volumes sudo vgscan
  • Activate volume sudo vgchange -ay <lvmVolume>
  • Find the volume sudo lvs
  • Mount the volume sudo mount /dev/<lvmVolume> /mnt/
  • Add raid config mdadm --examine --scan > /etc/mdadm/mdadm.conf

User and Groups

  • Change primary group
    • sudo usermod -g <primaryGroup> <user>
  • Add group to user
    • sudo usermod -aG <group> <user>
  • Reload user after adding to group
    • newgrp <group>

Block websites using hosts file

  • Enter the websites into /etc/hosts as follows:
0.0.0.0     <website>
::1         <website>
  • Flush DNS cache by restarting nscd and dnsmasq

Networking

  • Show ports being listened on netstat -plntu
  • Show used ports lsof -Pn -i4

Backup

  • Remote block device to local:
    • Backup : ssh -C <user@address> 'dd if=/dev/<device>' | dd of=<some_image>.img
    • Restore: dd if=<some_image>.img | ssh -C <user@address> dd of=/dev/<device>

SSH copy and untar after sending

  • pv <my.tar.gz> | ssh <address> 'tar yxpf - -C <destDirectory>'

Cleanup

  • Find large file: du -hs * | sort -h

Mount partition of disk image created with dd

  • Find partition start: fdisk -u -l <my_img>

  • Calculate start offset: <start> * 512, where 515 is the block size

  • Mount: mount -o loop,offset=<my_offset> <my_img> <my_mountpt>

  • Check hash for all files in folder: sha1sum <somePath>/* | sha1sum

  • Check hash for all files in tree: find <somePath> -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum

  • Check if content of directory has changed: ls -alR --full-time /folder/of/stuff | sha1sum

  • mpv play mono: --audio-channels=stereo

  • Screensaver stuff:

    • Set screensaver to 10mins: xset s 600
    • Disable screensaver: xset s 0
    • Disable powermanagement: xset -dpms
    • Enable powermanagement: xset +dpms
    • Show current settings: xset q
  • Sound on HDMI:

    • Check name of output: pacmd list-cards | grep output\:
    • Set to HDMI: pactl set-card-profile 0 output:hdmi-stereo
    • Set back to internal: pactl set-card-profile 0 output:analog-stereo
  • Connect iPhone

    • Install ifuse and libimobiledevice
    • Connect iPhone, unlock and select trust
    • Run idevicepair pair
    • Run ifuse -o allow_other <mountpoint>
      • The mount point should be owned by the user and not root
  • Get log from dd by running sudo kill -USR1 $(pgrep ^dd$)

Print

  • Server at: http://localhost:631

  • Wrong TERM on ssh (Terminal)

    • Copy terminfo to remote: infocmp | ssh <remote> tic -
  • Display formatting like \n, \t in less: printf "$(< <myfile>)" | less

  • Project dos to linux: `find . -type f -print0 | xargs -0 dos2unix

  • See all files an executable touches at start: strace -e openat <ELF>