Skip to content

Instantly share code, notes, and snippets.

@bp2008
Last active August 28, 2024 17:11
Show Gist options
  • Save bp2008/44ad5c81dca23010139fbdc2bc18f286 to your computer and use it in GitHub Desktop.
Save bp2008/44ad5c81dca23010139fbdc2bc18f286 to your computer and use it in GitHub Desktop.
Ubuntu Linux / Raspberry Pi Cheat Sheet

Linux Cheat Sheet

nginx

nginx is typically located at /usr/bin/nginx
nginx configuration is at /etc/nginx

Start

nginx

Stop

nginx -s stop

Reload configuration without downtime

nginx -s reload

Test configuration changes (do this before reloading)

nginx -t

Misc Linux

In order to mount Windows file shares (smb://)

As of NOOBS 2.9.0 the raspberry pi does not offer SMB as a choice in the Type menu at File Manager > Go > Connect to Server.

Try instead entering directly into the address bar smb://wherever/.

This may or may not be needed first:

sudo apt-get install  samba-common smbclient samba-common-bin smbclient  cifs-utils

To create symbolic link (soft link)

ln -s file1 link1

e.g.

ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/mysite

To reboot

sudo reboot now

To shutdown

sudo shutdown now

To get the local hostname

hostname -I

Disk Status

df - Shows the amount of disk space used and available on Linux file systems.
du - Display the amount of disk space used by the specified files and for each subdirectory.
du -cha --max-depth=1 / | grep -E "M|G" - Find large folders.
du -cha --max-depth=1 /lib | grep -E "M|G" - Find large folders in /lib.
sudo du -ahx / | sort -rh | head -n 20 - Find largest directories/files.

Monitor Current CPU Speed

Overall

watch -n1 "lscpu | grep 'MHz' | awk '{print $1}'"

Per Logical Core

watch -n1 "cat /proc/cpuinfo | grep 'MHz' | awk '{print $1}'"

Task Manager (equivalent)

top for basic info, htop for much more detail

In top, there is a line beginning with %Cpu(s). The meaning of the numbers is as follows:

us - Time spent in user space
sy - Time spent in kernel space
ni - Time spent running niced user processes (User defined priority)
id - Time spent in idle operations
wa - Time spent on waiting on IO peripherals (eg. disk)
hi - Time spent handling hardware interrupt routines. (Whenever a peripheral unit want attention form the CPU, it literally pulls a line, to signal the CPU to service it)
si - Time spent handling software interrupt routines. (a piece of code, calls an interrupt routine...)
st - Time spent on involuntary waits by virtual cpu while hypervisor is servicing another processor (stolen from a virtual machine)

(source)

Raspberry Pi Stuff

Fix unstable x11vnc server

An easier alternative is to just use the built-in RealVNC server, but that doesn't work with a TightVNC client.

x11vnc available for raspberry pi as of May 25, 2018 (and for months earlier at least) crashes often. So to get the fix WHICH WAS FOUND IN 2016 AT LEAST we have to build from source.

sudo apt-get install autoconf libssl-dev xorg-dev libvncserver-dev
sudo apt-get build-dep x11vnc
git clone https://github.com/LibVNC/x11vnc.git
cd x11vnc
./autogen.sh
make

The built x11vnc executable will be in the src directory because why not. The x11vnc.program file in this folder runs x11vnc out of here.

Removing unwanted screensavers, screen timeouts, etc

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

add these lines if they do not already exist:

@xset s noblank 
@xset s off 
@xset -dpms

ALSO the pi apparently has a screensaver enabled by default but no way to configure it.

sudo apt-get install xscreensaver

Then in Menu > Preferences > Screensaver, disable the screensaver.

Static IP Address

Edit /etc/dhcpcd.conf via sudo nano /etc/dhcpcd.conf and follow the templates provided within.
Example:

interface eth0
static ip_address=192.168.0.2/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1 8.8.8.8

Multiple static IP addresses

Add a single static IP address via the method above.
Run sudo nano /etc/dhcpcd.exit-hook
In this file, insert a line like this to add your second static IP address:

ip address add 192.168.1.2/24 dev eth0

Advanced IP address configuration

I haven't tested the methods here, but:

interface eth0
arping 192.168.2.1
arping 192.168.4.50

profile 192.168.2.1
static ip_address=192.168.2.44/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1

profile 192.168.4.50
static ip_address=192.168.0.44/24
static routers=192.168.4.50
static domain_name_servers=192.168.4.50

List IP Addresses

ip address list

or

ip address show dev eth0

Boot partition is full

This typically affects my Ubuntu VMs after a while.

When the boot partition is full, as indicated by df, it is probably necessary to remove old kernels.

All-in-one command

sudo dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -v `uname -r` | while read -r line; do sudo apt-get -y purge $line;done;sudo apt-get autoremove; sudo update-grub

Manual

https://askubuntu.com/questions/345588/what-is-the-safest-way-to-clean-up-boot-partition

First check your kernel version, so you won't delete the in-use kernel image, running:
uname -r
Now run this command for a list of installed kernels:
dpkg --list 'linux-image*' | grep ^ii
and delete the kernels you don't want/need anymore by running this:
sudo apt-get remove linux-image-VERSION
Replace VERSION with the version of the kernel you want to remove.

If you can't remove any because of the boot partition being full, try running this: sudo rm -rf /boot/*-3.2.0-{23,45,49,51,52,53,54,55}-* replacing 3.2.0 and the numbers in curly braces with appropriate numbers from the list of installed kernels.

When you're done removing the older kernels, you can run this to remove ever packages you won't need anymore:
sudo apt-get autoremove
And finally you can run this to update grub kernel list:
sudo update-grub

Alternate Commands: https://gist.github.com/ipbastola/2760cfc28be62a5ee10036851c654600

Prevention

To prevent this happening, it is supposed to be possible to remove old crap automatically.

See UbuntuServerSetup Script for a simple script to setup new Ubuntu Server VMs.

sudo dpkg-reconfigure --priority=low unattended-upgrades
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

Enable the setting Unattended-Upgrade::Remove-Unused-Dependencies and reboot the machine.

Which Ubuntu Server version is installed?

lsb_release -a

Killing processes

Use the commands kill if you know the process ID, or killall if you don't. Run each of these without arguments to see usage.

Example: killall -i mono will prompt before killing any process matching "mono".

Finding processes

To find "mono":

ps aux | grep -i mono

Find Listening Ports

From: https://www.cyberciti.biz/faq/unix-linux-check-if-port-is-in-use-command/

Run any one of the following command on Linux to see open ports:

sudo lsof -i -P -n | grep LISTEN
sudo netstat -tulpn | grep LISTEN
sudo lsof -i:22                      ## see a specific port such as 22 ##
sudo nmap -sTU -O IP-address-Here

For the latest version of Linux use the ss command. For example, ss -tulw

SSH Keys (Linux Client)

Step 1) Creating Keys for a Host

