3. Miscellaneous

3.1. Check version

$ cat /etc/debian_version
bookworm/sid
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.5 LTS"

3.2. User management

Add a new user:

$ sudo adduser xxxx

Add the new user to the sudo group:

$ sudo usermod -aG sudo xxxx

Add a new user with other specified attributes:

$ sudo adduser --disabled-password --disabled-login --no-create-home --gecos "XXXX" --uid 1001 xxxx

Delete a user:

$ sudo deluser xxxx
Removing user `xxxx' ...
Warning: group `xxxx' has no more members.
Done.
$ sudo rm -rf /home/xxxx

3.3. hostname

Show hostname:

$ hostname

Temporarily set hostname:

$ sudo hostname xxxx

Edit /etc/hostname to set it permanently (effective after reboot), and make it effective immediately after editing the file:

$ sudo hostname -F /etc/hostname

You may need to edit /etc/hosts to map the hostname accordingly.

3.4. systemctl editor

The editor to do systemctl edit can be changed to vi by:

export SYSTEMD_EDITOR=vi

Often we run the command with sudo, but the envs are cleared for security reasons. So we need to config sudo to keep the necessary envs:

$ sudo visudo
 # Ditto for GPG agent
 #Defaults:%sudo env_keep += "GPG_AGENT_INFO"
 
+Defaults:%sudo env_keep += "SYSTEMD_EDITOR"
+
 # Host alias specification
 
 # User alias specification

Note

Make sure the owner of /etc/sudoers is root:root and its mode is 0440, or sudo will refuse to run. Also, the rule is for sudo group, so the current user must be in this group.

3.5. Clear iptables

Delete all rules in all chains:

$ sudo iptables -F

Delete all chains (except the default ones):

$ sudo iptables -X

List rules:

$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

3.6. List port listening

$ sudo lsof -i :3306
COMMAND     PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
mariadbd 200092 mysql   20u  IPv4 1866849      0t0  TCP localhost:mysql (LISTEN)