Command Line Tips & Tricks for Ubuntu Server

  1. Find out whether a package is installed or not.
    $ dpkg -s [package-name]
    $ dpkg -s apache2-mpm-worker
    $ dpkg -l apache*
  2. List files in a package which has been installed.
    $ dpkg -L [package-name]
    $ dpkg -L python-openssl
    
  3. Check the reason a package was installed, ie. find out the reverse dependencies of a package
    $ apt-cache rdepends --installed [package-name]
    $ apt-cache rdepends --installed nano
    
  4. Update package list, then preform all upgrades.
    $ sudo apt-get update
    $ sudo apt-get upgrade
  5. Create new user and grant root privileges.
    $ sudo adduser newuser
    $ sudo usermod -a -G sudo newuser        # add to sudo group
  6. Delete a user.
    $ sudo userdel newuser
    $ sudo rm -rf /home/newuser
  7. List all users.
    $ cat /etc/passwd
    $ lastlog
  8. View current user uid and groups.
    $ id
  9. Add user to a group, eg: dialout group.
    $ sudo usermod -a -G dialout
  10. Change time zone.
    $ sudo dpkg-reconfigure tzdata
    $ less /etc/timezone            # verify timezone has changed
    $ sudo service cron restart    # restart cron so it pick up the timezone change
  11. Manual time synchronization.
    $ sudo ntpdate ntp.ubuntu.com
  12. Install time synchronization service.
    $ sudo apt-get install ntp
  13. Update hardware clock.
    $ hwclock --set --date '`date`'
  14. List all mounted and unmounted partitions.
    $ sudo fdisk -l | less
    
  15. List mounted partitions.
    $ df -h
  16. View size of a particular file or folder.
    $ du -h myfile
    $ du -sh myfolder
    
  17. Find the full path of a command.
    $ which [command]
    $ which python
  18. View appended data as a file grows.
    $ tail -f filename
    $ less +F filename
    
  19. Monitor a program status in real time.
    $ watch [command]
    $ watch who
    
  20. Get system environment.
    $ printenv
    
  21. Change server hostname.
    $ sudo sh -c "echo new_host_name > /etc/hostname"
    $ sudo /etc/init.d/hostname restart
    
  22. View information of the linux distribution.
    $ lsb_release -a
    
  23. Check the OS installation date.
    $ ls -ld /var/log/installer