Linux Cheatsheet

Table of contents

Color

I wanted to display colors in my terminal. I found this AskUbuntu article. What I liked best was this one-liner:

$ for x in {0..8}; do for i in {30..37}; do for a in {40..47}; do echo -ne "\e[$x;$i;$a""m\\\e[$x;$i;$a""m\e[0;37;40m "; done; echo; done; done; echo ""

And then this python script:

#!/usr/bin/env python
import sys
terse = "-t" in sys.argv[1:] or "--terse" in sys.argv[1:]
write = sys.stdout.write
for i in range(2 if terse else 10):
    for j in range(30, 38):
        for k in range(40, 48):
            if terse:
                write("\33[%d;%d;%dm%d;%d;%d\33[m " % (i, j, k, i, j, k))
            else:
                write("%d;%d;%d: \33[%d;%d;%dm Hello, World! \33[m \n" %
                      (i, j, k, i, j, k,))
        write("\n")

Dictionary

$ sudo apt install dict
$ sudo apt install dict-gcide

I like to put this script in my ~/.bin so i can just use define word:

#!/bin/bash

ARG1=${1}

dict -d gcide $ARG1 | less

du

To show size of only top level directories in the current folder:

$ du -h -d 1

LogWatch

To setup:

$ sudo apt install logwatch

Then, edit the config at /usr/share/logwatch/default.conf/logwatch.conf, and change MailTo = user and Detail = High


Mount USB

$ sudo fdisk -l

$ sudo mount -t vfat /dev/sdb1 ~/mount -o uid=1000

When Finished:

$ sudo umount /dev/sdb1

Rsync

$ rsync -azvP -e 'ssh -p 1234' [email protected]:source dest

From the man page of rysnc:

--archive, -a     archive mode is -rlptgoD (no -A,-X,-U,-N,-H)
--recursive, -r   recurse into directories
--links, -l       copy symlinks as symlinks
--perms, -p       preserve permissions
--times, -t       preserve modification times
--group, -g       preserve group
--owner, -o       preserve owner (super-user only)
-D                same as --devices --specials
--devices         preserve device files (super-user only)
--specials        preserve special files

--compress, -z    compress file data during the transfer

--verbose, -v     increase verbosity

-P                same as --partial --progress
--partial         keep partially transferred files
--progress        show progress during transfer

About trailing slash from stackoverflow:
A trailing slash on the source changes this behavior to avoid creating an additional directory level at the destination. You can think of a trailing / on a source as meaning “copy the contents of this directory” as opposed to “copy the directory by name”, but in both cases the attributes of the containing directory are transferred to the containing directory on the destination. In other words, each of the following commands copies the files in the same way, including their setting of the attributes of /dest/foo:

rsync -av /src/foo /dest
rsync -av /src/foo/ /dest/foo

As to the destination, I don’t think it has any major consequences. There is a difference if the source is a file and destination doesn’t exist — this will make a copy of SRC called DEST:

rsync SRC DEST

Whereas, this will create directory DEST and copy the SRC file into it:

rsync SRC DEST/

SCP

$ scp -P port [email protected]:/path/to/source /dest/

Screen

To scroll in screen, hit CTRL-a and then Esc, then you can use up and down arrows or pageup pagedown to scroll. When you’re finished, hit Esc again.

Nested Sessions

If you are using screen on your desktop, and log into a server with screen running, to send commands to the nested screen session (the server) you need to use Ctrl-a a.
So to change the active session on the server it would be Ctrl-a a n.

Know if you are in screen

To know if you are within a screen session:

$ echo $STY

If you aren’t in screen, the output will be empty. If you are it will look something like 7600.pts-1.servone.


Ssh

To close a frozen ssh session: enter ~ .


Ssh-Keygen

As of OpenSSH 9.5 ed25519 has become the default key for ssh-keygen. From what I have read this seems to be a better standard than ecdsa. So, you can now just use simply ssh-keygen with no arguments, as long as your on version 9.5 or newer. If you see it says Generating public/private rsa key pair., then you are on an older version, and will need to run it with a flag to tell it to use ed25519.

$ ssh-keygen -t ed25519

If you still want to use ecdsa:
According to ssh.com, ecdsa is:

“A new Digital Signature Algorithm standarized by the US government, using elliptic curves. This is probably a good algorithm for current applications. Only three key sizes are supported: 256, 384, and 521 (sic!) bits. We would recommend always using it with 521 bits, since the keys are still small and probably more secure than the smaller keys (even though they should be safe as well). Most SSH clients now support this algorithm.”

Create an ecdsa key with a size of 521 bits.

$ ssh-keygen -t ecdsa -b 521

Ufw

Show status

$ sudo ufw status

Show status with numbers

$ sudo ufw status numbered

Delete rule (get number with status numbered)

$ sudo delete 1

Allow port

$ sudo ufw allow 9000

Only allow from a specific IP(or range)

$ sudo ufw allow from 192.168.1.0/24 to any port 9000

Wifi dongle

This worked for my DLink DWA-182

source

These steps from @Joe_Linux worked like a champ for Ubuntu 22.04 with EDUP wifi dongle using RTL8812au chipset.

Had to unplug then re-plug in dongle after final dkms command. Did not have to reboot.

Summary steps pulled from quoted post:

$ sudo apt install git build-essential libelf-dev linux-headers-`uname -r` debhelper dpkg-dev dkms bc  
$ git clone https://github.com/aircrack-ng/rtl8812au  
$ cd rtl8812au  
$ sudo make dkms_install  

unplug usb wifi dongle then re-plug back in.

boom. led lights up right away looking for SSIDs

thank you.