Text Editors

✍ Vim
Save
    :w
Quit
    :qa
Delete all lines
    :%d

Package Management

✍ Uninstall package

  1. sudo apt remove <package_name>
  2. sudo apt purge <package_name>
  3. Remove any package files inside /usr/bin
  4. Close the terminal.
  5. Reopen the terminal.

Domain and Host

✍ Find server ip address
host domainname.com

✍ Find server location
curl https://ipinfo.io/host_ip_address

PHP

✍ Restart PHP fpm (this is required when you edit php.ini under /etc/php/7.4/fpm)
service php7.4-fpm restart

Services

✍ View systemctl log
journalctl -u service-name.service (e.g. journalct - u tmb.service)
✍ Find running service by name.
ps -ef | grep {application_name or part of it}
✍ Start service on boot.
sudo systemctl enable {service name}

Network

✍ Find out which port a process is listening on. 
sudo netstat -ltnp
This command will also give you the PID. You can kill the process with "kill -9 {PID}"

Process and Memory

✍ Terminate process
kill {PID}
kill -9 {PID}

Docker

✍ Show running containers
docker ps

Database

✍ Install mongodb php driver on Ubuntu 18.04 (php 7.2)
1. sudo apt-get install php7.2-dev
2. sudo pecl install mongodb
3. Add "extension=mongodb.so" to php.ini under \etc\php\7.2\apache2
4. sudo service apache2 restart
✍ Install mongodb php driver on Windows 10 (php 7.2.4)
1. Download x64 TS version from https://pecl.php.net/package/mongodb/1.4.2/windows
2. Extract php_mongodb.dll to D:\wamp\bin\php\php7.2.4\ext
3. Add "extension=php_mongodb" to php.ini under D:\wamp\bin\apache\apache2.4.33\bin
3. Restart Apache.

Apache

✍ Enable apache site
a2ensite xyz.abc.com.conf
✍ View enabled modules
apachectl -M
✍ Bitnami

  • Restart Apache
    sudo /opt/bitnami/ctlscript.sh restart apache

Devices

✍ List usb devices
lsusb
✍ View cpu temperature (please install lm-sensors before using this command):
sensors

Disk

✍ Check disk space
df -H

✍ Check which processes are using disk I/O
iotop
iotop --only
✍ See how much space the top-level folders use (up to 3 levels):
sudo du -cha --max-depth=3 / | grep -E "M|G"
✍ Limit the size of journal log and set max age to 1 day:
sudo journalctl --vacuum-time=1d
✍ Find total hdd size:
dmesg | grep blocks
sudo fdisk -l
✍ Free up disk space used by composer cache
composer clear-cache

Cron Jobs

✍ Edit cron jobs:
crontab -e
press i to start editing
after finishing editing, press esc, and press :wq to save and quit

System Log

✍ View history of reboot date and time:
last reboot
✍ Clear btmp log files
cat /dev/null > /var/log/btmp
cat /dev/null > /var/log/btmp.1

Files and Folders

✍ Find all zip files in the current directory
find . -iname \*.zip
✍ Move all files from current folder to another folder
ls -l -a
✍ Show hidden file (e.g. .htaccess)
ls -l -a
✍ Create and edit text file.
sudo nano /home/sysadmin2/set_ip.sh
✍ View file and folder permissions in numerical format
stat -c "%a %n"
Rename folder
mv source_folder destination_folder
✍ Remove all files in current directory:
sudo rm -f *
✍ Remove folder and its content:
sudo rm -Rf folder-name
✍ Copy files from 2nd-level ssh machine to 1st-level ssh machione (you need to ssh to 1st-level machine before using these commands)
Source folder:
/home/box24/box24-locker
To copy files from remote server to local machine, use this command:
sudo scp -rp box24@10.0.3.72:/home/box24/box24-locker /my-documents/my-app
To exclude node_modules, git, and video folders, use this command:
sudo rsync -av --exclude='node_modules/' --exclude='.git/' --exclude='assets/videos/' box24@10.0.3.72:/home/box24/box24-locker /my-documents/my-app

User Management

✍ Allow php script to restart service without having to enter password
You can allow php script to run sudo command without password by adding this line to /etc/sudoers
www-data ALL=(ALL) NOPASSWD: /bin/systemctl restart wekan.service
Then, hit Enter and save.
And in your php deployment script, you can do something like this.
shell_exec("sudo systemctl restart wekan.service");

✍ Allow a user to run sudo command without having to enter password
sudo visudo
Then, add this line at the end of the file.
sysadmin2 ALL=(ALL) NOPASSWD:ALL
Then, hit Ctrl O to save, and hit Ctrl X to exit
✍ List all users
cut -d: -f1 /etc/passwd
✍ Create new user
sudo adduser <username>
✍ Add user to group
sudo usermod -a -G sudo sysadmin2