Linux: Ubuntu cheat sheet

Robert AndresenPC and apps Leave a Comment

This a personal Cheat sheet for Ubuntu (mostly server related stuff) + some MySQL and Docker commands. This page will be updated when I find useful commands 🙂

Guides

Disk

Show filsystem disk usage

df -h

Find what taking up diskspace

du -cha --max-depth=1 / | grep -E "M|G"

Database

Import / export mysql database

# Export
mysqldump -u username -p databasename > filename.sql

# Import
mysql -u username -p databasename < filename.sql

Docker

Docker log file taking up space

For some reasons my docker-container is storing a lot of log from apache. This log is filling up the disk.

# du -h /var/lib/docker/containers/ | grep -E "M|G"
51M     /var/lib/docker/containers/48367957...r8
1.5M    /var/lib/docker/containers/10eec...0d
574M    /var/lib/docker/containers/300456...4l
255M    /var/lib/docker/containers/89ab57...n7
16G     /var/lib/docker/containers/45b425...w2

# ll /var/lib/docker/containers/45b425...w
total 30749308
drwx--x---  4 root root        4096 Jan 31 18:30 ./
drwx--x--- 17 root root        4096 Dec 14 22:39 ../
-rw-r-----  1 root root 31487173681 Jan 31 18:42 abc123...-json.log


# tail /var/lib/docker/containers/45b425...w2/abc123...-json.log
{"log":"\u001b[0;33;1mnginx.1     | \u001b[0mapi.domain.no xxx.xxx.xxx.xxx - - [31/Jan/2024:17:43:51 +0000] \"POST /player/log HTTP/2.0\" 200 82 \"https://domain.no/\" \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36\"...

# truncate -s 0 /var/lib/docker/containers/45b425...w2/abc123...-json.log

Create log rotation

This part is from ChatGPT, where you have to create a new config file. I have not verified that it works at this moment.

# nano /etc/docker/daemon.json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}
# sudo systemctl restart docker

Auto-start / restart docker-container

docker update --restart=always 0576df221c0b

# More "gentle" mode from the documentation:
docker run -dit --restart unless-stopped <image_name>

# Stop all running containers
docker stop $(docker ps -aq)

Source: https://stackoverflow.com/questions/30449313/how-do-i-make-a-docker-container-start-automatically-on-system-boot

Kubernetes

# Deploy new version
kubectl rollout restart deployment/myapp-deployment

# Get pods (-o wide for more info)
kubectl get pod -o wide

# Bash inside pod
kubectl exec --stdin --tty myapp-1234-abcd -- /bin/bash

# Logs from pod (-f for follow)
kubectl logs -f myapp-1234-abcd

LAMP-stack

List installed PHP-extentions

sudo dpkg --list | grep php
# OR dpkg --get-selections | grep -i php
# OR php -m
# OR php -m | grep -i ftp

Check for avalible PHP-extentions

apt-cache search php | grep "^php7"
# OR apt-cache search php | grep "^php"

Log

Read log-file backwards:

tac filename | less

Check ssh login:

#Check for failed login
grep sshd.\*Failed /var/log/auth.log | less

# Check for accepted login
grep sshd.\*Accepted /var/log/auth.log | less

Troubleshoot

MySQL

#3009 – Column count of mysql.user is wrong. Expected 51, found 49. Created with MySQL 80012, now running 80015. Please use mysql_upgrade to fix this error.

mysql_upgrade --force -uroot -p

Full /boot

Soruce: https://gist.github.com/ipbastola/2760cfc28be62a5ee10036851c654600

sudo dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -v `uname -r`

sudo rm -rf /boot/*-3.19.0-{25,56,58,59,61,65}-*

sudo apt-get -f install

sudo apt-get autoremove

sudo update-grub

sudo apt-get update