# Unix & Linux # ++++ + +++++ 12345678911234567892123456789312345678941234567895123456789612345678971234567898 ## Set date ``` date +%Y%m%d -s "20231005" date +%T -s "16:40:00" ``` ## get date ``` date --utc ``` ## select all directories inside a folder ``` shopt -s globstar chmod g+s /var/www/**/*/ ``` or ``` chmod -R a+X workspace/synergy ``` # add user to a group - `usermod -a -G ` # to see user groups - `id` ## copy without overwriting duplicate files cp -n ./lib /usr/ ## Set default C/C++ com1piler export CC=/usr/bin/clang export CXX=/usr/bin/clang++ export CPP=/usr/bin/clang-cpp-14 export LD=/usr/bin/ld.lld ## get binary file architecture `file ` ## run a program on specific core (index 1) ## (not single threaded but non concurrent) numactl --physcpubind=+1 ./bn_cl ## check attributes on directory ls -ld directory/ ## resolve all links to target readlink -f # sudo compound/piped/multiple commands sudo sh -c "" # find open ports netstat -na | grep -i LISTEN netstat -f inet -na | grep -i LISTEN # linux sudo netstat --listen sudo netstat -lntpd # find process listening in on $PORT lsof -n -iTCP:$PORT | grep LISTEN # generate public and private key pair mkdir ~/.ssh chmod 700 ~/.ssh ssh-keygen -t rsa # to list sha256 of a key ssh-keygen -lf ~/.ssh/id_rsa.pub # sshd: no hostkeys available -- exiting ssh-keygen -A # prin sha256 of a key ssh-keygen -lf .ssh/id_rsa.pub # letsencrypt ssl certs sudo letsencrypt certonly --standalone -d ferryfair.com -d FerryFair.com -d www.ferryfair.com -d WWW.FerryFair.com -d rosy.ferryfair.com -d Rosy.FerryFair.com -d mail.ferryfair.com -d mail.FerryFair.com -d roundcube.ferryfair.com -d RoundCube.FerryFair.com -d fairplay.ferryfair.com -d FairPlay.FerryFair.com -d ferry.ferryfair.com -d Ferry.FerryFair.com -d mysqlclient.ferryfair.com -d squirrelmail.ferryfair.com -d vpn.ferryfair.com -d RPi1B2.ferryfair.com -d RPi3B.ferryfair.com -d RT-N18U.ferryfair.com -d Deepthi-PC.ferryfair.com -d RPi3BP.ferryfair.com -d underconstruction.ferryfair.com -d BPiCM4.ferryfair.com -d Core3566.ferryfair.com certbot renew Necktwi@Core3566 ~/workspace/acme-tiny $ python acme_tiny.py --disable-check --account-key ~/account.key --csr ~/domain.csr --acme-dir ~/workspace/WWW-development/.well-known/acme-challenge/ > signed_chain.crt sudo cp signed_chain.crt /etc/letsencrypt/live/ferryfair.com/signed_chain.crt # CA certificat autority certificates location /etc/ssl/certs/ca-certificates.crt ## ssh-agent ### list keys ssh-add -l/-L ### start agent ssh-agent ### remove all ssh agent keys ssh-add -D ### or just stop ssh agent on macOS killall ssh-agent ### disable on macOS launchctl disable "gui/$(id -u)/com.openssh.ssh-agent" # to check cert.pem expiry date check for "Not After" in the output of openssl x509 -in /etc/letsencrypt/live/ferryfair.com/cert.pem -text ### convert to pem ssh-keygen -f ~/.ssh/id_rsa.pub -e -m PKCS8 > ~/.ssh/id_rsa.pub.pem ### encrypt echo "hello gowtham" | openssl rsautl -encrypt -pubin -inkey ~/.ssh/id_rsa.pub.pem > hello.crypt ### decrypt openssl rsautl -decrypt -inkey ~/.ssh/id_rsa -in hello.crypt # print the private key contents openssl rsa -in ~/.ssh/id_rsa -noout -text # to view the key in hex and ascii cat | base64 -d | xxd MIIEpAIBAAKCAQEA7dDtxLPmGkHusoBr7dortWhypPX9nsV4Z6zeXuTl2bT9EuTT Q9hcuGGmmqlvnqjkhkNaEwzOiR2ExJKoZpT8uu44qrgGQ37agxZFfK8YKGnQlGIu xZFqd5e+5RWcP1e0kotsFVKDOUMP1IQFpSJD7ElSS+Y44dDuMne2U44emGGHZ/Lc ... vmT+J+xze0D6MwXGJaumH+5LT7HM3QjLgwBLeamX1rrQ/HeFX9cm3w== C-d 00000000: 3082 04a4 0201 0002 8201 0100 edd0 edc4 0............... 00000010: b3e6 1a41 eeb2 806b edda 2bb5 6872 a4f5 ...A...k..+.hr.. 00000020: fd9e c578 67ac de5e e4e5 d9b4 fd12 e4d3 ...xg..^........ ... 00000490: ee4b 4fb1 ccdd 08cb 8300 4b79 a997 d6ba .KO.......Ky.... 000004a0: d0fc 7785 5fd7 26df # gpg - gnupg gpg --list-keys gpt --edit-key > list > expire > save gpg --list-secret-keys --keyid-format=long gpg --export-secret-keys > my.key scp my.key remote:~/ # on remote gpg --import my.key gpg --list-secret #print key in ascii gpt --armor --export ## vi commands :help C-] #open help under cursor :set nonu :set nu / n # next match N # previous match :s/foo/bar/gc # replace on current line :%s/foo/bar/g # replace all in current line G u undo C-r redo :tabnew :tabe :tabp :tabn vi -b binary_file :%!xxd edit :%!xxd -r :wq :set paste # no auto-indent when pasting text :set nopaste # turn off paste mode after pasting data :e #reload the file # set vi tab size set tabstop=3 softtabstop=0 expandtab shiftwidth=3 smarttab # vimdiff ]c - next difference [c - previous difference do - diff obtain dp - diff put zo - open folded text zc - close folded text :diffupdate - re-scan the files for differences :diffg RE " get from REMOTE :diffg BA " get from BASE :diffg LO " get from LOCAL v -select text C-v -rectangular select d -cut y -copy P -paste ## diff two directories diff -qr dir1 dir2 ## convert all files in dir for entry in ./*.mkv; do ffmpeg -i "$entry" -codec copy "${entry%.*}.mp4"; rm $entry; done ## match numerics shopt -s extglob ls Arrow.S04E+([0-9]).mp4 #redirect stderr and stdout cmd >>file.txt 2>&1 ## mount samba shares `sudo mount -t cifs -o username=Necktwi,password=tornshoes,vers=1.0 \ //192.168.0.1/Lake/ /mnt/Lake/` ## umount all samba share umount -a -t cifs -l ## mount nfs `sudo mount -t nfs 192.168.2.4:export/IceCap/Gowtham /tmp/IceCap` ### fstab `192.168.100.3:/Ganges /mnt/Ganges nfs noatime 0 0` ## mount exfat sudo mount -o "defaults,uid=1000,gid=1000,dmask=0002,fmask=0003" /dev/sda2 \ /mnt/ssd2/ ## kill unresponsive ssh sessions ~. ## Tunneling ### to map remote port 3000 to local port 8080 ssh admin@server1.example.com -L 8080:server1.example.com:3000 L4070 localhost:4000 L7011 localhost:1111 L7022 localhost:22 L7088 localhost:8888 #### on macOS `sudo ssh -L 192.168.0.101:80:192.168.2.4:80 -N gowtham@127.0.0.1` `sudo autossh -M 20000 -f -L 192.168.0.101:80:192.168.2.4:80 -N gowtham@127.0.0.1` ### to map local port 22 to remote port 3022 on remote server #set `GatewayPorts yes` in `/etc/ssh/sshd_config` of remote server #-N doesn't open a shell #-f runs in background `ssh -f -N Necktwi@rpi3b.ferryfair.com -R 3022:localhost:22` #to auto reconnect `autossh -M 20000 -f -N rpi3b.ferryfair.com -R 3022:localhost:22 -C` ### to debug ssh; find config location ssh -vvvv ### ssh x forward ssh -Y user@ip ## Mount remote folder using ssh sudo sshfs -o auto_cache,reconnect,no_readahead,allow_other,IdentityFile=/home/gowtham/.ssh/id_rsa -o uid=999,gid=999, -o defer_permissions,noappledouble,nolocalcaches,local,volname=BBB -o debug,sshfs_debug,loglevel=debug gowtham@ubuntuvm.ferryfair.com:/home/gowtham/bbb_mount /home/ubuntu/Projects ### to allow root to access sshfs mount sshfs -o allow_other 10.138.138.16:/mnt/ssd2 /home/amd/MI200-EthanolX/ ## append local file to remote file or send text over ssh cat .ssh/id_rsa.pub | ssh 10.138.138.16 -T "cat >> ~/.ssh/authorized_keys" ## Mount a folder mount --rbind /dev /mnt/dev mount --bind ~/ /export ## Mount iso ## Unmount remote folder sudo fusermount -u /home/ubuntu/Projects # Lazy unmount. Detach the filesystem from the filesystem # hierarchy now, and cleanup all references to the filesystem as soon as # it is not busy anymore. (Requires kernel 2.4.11 or later.) sudo umount -l /mnt/backup ### if target is busy while umount `lsof | grep /path/to/mountpoint` - or `fuser -mv /path/to/mountpoint` ## add a service or daemon at startup update-rc.d defaults ## set run update-rc.d disable [ S|2|3|4|5 ] update-rc.d enable [ S|2|3|4|5 ] update-rc.d -f remove # Remove a service sudo update-rc.d -f apache2 remove ## Searching for a file ``` find / -iname “*.conf” find / -regex "+.\.[ch]$' find /bin -lname busybox -delete find / -type d \( -path /etc -o -path /data -o -path /usr -o -path /var \ -o -path /tmp \) -prune -name java find . -not -path "*.cache*" -name velodyne128_detection_conf.pb.txt # to exclude symlinks find /root/ -type f -name magisk_patched-27000_xMP6j.img ``` ## Searching for a text in files #-I to ignore binaries #-R follows symlinks, -r won't #-i to ignore case #-w whole word #-n print line number #--exclude-dir=dir #--include="*.txt" to search only txt files grep -rnw '/path/to/somewhere/' -e "pattern" ## extracting pattern rocprof --list-derived | sed -nE 's/^.+: (.+) :.+$/\1/p' ### multiline sed example | extract ip ipconfig | sed -n '/Ethernet 9:/, /Ethernet/p' | sed -nE 's/^.+IPv4\sAddress.+:\s(.+)$/\1/p' or ip addr | sed -n '/eno1/, /inet6/p' | sed -nE 's/.*inet (.+)\/.+$/\1/p' ## Replace string in files perl -i -pe 's/ffjson.h/FFJSON.h/g' ./* or grep -rl --exclude-dir={*.git,dir2} /path/to/folder | xargs sed -i s^^^g ## Replacing file names rename 's/ACDC/AC-DC/' *.xxx ## Remove a folder from path PATH=$(echo "$PATH" | sed -e 's/:\/home\/wrong\/dir$//') or PATH=${PATH//$SEARCH_STRING/$REPLACE_STRING} ## copy with xargs ``` find modules/prediction/ -iname *.pt | xargs cp -t /mnt/ssd2/pts/ #or find modules/prediction/ -iname *.pt | xargs -Issss cp ssss /mnt/ssd2/pts/ ``` ## Graphics driver lshw -c video modinfo -F filename `lshw -c video | awk '/configuration: driver/{print $2}' | cut -d= -f2` ## Get the info on loaded drivers modinfo ## load moudle modprobe ## force load module insmod ## list all modules lsmod ## turn off display from command line sleep 1 && xset -display :0.0 dpms force off ## Get Resolution fbset -s ## get edid ```bash sudo apt install read-edid sudo find /sys | grep -i edid cat /sys/devices/platform/soc/soc:gpu/drm/card0/card0-HDMI-A-1/edid | parse-edid ``` ## Get screen id or multi monitor/display coordinates - `xrandr` - to control display from remote terminal `export DISPLAY=:0.0` ## Set Resolution ```bash xrandr --output Virtual1 --mode 1280x800 cvt 1280 800 cvt 1440 810 : <<'OUTPUT' 1280x800 59.81 Hz (CVT 1.02MA) hsync: 49.70 kHz; pclk: 83.50 MHz #Modeline "1280x800_60.00" 83.50 1280 1352 1480 1680 800 803 809 831 -hsync +vsync sudo xrandr --newmode "1280x800_60.00" 83.50 1280 1352 1480 1680 800 803 809 831 -hsync +vsync xrandr | grep -e " connected [^(]" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/" OUTPUT ``` ## addmode ``` sudo xrandr --addmode eDP-1 1280x800_60.00 vi ~/.xprofile #!/bin/sh xrandr --newmode "1280x800_60.00" 83.50 1280 1352 1480 1680 800 803 809 831 -hsync +vsync sudo xrandr --addmode eDP-1 1280x800_60.00 xrandr --output DSI-1 --scale 0.75x0.75 ``` ## startx on a virtual console if no xinit for the user if [ "$(rc-status -r)" = "x" ] && [ -z $DISPLAY ] && [ $(tty | grep "/dev/tty[1-6]") ] && ! [ $(pgrep -U $UID xinit) ]; then startx; fi ## vnc - `sudo x11vnc -display :0 -auth /var/run/lxdm/lxdm-\:0.auth -forever -noxdamage -nopw` ## switch to dGPU glxinfo | grep "OpenGL renderer" xrandr --listproviders xrandr --setprovideroffloadsink DRI_PRIME=1 glxinfo | grep "OpenGL renderer" __GLX_VENDOR_LIBRARY_NAME=intel __VK_LAYER_NV_optimus=non_NVIDIA_only VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/intel_icd.x86_64.json __NV_PRIME_RENDER_OFFLOAD=0 DRI_PRIME=0 ./simulator __GLX_VENDOR_LIBRARY_NAME=nvidia __VK_LAYER_NV_optimus=NVIDIA_only VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/nvidia_icd.json __NV_PRIME_RENDER_OFFLOAD=1 DRI_PRIME=1 ./simulator VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/intel_icd.x86_64.json vkcube ## set console mode or resolution vi /etc/default/grub GRUB_GFXMODE=1366x768 GRUB_GFXPAYLOAD_LINUX=keep #if text mangles in nano or emacs then do terminal reset ## record screen ``` ffmpeg -video_size 1500x2160 -framerate 25 -f x11grab \ -i :0.0+, screenreocrd1.mp4 ``` ## amd gpu ``` echo "on" | sudo tee /sys/class/drm/card2/device/power/control cat /sys/class/drm/card2/device/power/runtime_status echo "high" | sudo tee /sys/class/drm/card2/device/power_dpm_force_performance_level ``` ## nvidia gpu ``` nvidia-smi --query-gpu=name,power.limit,power.draw --format=csv sudo nvidia-smi -pl 125 ``` ## insert service in run-command tree insserv /etc/init.d/myservice insserv -r /etc/init.d/myservice sudo service openvpn@client start # live log file tail -f /var/log/ferryfair.log # live log folder inotifywait -m /proc -e create -e moved_to | while read dir action file; do echo "The file '$file' appeared in directory '$dir' via '$action'" # do something with the file done # mount all fstab sudo mount -a # remount incase any error sudo mount -o remount, #source #destination #fs-type #mount-options(auto/noauto,rw/ro,exec/noexec, user/nouser,ro/rw,sync/async # fstab for sshfs sshfs#gowtham@192.168.2.23:/media/Tin /media/Tin fuse defaults,allow_other,IdentityFile=/home/gowtham/.ssh/id_rsa 0 0 # mount udf sudo mount -t udf /dev/sda1 /media/BitCoin # or sudo mount -t iso9660 /dev/sr0 /cdrom # check disk for badblocks sudo badblocks -sv /dev/sda | tee /media/Aren/bad-blocks-result # repair disk with badblocks sudo fsck -t udf -l /media/Aren/bad-blocks-result /dev/sda1 # repair root partition sudo touch /forcefsck btrfs check --repair /dev/sdb3 #/etc/network/interfaces # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback auto eth0 # triggers upon hardware interaction allow-hotplug eth0 iface eth0 inet static address 10.0.0.41 netmask 255.255.255.0 network 10.0.0.0 broadcast 10.0.0.255 gateway 10.0.0.1 dns-nameservers 10.0.0.1 8.8.8.8 dns-domain acme.com dns-search acme.com auto eth1 allow-hotplug eth1 iface eth1 inet dhcp allow-hotplug wlan0 iface wlan0 inet manual wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf iface default inet dhcp auto eth1 allow-hotplug eth1 iface eth1 inet dhcp post-up addr add 192.0.2.200 dev eth0 # /etc/wpa_supplicant.conf country=IN ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="Kudupudi" psk="16181631729" priority=5 id_str="Kudupudi" } ## ping/broadcast all ips in subnet/lan nmap -n -sP 192.168.13.0/24 ## to find route or hops to an IP - `mtr` ## find MAC or HW address of systems on LAN arp #if it doesn't list all, ping the missing ip and rerun ## DHCP sudo nmap --script broadcast-dhcp-discover ## network echo 1 > /proc/sys/net/ipv4/ip_forward ## forward all traffic another computer ./sshuttle -r MI200-EthanolX 0.0.0.0/0 -vv ## Internet speed test - `curl -s \ https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | \ python3 -` # change sound card boot priority sudo nano /etc/modprobe.d/alsa-base.conf # comment options snd-usb-audio index=-2 sudo vi /lib/modprobe.d/aliases.conf # comment options snd-usb-audio index=-2 sudo vi /usr/share/alsa/alsa.conf # change all card 0 to card 1 # play sound from multiple speakers or devices pactl load-module module-combine-sink ## stream sound - sink: `ffplay -nodisp -fflags nobuffer -flags low_delay -probesize 32 -analyzeduration 0 -f s16le -ar 44100 -ac 2 udp://0.0.0.0:5000`or`nc -u -l -p 5000 | pacat --format=s16le --channels=2 --rate=44100 --latency-msec=10` - pacat crackles - source: `parec -d alsa_output.pci-0000_00_1f.3.hdmi-stereo.monitor --format=s16le --channels=2 --rate=44100 --latency-msec=10 | nc -u 192.168.2.3 5000` # view partition table fdisk -l ## to fix partition order in fdisk ``` x f ``` ## change UUID - umount /dev/sdb1 - tune2fs -U random /dev/sdb1 ## find the process keeping device busy from unmount `fuser -mv /path/to/mountpoint` # check for fuse grep -i fuse /lib/modules/$(uname -r)/modules.builtin # check fuse version fusermount -V # check if a filesytem is supported e.g. udf cat /proc/filesystems ## track progress of dd with pipe viewer; ``` # for linux bs=4M, skip n seek to dd from any point dd if=some.iso bs=4m skip=500 conv=noerror,sync | pv | \ dd of=/dev/sdb bs=4m seek=500 conv=noerror,sync or Ctrl + t pkill -INFO -x dd or dd if=/media/necktwi/Ventoy/lubuntu-24.04.2-desktop-amd64.iso bs=4M conv=noerror,sync of=/dev/sdb status=progress ``` ## quickly create a huge garbage file `fallocate -l 10G gentoo_root.img` # save history to a file history -w source1.sh # clear history history -c # load history history -r source1.sh # in the ~/.bashrc put # update history for every command # Avoid duplicates HISTCONTROL=ignoredups:erasedups # When the shell exits, append to the history file instead of overwriting it shopt -s histappend # After each command, append to the history file and reread it PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r" # for setting history length see HISTSIZE and HISTFILESIZE in bash(1) HISTSIZE=100000 HISTFILESIZE=100000 # startup daemon chkconfig --add /etc/init.d/zoneedit # mail echo "message" | mail -s "Subject" -t to@address.com -A ~/Attachment1 -A ~/Attachment2 # flush mail queue or outbox sendmail -bp # login as different user su - another_user # run a command as another user sudo runuser -u transmission -- touch /mnt/IceCap/Gowtham/movies/test # Restart networking sudo ifconfig eth0 down && sudo ifconfig eth0 up sudo ip link set eth0 down && sudo ip link set eth0 up sudo service network-manager restart ## assign static ip ``` ifconfig eth0 192.168.2.2/24 rotue add default gw 192.168.2.1 ip addr change 192.168.2.1/24 dev eth0 ip route add default via 192.168.2.1 dev eth0 ip route ``` # Set prompt echo "PS1='${debian_chroot:+($debian_chroot)}\[\033[00;34m\]\h\[\033[00m\]:\[\033[00;34m\]\W\[\033[00m\] ${USER}\[\e[00;33m\]\$\[\033[00m\] '" >> /etc/bash.bashrc PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\h:\W ${USER}\a\]$PS1" PS1='${debian_chroot:+($debian_chroot)}\h:\W ${USER}\$ ' PS1='\[\033[00;36m\]\h\[\033[00m\]:\[\033[00;36m\]\W\[\033[00m\] ${USER}\[\e[00;33m\]\$\[\033[00m\] ' PS1='\[\033[00;31m\]\h\[\033[00m\]:\[\033[00;36m\]\W \[\033[00m\]\# ' PS1='\[\033[00;31m\]${HN}\[\033[38;5;256m\]:\[\033[38;5;27m\]\\ W \[\033[38;5;256m\]#\[\033[00m\]\n' PS1='\[\033[38;5;77m\]${HN}\[\\ 033[38;5;256m\]:\[\033[38;5;27m\]\W \[\033[38;5;11m\]\\[\033[38;5;27m\]\u\[\033[00m\]$\[\033[00m\]\n' # tar extract zipped archived file tar -xzvf file.tar.gz # tar create zipped archived file tar -czvf filename.tar.gz folder # tar extract tbz2 tar -xjvf file.tbz2 gunzip -k file.gz tar -xvf file.tar.xz tar -xvf file.tar.bz2 bzip2 -dk filename.bz2 unxz file.xz # tar -C /target/directory # tar --strip-components=1 to righ away extract contents of sub folders # extract specific file tar -xf file.tar.xz "/file/to/extract.png" # extract zip file unzip file.zip -d destination_folder # zip folder `zip -r LTJoiningDocs.zip LTJoiningDocs/` # extract .7z `7zz -x compressed.7z` ## fonts /etc/fonts/fonts.conf /usr/local/share/fonts cp *.ttf /usr/local/share/fonts/ fc-list #list fonts ## font kerning - allows to mix different syllables in to a character in the featured languages # wildcard or glob hidden files GLOBIGNORE=".:.." ls -la * shopt -s dotglob # ls colors LS_COLORS="$LS_COLORS:rs=0:di=0;36:ln=0;37:mh=00:pi=40;33:so=0;35:do=0;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=0;32:*.tar=0;31:*.tgz=0;31:*.arc=0;31:*.arj=0;31:*.taz=0;31:*.lha=0;31:*.lz4=0;31:*.lzh=0;31:*.lzma=0;31:*.tlz=0;31:*.txz=0;31:*.tzo=0;31:*.t7z=0;31:*.zip=0;31:*.z=0;31:*.Z=0;31:*.dz=0;31:*.gz=0;31:*.lrz=0;31:*.lz=0;31:*.lzo=0;31:*.xz=0;31:*.zst=0;31:*.tzst=0;31:*.bz2=0;31:*.bz=0;31:*.tbz=0;31:*.tbz2=0;31:*.tz=0;31:*.deb=0;31:*.rpm=0;31:*.jar=0;31:*.war=0;31:*.ear=0;31:*.sar=0;31:*.rar=0;31:*.alz=0;31:*.ace=0;31:*.zoo=0;31:*.cpio=0;31:*.7z=0;31:*.rz=0;31:*.cab=0;31:*.wim=0;31:*.swm=0;31:*.dwm=0;31:*.esd=0;31:*.jpg=0;35:*.jpeg=0;35:*.mjpg=0;35:*.mjpeg=0;35:*.gif=0;35:*.bmp=0;35:*.pbm=0;35:*.pgm=0;35:*.ppm=0;35:*.tga=0;35:*.xbm=0;35:*.xpm=0;35:*.tif=0;35:*.tiff=0;35:*.png=0;35:*.svg=0;35:*.svgz=0;35:*.mng=0;35:*.pcx=0;35:*.mov=0;35:*.mpg=0;35:*.mpeg=0;35:*.m2v=0;35:*.mkv=0;35:*.webm=0;35:*.ogm=0;35:*.mp4=0;35:*.m4v=0;35:*.mp4v=0;35:*.vob=0;35:*.qt=0;35:*.nuv=0;35:*.wmv=0;35:*.asf=0;35:*.rm=0;35:*.rmvb=0;35:*.flc=0;35:*.avi=0;35:*.fli=0;35:*.flv=0;35:*.gl=0;35:*.dl=0;35:*.xcf=0;35:*.xwd=0;35:*.yuv=0;35:*.cgm=0;35:*.emf=0;35:*.ogv=0;35:*.ogx=0;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:" ## sync copy excluding subfolders. #use -z to delete files in destination that are not in source. #use -R to create parent directories #-a is archive mode which does recursion and preserves almost everything #-v verbose, --stats rsync -avuh --progress --info=progress2 -e 'ssh -p $port' -R /mnt/IceCap/ 192.168.1.101:/mnt/Aren --exclude=workspace/Linux.git --exclude=.git --exclude=var --exclude=tmp --exclude=.cache --exclude=.bash_history --exclude=backups ## Compile Linux kernel ``` # arbitrary precision numeric processing language. Syntax is similar to C pacman -S bc cd /usr/src/linux make O=~/.build/kernel distclean lspci -k //give currently loaded modules make O=~/.build/kernel list-defconfigs #./arch/*/configs/ make kernelversion make help make O=~/.build/kernel menuconfig or emerge --config sys-kernel/gentoo-kernel # create defconfig make LLVM=1 LLVM_IAS=1 ARCH=arm64 HOSTCC=clang CC=aarch64-gentoo-linux-musl-clang savedefconfig mv defconfig arch/arm64/configs/luckfox_core3566_linux_defconfig # if upgrading kernel version place old .config in new kernel tree and do make olddefconfig # generates .config based on currently loaded modules make localmodconfig zcat /proc/config.gz > .config #if not there do `modprobe configs` scripts/config --disable SYSTEM_TRUSTED_KEYS scripts/config --disable SYSTEM_REVOCATION_KEYS #for vmware fusion CONFIG_FUSION_SPI=Y #if clang, set LLVM=1 LLVM_IAS=1 #if arm64, export ARCH=arm64 SUBARCH=arm64 CC=clang LD=ld.lld ARCH=arm CROSS_COMPILE=armv6zk-unknown-linux-musleabihf- make -j2 make INSTALL_MOD_PATH="/" modules_install make INSTALL_PATH="/boot" install cp /boot/vmlinuz-* /boot/efi/boot/bootx64.efi make headers_install emerge --ask sys-kernel/genkernel genkernel --lvm --mdadm --install initramfs mount -o remount,rw /sys/firmware/efi/efivars ``` ## debugging a kernel - kdb - kgdb - more robust than kdb - debugfs: `sudo mount -t debugfs` ## Ways to communicate between the userspace and kernel space - IOCTL : Input and Output Control used for operations does not have a system call by default. steps to implement ioctl command: 1. Create IOCTL command in the driver - define the IOCTL command: ``` #define "ioctlName" __IOX("magicNumber", "commandNumber", "argType") ``` - IOX: IO, IOW, IOR, IOWR - magicNumber: differentiates our set of ioctl calls from the others; device major number can be used here. - commandNumber: differentiates ioctls among our set. - argType: type of data, e.g. int32_t* 2. Write IOCTL function in the driver ``` int ioctl (struct inode* inode, struct file* file, uint cmd, ulong arg) ``` - inode: inode number of the file being worked on - file: file pointer that was passed by the application - cmd: command that was called from userspace - arg: arguments passed from the userspace - `fops` pointer `unlocked_ioctl` shoult be pointed to `etx_ioctl` ``` static long etx_ioctl(struct file* file, uint cmd, ulong arg) { switch(cmd) { case WR_VALUE: if (copy_from_user(&value ,(int32_t*) arg, sizeof(value))) { pr_err("Data Write : Err!\n"); } pr_info("Value = %d\n", value); break; case RD_VALUE: if (copy_to_user((int32_t*) arg, &value, sizeof(value))) { pr_err("Data Read : Err!\n"); } break; default: pr_info("Default\n"); break; } return 0; } static struct file_operations fops = { .owner = THIS_MODULE, .read = etx_read, .write = etx_write, .open = etx_open, .unlocked_ioctl = etx_ioctl, .release = etx_release, }; ``` 3. Create IOCTL command in a userspace application ``` #define WR_VALUE _IOW('a', 'a', int32_t*) #define RD_VALUE _IOR('a', 'b', int32_t*) ``` 4. Use IOCTL system call in userspace ``` #include ioctl(fd, WR_VALUE, (int32_t*)&number); ioctl(fd, RD_VALUE, (int32_t*)&value); ``` - https://github.com/Embetronicx/Tutorials/tree/master/Linux/\ Device_Driver/IOCTL - procfs - sysfs - configfs - debugfs - sysctl - udp sockets - netlink sockets ## drivers - dt: device tree or previosly called of(OpenFirmware) ### PCI: Peripheral Component Interconnect - pci number: 00:02.01: bus:device.function - rootComplex: device with only downStream port. 00.00.00 - bridge: device with both upStream and downStream port. connects 2 buses - endPoint: device with only upStream port - address spaces of pci device: configuration space, memoryOrIo space. - ECAM(EnhancedConfigurationAccessMechanism)Address mapping: - |Bus(8bits)|Device(5)|Function(3)|Offset(12)| - serial port data is sent over Io space. frames of gpu stored in memory space - BAR: Base Address Registers, link between memoryOrIo and configuration space. It says: Whether its a memory or io space(bit0) - bits1,2: 00: 32bit 01: 64bit AddressDecoding - how much space is available on the bar(write1's clear 4lsbits of answer, negate, +1) - map virtual memory address space to bar PCI BAR Bits +-------+----------------+---------------------------+ | Bits | Description | Values | +=======+================+===========================+ | For all PCI BARs | +-------+----------------+---------------------------+ | 0 | Region Type | 0 = Memory | | | | 1 = I/O | +-------+----------------+---------------------------+ | For Memory BARs | +-------+----------------+---------------------------+ | 2-1 | Locatable | 0 = any 32-bit | | | | 1 = < 1 MB | | | | 2 = any 64-bit | +-------+----------------+---------------------------+ | 3 | Prefetchable | 0 = no | | | | 1 = yes | +-------+----------------+---------------------------+ | 31-4 | Base Address | naturally 16-byte aligned | +-------+----------------+---------------------------+ | For I/O BARs | +-------+----------------+---------------------------+ | 1 | Reserved | | +-------+----------------+---------------------------+ | 31-2 | Base Address | naturally 4-byte aligned | +-------+----------------+---------------------------+ - to compute available memory or io on pci device, write all 1's to BAR and read back from BAR, the read value contains #### pci bus enumeration - bus connected directly to cpu is always bus 0 - scans bus for devices - `lspci -tv`: tree view ``` -[0000:00]-+-00.0 Intel Corporation Celeron N3350/Pentium N4200/Atom E3900 Series Host Bridge +-13.0-[01]-- +-13.2-[02]----00.0 Realtek Semiconductor Co., Ltd. RTL8111/8168/8211/8411 PCI Express Gigabit Ethernet Controller $ lspci 02:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8211/8411 PCI Express Gigabit Ethernet Controller (rev 15) ``` - device 13 on bus 00 has 2 buses as functions - 13.2 = PCI device 00:13.2 (a bridge/port on the host bridge) - [02] = PCI bus 2 (created by that bridge) - ---00.0 = Device 00:0 on bus 2 (the Realtek NIC) - so the NIC pci addres is 02.00.0 because its on 2nd bus(among all the buses in the system) .00 device and .0 function. ```bash $ lspci 00:02.0 VGA compatible controller: Intel Corporation HD Graphics 500 (rev 0b) $ ./pciheader 00 02 0 | Byte 0 | Byte 1 | Byte 2 | Byte 3 | |-----------------------------------------------------------| | Vendor ID | Device ID | | 0x8086 | 0x5A85 | 0x00 |-----------------------------------------------------------| | Command | Status | | 0x0407 | 0x0010 | 0x04 |-----------------------------------------------------------| | Revision ID | Class Code | | 0x0B | 0x000300 | 0x08 |-----------------------------------------------------------| | Cache Line S | Lat. Timer | Header Type | BIST | | 0x10 | 0x00 | 0x00 | 0x00 | 0x0c |-----------------------------------------------------------| | BAR 0 | | 0x90000004 | 0x10 |-----------------------------------------------------------| | BAR 1 | | 0x00000000 | 0x14 |-----------------------------------------------------------| | BAR 2 | | 0x8000000C | 0x18 |-----------------------------------------------------------| | BAR 3 | | 0x00000000 | 0x1c |-----------------------------------------------------------| | BAR 4 | | 0x00002001 | 0x20 |-----------------------------------------------------------| | BAR 5 | | 0x00000000 | 0x24 |-----------------------------------------------------------| | Cardbus CIS Pointer | | 0x00000000 | 0x28 |-----------------------------------------------------------| | Subsystem Vendor ID | Subsystem ID | | 0x1025 | 0x110C | 0x2c |-----------------------------------------------------------| | Expansion ROM Address | | 0x00000000 | 0x30 |-----------------------------------------------------------| | Cap. Pointer | Reserved | | 0x40 | 0x000000 | 0x34 |-----------------------------------------------------------| | Reserved | | 0x00000000 | 0x38 |-----------------------------------------------------------| | IRQ | IRQ Pin | Min Gnt. | Max Lat. | | 0xFF | 0x01 | 0x00 | 0x00 | 0x3c |-----------------------------------------------------------| ``` - command: register with which host system can control pcie endpoint ### GPU #### DRM - provides MemoryManagement, InterruptHandling, DMA and uniform interface to applications. - it features TTM memory manager, output configuration and mode setting, the new vblank internals ##### i915 - i915, drm_display_helper, drm_buddy, i2c_algo_bit, video, cec ### Thermal ## add --removable for virtual box grub-install --target=x86_64-efi --efi-directory=/boot vi /etc/default/grub # you can also append custom menu entry in /etc/grub.d/40_custom # you can copy existing menu entry from /boot/grub/grub.cfg and rename grub-mkconfig -o /boot/grub/grub.cfg vi /boot/grub/grub.cfg ... menuentry ... { ... linux /vmlinuz-4.19.66-gentoo root=/dev/sdb5 ro softlevel=vbox } emerge @module-rebuild ## UEFI as bootloader ``` emerge -av sys-boot/efibootmgr efibootmgr --create --disk /dev/sdc --part 1 --label "Grub" --loader "\EFI\grub\grubx64.efi" -u "root=PARTUUID=b3ff8644-a7e4-ed42-b91a-3e5e5a560e11 cgroup_memory=1 cgroup_enable=cpuset cgroup_devices=1 cgroup_blkio=1 softlevel=xwin dobtrfs intel_iommu=on iommu=pt fbcon=rotate:3 module_blacklist=nvidia,nvidiafb vfio-pci.ids=10de:1f08,10de:10f9,10de:1ada,10de:1adb,c0a9:2263 default_hugepagesz=1G hugepagesz=1G hugepages=16 isolcpus=4-5 nohz_full=3-5 rcu_nocbs=3-5" efibootmgr -c -d /dev/sda -p 1 -L "Gentoo" -l '\efi\boot\bootx64.efi' -u 'root=/dev/sda3 initrd=\efi\boot\initramfs.img quiet' # delete and modify efibootmgr -b 0002 -B ## for amdgpu rx7800 sudo efibootmgr -c -d /dev/sda3 -p 1 -L "Godavari" -l '\efi\Gentoo\vmlinuz-6.6.51-gentoo-dist.efi' -u 'root=/dev/sda3 amdgpu.dpm=1 softlevel=bsp initrd=\efi\Gentoo\initramfs-6.6.51-gentoo-dist.img amdgpu.ppfeaturemask=0x0007bfff' ## to reboot to UEFI sudo bootctl reboot-to-firmware true sudo ``` ## autologin user /etc/inittab ``` c1:12345:respawn:/sbin/agetty --noclear --autologin Necktwi 38400 tty1 linux ``` ## add nologin system user useradd -rM puddle usermod -L puddle chage -M 1 doctor; # doctor's password expire in 1 day chage -M ## rename user ``` exec sudo -i killall -u [oldname] id [oldname] usermod -l [newname] [oldname] groupmod -n [newname] [oldname] usermod -d /home/[newname] -m [newname] usermod -c "[full name (new)]" [newname] id [newname] ``` ## acl getfacl setfacl -m u:username:rwx /testfiles setfacl -m g:groupname:rwx testfolder/ #append -d to set default acl ## find ssd serial numaber udevadm info --query=all --name=/dev/sda | grep ID_SERIAL ## change default shell to bash - `chsh /bin/bash` relogin ## ~/.bashrc executed at the start of interactive non-login shell # ~/.bash_profile executed at the start of login shell ## delete bash variable unset MY_VAR ## foreground a job - `jobs` - `fg %1` ## security - Stack Smashing Protection ### set the shell properties example make it print all the commands set -x ### unset set +x ## print the shell name echo $SHELL ## create coredump up on abort - `ulimit -c unlimited` # screen # scroll C-a ESC ESC or q to end scroll RET to select text RET to copy text C-a ] - to paste text C-a c - new window C-a C-a - switch to last active window C-a ' C-a n - change to next window in list C-a p - change to previous window in list C-a " - window list screen -S screen -D -R -S # set baud rate for a serial device stty -F /dev/ttyUSB0 115200 # start dialog with serial device (telnet) picocom -b 115200 -e p /dev/ttyUSB0 ## quit picocom `C-p C-q` # print disk usage or file size in human readable format du -h /var/log # print sizes of only immediate files or folders du -sh /mnt/root-rebound/* # print disk usage df -h split -b 23k /var/tmp/portage/media-video/vlc-3.0.9999/temp/build.log build.log;fs=(build.log*);tot=${#fs[@]};i=0;for f in ${fs[@]}; do if [ $i -lt 3 ] || [ $i -gt $(( $tot - 3 )) ]; then wgetpaste -s dpaste $f; echo ""; fi; i=$(( $i + 1 )); done;rm -f build.log* ## print keys - `showkey -s` ## change key map - `setxkbmap -option altwin:swap_alt_win` swaps alt and super keys ## nano - `M-w search next16` ## tmux tmux ls tmux a -t 1 C-a :kill-session tmux kill-session -t 3 C-a q - kill window tmux -L myname tmux -L myname a C-a d detach C-a c - new window C-a p - switch to previous w C-a 1 - switch to 1 window C-a [ - copy mode start C-r - search up C-s - search down n - next N - previous C-space - toggle selecting text with arrows M-w or C-w - copy C-a ] - paste C-a :swap-window -t 0 C-a :set -g status off C-a % split vertically into panes C-a " split horizontally C-a o switch panes C-a :resize-pane -U/D/L/R 10 ### transfer program to tmux ### tmux.conf #send prefix inside app; C-a a sends C-a inside app bind-key a send-prefix #for emacs set -g default-terminal "tmux-256color" #for 24bit color set-option -a terminal-overrides ",*256col*:RGB" ## steam ## get terminfo infocmp -x infocmp tmux.xterm-256color # add terminfo tic -x /etc/terminfo/t/tmux-24Mcolor-italic.terminfo ## Check 256 color printf "\x1b[38;5;165m256color\e[0m\n" ## Check true color printf "\x1b[38;2;255;02;03mTRUECOLOR\x1b[0m\n" ## get background color printf "\e]11;?\a" ## print ascii colors ``` RED=$(tput setaf 1) GREEN=$(tput setaf 2) YELLOW=$(tput setaf 3) BLUE=$(tput setaf 4) MAGENTA=$(tput setaf 5) CYAN=$(tput setaf 6) WHITE=$(tput setaf 7) RESET=$(tput sgr0) echo "${RED}This is red text.${RESET}" echo "${GREEN}This is green text.${RESET}" echo "${YELLOW}This is yellow text.${RESET}" echo "${BLUE}This is blue text.${RESET}" echo "${MAGENTA}This is magenta text.${RESET}" echo "${CYAN}This is cyan text.${RESET}" echo "${WHITE}This is white text.${RESET}" ``` ## printf echo file printf '%b\n' "$(cat my_file.txt)" # leave current command in middle for another command and resume C-u -leave C-y -resume # search command history C-r curl --request GET --header 'PRIVATE-TOKEN: ckHc9TVLdszJfwsFSYwS' 'https://gitlab.example.com/api/v4/projects/13083/repository/files/app%2Fmodels%2Fkey%2Erb?ref=master' -o Erb.txt ## measure time taken by a command time # mount img fdisk -l /mnt/Aren/software/common/genpi64.img # get offset; if sectors muliply with sector size to get offset in bytes sudo mount -o loop,offset=$((131072*512)),sizelimit=$((23068672*512)) /mnt/IceCap/software/common/genpi64.img /mnt/genpi64/ sudo mount -o loop,offset=$((2048*512)),sizelimit=$((129024*512)) /mnt/IceCap/software/common/genpi64.img /mnt/genpi64/boot/ losetup -d /dev/loop0 # create a loop device mknod -m 0660 /dev/loop8 b 7 8 ## ttyecho https://www.humbug.in/2010/utility-to-send-commands-or-data-to-other-terminals-ttypts/ ## send commands to another terminal tty sudo ttyecho -n /dev/pts/0 "~/workspace/synergy-core/build/bin/synergyc -f --restart -n RPi3B 192.168.2.9" # A real-time interactive pattern matcher in console that updates the tty input buffer. # selector examples selector -v -x @ <(find . -maxdepth 2 -type d | awk '{print $0"@cd "$0}') selector -v -x @ <(grep -E -o 'http[^ ]+' fileWithURLS) # Find current tty tty # to find tty associated with /dev/ttyUSB0 dmesg | grep tty ## view old dmesg journalctl -o short-precise -k -b -1 ## view dmesg in real time dmesg -w # list processes attached to a tty ps -t /dev/tty1 -o comm= # list all processes ps -eaf pstree # list shared libraries to which the process is linked to lsof -p # change virtual tty chvt # get file owner stat -c '%U' /path/of/file/or/directory # suspend a shell suspend # suspend a chroot suspend kill -sigcont ## suspend and resume a process kill -STOP kill -CONT ## set process priority - `renice ` lesser the niceness higher the priority ## wget saveas wget https://lkml.org/lkml/diff/2018/12/11/1277/1 -O 2 ### wget download folder wget -r -np -R "index.html*" http://example.com/configs/.vim/ # nfs reload config exportfs -ra # nfs can't be mounted with username and password or try -O user=root,pass=mypass # s & t sticky bits s - makes files in the dir with its gid or executes files with their uid (setuid) t - makes files in the dir deletable only by their owner # to set stick bit on all the subfolders including current folder sudo chmod g+s ./**/ # to set umask for a directory sudo setfacl -d -m group:portage:rwx /var/db/repos/gentoo/**/ fallocate -l 1G /swapfile mkfs.fat mkfs.vfat -F 32 mkfs.ext4 -L