Log in to the terminal on the machine that needs to have keys created for itself.

ssh-keygen -t rsa

Some prompts will appear asking for the location to save the keys and a passphrase. You can just press enter to use the defaults at each step.

Step 2) Allow the host's keys to be used to authenticate to a remote SSH server

ssh-copy-id -i /root/.ssh/id_rsa.pub user@RemoteHostname

(replace user and RemoteHostname as appropriate)

You will be asked for authentication.

Step 3) Test key-based authentication

ssh user@RemoteHostname

Access should be granted without login prompt.

exit

Listing authorized keys allowed to connect to a host

https://security.stackexchange.com/questions/86256/how-to-view-all-ssh-authorized-keys-for-a-unix-server#:~:text=ssh%2Fauthorized_keys%20exists%20in%20that,ssh%2Fauthorized_keys%20.

#!/bin/bash
for X in $(cut -f6 -d ':' /etc/passwd |sort |uniq); do
  for suffix in "" "2"; do
    if [ -s "${X}/.ssh/authorized_keys$suffix" ]; then
      echo "### ${X}: "
      cat "${X}/.ssh/authorized_keys$suffix"
      echo ""
     fi;
   done;
done

More information

https://www.cyberciti.biz/faq/how-to-set-up-ssh-keys-on-linux-unix/

SSH Keys (Putty Client on Windows)

You can use your existing key pair to authenticate with the Ubuntu server. Here's how you can do it:

  1. First, you need to copy the public key to the Ubuntu server. You can do this by opening PuTTYgen, loading your private key file (ppk format), and then copying the entire contents of the "Public key for pasting into OpenSSH authorized_keys file" box.

  2. Next, log in to your Ubuntu server using PuTTY with your username and password as usual.

  3. Once you are logged in, you need to add the public key to the authorized_keys file. You can do this by running the following command: echo 'your-public-key' >> ~/.ssh/authorized_keys. Make sure to replace your-public-key with the public key you copied earlier.

  4. Finally, you need to configure PuTTY to use your private key for authentication. You can do this by going to the "Connection" -> "SSH" -> "Auth" section in PuTTY's configuration and selecting your private key file (ppk format) under "Private key file for authentication".

After completing these steps, you should be able to log in to your Ubuntu server using your key pair instead of a username and password.

