Common used commands

Updated on: 14.Dec.2020

Operation

  • To view the processes that is running,
  top
 
OR
 
  htop
 
You have to install htop with the following command

  sudo apt install htop
  • To view memory usage
  free -h
 
where "h" is human readable numbers.
  •  To find the location of a program
   whereis 7z
  • To search text in a text file, replace the text that you are looking for in "some-search-text".
  sudo cat /var/log/auth.log | grep some-search-text 

If a file has been gzip (file extension is .gz), then, use zcat to

  sudo zcat /var/log/auth.log.1.gz | grep some-search-text 
 
Matching text1 and excluding text2, in this case, you need two grep commands. The first one is matching text1 and after that excluding text2.
 
  sudo zcat /var/log/auth.log.1.gz | grep text1 | grep -Ev text2
  • Find out current directory
   pwd
  • To search file in sub-directory:  
  find . -name myFileName*
  • To cancel a command, CONTROL+C.
Server management
  • To view current server name
  hostname
  • To change the host name   
    sudo hostnamectl set-hostname myserver01
 
Before rebooting the server, 
 
   sudo nano /etc/cloud/cloud.cfg

Then, change the following line from "false" to "true".

  preseve_hostname: true

Save it and reboot the server.
 
  •  Viewing the server OS version
   cat /etc/lsb-release
  •  To check if the server requires rebooting after apt update
   cat /var/run/reboot-required
 
Daemon/service management
  • To start, stop, restart or check status of a daemon
   sudo systemctl [start|stop|restart|status] service_name
 
For example, check the fail2ban service
 
  sudo systemctl status fail2ban
 
OR equivalent command:
 
  sudo service fail2ban status
  • To view all services
  sudo systemctl -l
 
OR 
 
  sudo service --status-all 
  • To view failed services
   sudo systemctl --failed
  • Search for 'nginx' service
   sudo service --status-all | grep nginx
 
apt command
  • To view installed packages
   sudo apt list --installed 
   sudo apt list -i
  • View the list of upgradable packages,
   apt list --upgradeable
  • To upgrade the installed components,
   sudo apt update
   sudo apt full-upgrade
 
   # you must reboot before upgrade
   sudo reboot

   # cleanup the unused components - this will recover some spaces
   sudo apt --purge autoremove

   # finally, upgrade the OS
   sudo do-release-upgrade
        
Personalize your console command by using "alias".
  • Edit your console profile,
   sudo nano ~/.bashrc
    
Then, add the following to the bottom of the file

    alias f2b='sudo iptables -S | grep f2b'
    alias f2b-log='sudo tail -n 30 /var/log/fail2ban.log'
    alias auth-log='sudo tail /var/log/auth.log -n 100'
    alias check-reboot='cat /var/run/reboot-required'

Finally, saves the changes.

No comments:

Post a Comment