# Command line tips and tricks # Apt / apt-get Install error ``` Unpacking xroad-signer (7.6.2-1.ubuntu22.04) over (7.5.1-1.ubuntu22.04) ... dpkg: error processing archive /var/cache/apt/archives/xroad-signer_7.6.2-1.ubuntu22.04_amd64.deb (--unpack): unable to open '/etc/xroad/conf.d/signer-console-logback.xml.dpkg-new': No such file or directory No apport report written because the error message indicates an issue on the local system ```
Solution
``` rm -rf /var/cache/apt/archives/xroad-signer_7.6.2-1.ubuntu22.04_amd64.deb apt --fix-broken install ``` ##### Find out from which deb package a file is ``` # dpkg -S p11tool gnutls-bin: /usr/bin/p11tool gnutls-bin: /usr/share/man/man1/p11tool.1.gz ``` ##### Add gpg key from url `apt-key adv --fetch-keys ` ##### Show info about the package `apt show ` ##### List available updates `apt list --upgradable` ##### Do everything `apt update && apt dist-upgrade -y && apt autoremove -y && apt autoclean -y && lsof | grep lib | grep DEL && init 6` ##### List the package versions available from all your sources `apt-cache madison ` ##### Search package from local cache `apt-cache search ` # Bash tools ##### Awk First and third column from file, seperated by spaces `awk -F " " '{print $1,$3'} result.txt` Print from line 156 to line 170 `awk 'NR >= 156 && NR <= 170' log.txt` ##### Cat Copy Data to Other File `cat file1 > file2` Combine Files And Sort Alphabetically `cat file1 file2 | sort > file3` Create new file that consists of the contents of file1, followed by keyboard. `cat file1 -> file2` Multi line to singe line `cat multiline.txt | tr -d '\n' > oneline.txt` ##### Cut First and third column from file, seperated by spaces `cut -d' ' -f1,3 result.txt` ##### Find Search for a file `find location -name test.zip` `find / -name phppgadmin.conf` Search filetype `find / -i name "*.txt"` Accessed less than 3m ago `find / -amin -3` Accessed two days ago `find / -time -2` Smaller than 5MB, bigger than 2MB `find / -size -5M -and -size +2M` Remove all .DS\_Store files `find . -name '*.DS_Store' -type f -delete` Find files older than 7 days ``` find -type f -mtime +7 ``` **Mtime** *+n* More than *n*. *n* Exactly *n*. *-n* Less than *n*. ``` You can write -mtime 6 or -mtime -6 or -mtime +6: Using 6 without sign means "equal to 6 days old — so modified between 'now - 6 * 86400' and 'now - 7 * 86400'" (because fractional days are discarded). Using -6 means "less than 6 days old — so modified on or after 'now - 6 * 86400'". Using +6 means "more than 6 days old — so modified on or before 'now - 7 * 86400'" (where the 7 is a little unexpected, perhaps). ``` ##### Grep
FlagDescription
-iIgnore Case
-lPrint name of each file which contains a match
-vReturn all lines which don't match the pattern
-cPrint a count of matching lines
-nPrint the line nr before each line that matches
List all files in directory where file name contains "test" `ls | grep "test"` `grep "" -l "test"` To search for a line containing text hello.gif in any .txt files `grep "hello\.gif" *.txt` Exactly and solely "apple" `grep -x "apple" list.txt` Everything else but not "apple" `grep -v "apple" fruitlist.txt` Match insensitive `grep -i "fruit" fruitlist.txt` Match apple and Apple `grep "[Aa]pple" fruitlist.txt` Grep Terminal History `history | grep "ionic"` Output non duplicates `grep -vFf file1 file2` ##### iPerf3 ``` # on receiver iperf3 -s -p 7575 # on client iperf3 -c -p 7575 ``` ##### Links Create symbolic link `ln -s /source /destination` Remove link `unlink /destination/script.sh` ##### Netcat Scan ports `nc -vvvnz -w5 185.31.92.195 5577 5500` ##### Netstat `sudo netstat -lntp`l - listening ports n - no hostnames t - tcp p - processes ##### SSH ssh-key to machines `ssh-copy-id ` ##### Tar Archive a directory / File `tar -zcvf archive.tar.gz test/` Unarchive ``` tar -zxvf archive.tar.gz tar -zxvf archive.tar.gz -C tar xopf thing.tar ``` TCP `tcpdump -n -v -i eth0 -s 0 port 80 -w /home/$USER/tcpdump.pcap`-n don't convert addresses to names -v verbose -i interface eth0 -s snapshot length ##### Users Remove user from group `passwd -d ` Reload bash user `. ~/.bash_profile` Display all users or groups `compgen -u` `compgen -g` Add user to sudoers `sudo usermod -aG sudo qrl` ##### Zip Compress a directory / File `zip -r ` Decompress `unzip -d ` ##### Wget Save website ``` wget --mirror --convert-links --adjust-extension --page-requisites --reject-regex "report" --no-warc-compression --warc-file="kalaralli" https://qrl.ee/kalaralli/ ``` # Docker ##### Read compose logs ``` docker-compose logs -f --tail=100 ``` ##### Volume in dir ``` - ./name:/name # For macOS - ${PWD}/name:/name ``` ##### Force restart service with compose `docker-compose up -d --force-recreate ` ##### Port forwarding <host\_port>:<container\_port> ##### Build image `docker build -t /: .` ##### Push image to docker hub registry `docker image push /image-name` ##### Docker-compose port forwarding `host-port:container-port` ##### Rebuild container `docker-compose up -d --no-deps --build --force-recreate ` \--no-deps - Don't start linked services \--build - Build images before starting containers \--no-cache \--force-recreate ##### Updating update single image `docker-compose pull deluge` update all images `docker-compose pull` update single container `docker-compose up -d deluge` update all necessary containers `docker-compose up -d` remove unnecessary images `docker image prune` ##### Get container image version `docker inspect | grep -i version` # Git ##### Push commits to another branch ``` git push origin : ``` ##### List git config settings ##### `git config --list` Change git user for single repository `git config user.name "John Smith"` ##### Change git user email globally `git config --global user.email "john@smith.com"` ##### Checkout to old commit `git checkout ` ##### Change git case sensitivity(default is true) `git config core.ignorecase false` ##### Restore to certain commit `git reset --hard f170a0c` `git push -f` ##### Change origin URL `git remote -v` `git remote set-url origin ` ##### Reset one file `git checkout HEAD -- ` ##### Clone specific branch `git clone -b ` ##### Show commit diff `git show ` # IPtables
\# src(me) -> dest
iptables -A OUTPUT -p tcp --match multiport --dports 80,443 -d 10.11.12.55 -j ACCEPT -m comment --comment 'X-tee turvaserver'
\# dst -> src(me)
iptables -A INPUT -p tcp --match multiport --dports 80,443 -s 10.11.12.55 -j ACCEPT -m comment --comment 'X-tee turvaserver'
\# list by line numbers
iptables -L --line-numbers
\# reegli nr 24 kustutamine
iptables -D INPUT 24
\# reegli lisamine reale 53
iptables -I OUTPUT 53 -p tcp --match multiport --dports 80,443 -d 10.11.12.55 -j ACCEPT -m comment --comment 'X-tee turvaserver'
# macOS / OS X ##### Mac install sshpass ``` brew tap esolitos/ipa brew install sshpass ``` ##### Vagrant bash completion `brew install vagrant-completion` add to .bash\_profile: ``` if [ -f `brew --prefix`/etc/bash_completion.d/vagrant ]; then source `brew --prefix`/etc/bash_completion.d/vagrant fi ``` ##### Ignore .DS\_Store globally `echo ".DS_Store" > ~/.gitignore_global` `git config --global core.excludesfile ~/.gitignore_global` ##### Update macOS DB File `/usr/libexec/locate.updatedb` ##### Launch VS Code from Terminal In VS Code do `⇧⌘P` and type `Shell Command: Install 'code' command in PATH` In Terminal, reload user `source ~/.bash_profile` ##### Find process PID by port `lsof -i tcp:8080` ##### Mac equivalent to netstat -lntp `netstat -p tcp -van | grep LISTEN` ##### [Reset Final Cut Pro Trial](https://gist.github.com/dannote/17e0396fe2e19c6e60c915838376d267) `mv -v ~/Library/Application\ Support/.ffuserdata ~/.Trash` # macOS software setup #### Install first
- docker - [https://docs.docker.com/docker-for-mac/install/](https://docs.docker.com/docker-for-mac/install/) - iterm2 - [https://iterm2.com/](https://iterm2.com/) - fork - [https://git-fork.com/](https://git-fork.com/) - homebrew - [https://brew.sh/](https://brew.sh/) - postman - [https://www.postman.com/](https://www.postman.com/) - skype - [https://www.skype.com/en/get-skype/](https://www.skype.com/en/get-skype/) - skype for business - [https://www.microsoft.com/en-us/download/details.aspx?id=54108](https://www.microsoft.com/en-us/download/details.aspx?id=54108) - visual studio code - [https://code.visualstudio.com/download](https://code.visualstudio.com/download')
#### Install with brew ##### ansible `brew install ansible` ##### vagrant `brew tap hashicorp/tap` `brew install vagrant` ##### ansible-lint `brew install ansible-lint` ##### bash-completion [https://sourabhbajaj.com/mac-setup/BashCompletion/](https://sourabhbajaj.com/mac-setup/BashCompletion/) ##### vagrant-completion `brew install vagrant-completion` ``` if [ -f `brew --prefix`/etc/bash_completion.d/vagrant ]; then source `brew --prefix`/etc/bash_completion.d/vagrant fi ``` ##### sshpass `brew tap esolitos/ipa` `brew install sshpass` ##### terraform and terragrunt `brew install terraform terragrunt` #### htop brew install htop ##### wget brew install wget ##### whois brew install whois #### Install from App Store - rocketchat #### Backup ``` export PC_NAME=nimi && \ cd ~ && \ tar -zcf $PC_NAME-downloads-`date +"%Y-%m-%d_%H-%M"`.tar.gz ~/Downloads/ && \ tar -zcf $PC_NAME-desktop-`date +"%Y-%m-%d_%H-%M"`.tar.gz ~/Desktop/ && \ tar -zcf $PC_NAME-lab-`date +"%Y-%m-%d_%H-%M"`.tar.gz ~/LAB/ ``` # Mounting Get info of the disk(s) ``` fdisk -l ls -l /dev/disk/by-uuid/ ``` Manual USB mount ``` mount /dev/sdb1 /mnt/usb-ssd -o uid=qrl,gid=qrl,iocharset=utf8,nofail ``` Auto USB mount ( /etc/fstab ) ``` UUID=6403-240D /mnt/usb-ssd exfat uid=qrl,gid=qrl,iocharset=utf8,nofail 0 0 ``` Auto NAS mount ( /etc/fstab ) ``` //192.168.1.109/usbdisk1 /mnt/nas-usb cifs username=qrl,password=qrl,iocharset=utf8,uid=1001,gid=1002,nofail 0 0 ``` # Network Newer Debian ``` resolvectl status service systemd-resolved restart ``` Older Debian ``` systemd-resolve --status systemctl restart systemd-resolved.service ``` # OpenSSL ##### Get sha256 fingerprint of cert ``` openssl x509 -noout -sha256 -fingerprint -in ``` ##### Utf8 output ``` -nameopt utf8 ``` ##### Convert RSA DER public key to PEM ``` openssl rsa -pubin -inform DER -in slot3_pub.key -outform PEM -out slot3_pub.pem ``` ##### Convert DER certificate to PEM ``` openssl x509 -inform der -in cert.der -out cert.pem ``` ##### Convert DER CSR to PEM ``` openssl req -inform der -in der-csr.csr -out pem-csr.pem ``` ##### Get pub key of CSR ``` openssl req -pubkey -noout -in .csr ``` ##### Get pub key from certificate ``` openssl x509 -pubkey -noout -in .crt ``` ##### Modulus of Pub Key ``` openssl rsa -noout -text -modulus -in [public_key_filename] ``` ##### Modulus of Certificate ``` openssl x509 -noout -text -modulus -in [certificate_filename] ``` ##### View chained certificate ``` openssl storeutl -text -noout -certs CAFile.pem ``` ##### View CSR ``` openssl req -noout -text -in .csr ``` ##### View PEM certificate ``` openssl x509 -in cert.pem -text -noout ``` ##### View RSA public key ``` openssl rsa -pubin -in slot3_pub.pem -text ``` # Pkcs11-tool
Install pkcs11-tool
``` apt install opensc ```
Get hsm info
``` pkcs11-tool --module /opt/nfast/toolkits/pkcs11/libcknfast.so --show-info ```
List available slots
``` pkcs11-tool --module /opt/nfast/toolkits/pkcs11/libcknfast.so --list-slots ```
List objects on specified slot
``` pkcs11-tool --module /opt/nfast/toolkits/pkcs11/libcknfast.so --list-objects --slot 0x1f655491 ```
List certificates on specified slot
``` pkcs11-tool --module /opt/nfast/toolkits/pkcs11/libcknfast.so --list-objects --slot 0x1f655491 --type cert ```
Export public key
``` pkcs11-tool --module /usr/lib/libcs_pkcs11_R2.so --login --slot 3 --read-object --type pubkey --id --output-file slot3_pub.key ```
Export certificate(needs partitiob/slot password)
``` pkcs11-tool --module /opt/nfast/toolkits/pkcs11/libcknfast.so --login --slot 0x1f55492 --read-object --type cert --id --output-file cert.der ```
Thales-Other
``` pkcs11-tool --module /usr/lib/libCryptoki2_64.so --list-slots pkcs11-tool --module /usr/lib/libCryptoki2_64.so --list-objects --login --login-type user --token-label