Source: Conversation with Bing, 8/17/2023  
(1) ubuntu - Use existing SSH key pair with openSSH to connect to a server .... https://superuser.com/questions/1699465/use-existing-ssh-key-pair-with-openssh-to-connect-to-a-server.  
(2) How To Configure SSH Key-Based Authentication on a Linux Server. https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server.  
(3) Unable to use RSA keys with SSH: Server refused our key. https://askubuntu.com/questions/580395/unable-to-use-rsa-keys-with-ssh-server-refused-our-key.  
(4) How to Set up public key authentication in Ubuntu | Ubuntu Server .... https://hostingultraso.com/help/ubuntu/how-set-public-key-authentication-ubuntu.  

Avoid being prompted for a username when using Putty

Enter the user name in the Host Name field like this: user@hostName

Avoid being prompted for a password when using sudo

If you want to avoid being prompted for a password when using sudo, you can configure the sudoers file to allow passwordless sudo for your user account. Here's how you can do it:

  1. Open a terminal and type sudo visudo to edit the sudoers file.

  2. In the sudoers file, find the line that looks like this: %sudo ALL=(ALL:ALL) ALL

  3. Change this line to: %sudo ALL=(ALL) NOPASSWD:ALL

  4. Save and close the sudoers file.

After making these changes, you should be able to use sudo without being prompted for a password. However, please note that allowing passwordless sudo can be a security risk, so make sure you understand the implications before making this change.

Source: Conversation with Bing, 8/17/2023  
(1) How to avoid being prompted for a password by sudo? - Ask Ubuntu. https://askubuntu.com/questions/470383/how-to-avoid-being-prompted-for-a-password-by-sudo.  
(2) sudo - How to disable the password prompts? - Ask Ubuntu. https://askubuntu.com/questions/675379/how-to-disable-the-password-prompts.  
(3) Prevent sudo from prompting for password when running non-permitted .... https://unix.stackexchange.com/questions/303771/prevent-sudo-from-prompting-for-password-when-running-non-permitted-command.  
(4) command line - Execute sudo without Password? - Ask Ubuntu. https://askubuntu.com/questions/147241/execute-sudo-without-password.  
(5) prompt - How do I stop Ubuntu from asking for my password every time I .... https://askubuntu.com/questions/852527/how-do-i-stop-ubuntu-from-asking-for-my-password-every-time-i-install-something.  
(6) undefined. https://askubuntu.com/a/614537/115816.  
(7) undefined. https://help.ubuntu.com/community/Sudoers.  

systemd / systemctl

To configure and manage a service, create a *.service file in /etc/systemd/system/ according to the following example.

Example

sudo nano /etc/systemd/system/temperserverpi.service
[Unit]
Description=Temper Server Pi
# If the service relies on network uncomment the next line.
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/mono-service /home/pi/temper/TemperServerPi.exe

[Install]
WantedBy=multi-user.target

After creating this file, run

sudo systemd-analyze verify temperserverpi.service

and if everything is okay (if it outputs nothing), then you can manage the service via:

sudo systemctl enable temperserverpi         <-- Allow to start at boot
sudo systemctl disable temperserverpi        <-- Disallow to start at boot
sudo systemctl status temperserverpi
sudo systemctl start temperserverpi
sudo systemctl stop temperserverpi
sudo systemctl restart temperserverpi
sudo systemctl reload temperserverpi               <-- Some apps can "reload" more efficiently than restarting.
sudo systemctl reload-or-restart temperserverpi    <-- Reloads if available, otherwise restarts

In the specific case of mono-service, it is necessary to use the option --no-daemon. Otherwise the service will immediately exit (I think because of mono-service trying to send the service to the background -- this is probably not compatible with systemd). Alternatively, if you can build the service as a command line app, you can run it using mono, and systemd has no problem with that.

ExecStart=/usr/bin/mono-service --no-daemon /home/pi/DotNetServiceApp.exe
ExecStart=/usr/bin/mono /home/pi/DotNetCommandLineApp.exe

More info: https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units

More Commands

systemctl daemon-reload

Advanced changes to the .service file may require you to reload the systemd manager configuration:

systemctl daemon-reload

daemon-reload

Reload the systemd manager configuration. This will rerun all generators (see systemd.generator(7)), reload all unit files, and recreate the entire dependency tree.

Source: https://www.man7.org/linux/man-pages/man1/systemctl.1.html

Installing Webmin

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl apt-show-versions python unzip

Get the latest version of webmin from https://www.webmin.com/deb.html

(as of 2021-08-30:)

wget wget http://prdownloads.sourceforge.net/webadmin/webmin_1.981_all.deb
sudo dpkg --install webmin_1.981_all.deb

The service listens by default on port 10000. e.g. https://192.168.0.1:10000/

Disabling SSL

If SSL for the web interface is unwanted, change ssl=1 to ssl=0 in /etc/webmin/miniserv.conf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment