Code Done Right!

Useful Tricks

Here you will find a collection of useful tricks you can use to make your work a bit easier. It does not matter if you work with Raspbian, Debian or Ubuntu

Find out your local IP address

The easiest way to find out your local IP address is to type the following command

ifconfig

Which will show you output similar to the below

IP address
Local IP address on eth0 – cable connection

Screenshot above shows the local IP address of eth0, which means that my Raspberry is connected via an ethernet cable to my router and the local IP address is 192.168.0.129

If you have more than one connection method, like Pi 4B has, both ethernet port and Wi-Fi, then all methods that are active will be listed, even if they are not used at the moment. You will simply see a connection that is not active, but ready.

  • eth0 – cable connection
  • wlan – Wi-Fi connection
  • lo – local connection, ignore for now

Better search engine for package repository

Default apt search functionality is not that great. With xapian indexing you can extend the search functionality.

Install it with the following command

sudo apt install apt-xapian-index

Rebuild your package repository and update xapian index with

sudo apt update
sudo update-apt-xapian-index

Now you can use axi-cache command to browse your repository with the following

axi-cache search $search_term

If you want to learn how to refine your search run

axi-cache --help

Check installed packages

If you want to see what packages you have installed on your system, run the following command

apt list --installed

If you want to search the list for a particular package you have to pipe the result to a different command – grep.

In order to check if you have apache2 package installed run the following command

apt list --installed | grep apache2

You can substitute apache2 with any string to search for a different package

Create a list of installed packages and install them on a different machine

Two commands below will help you create a file that contains a list of all installed packages on your system. Why would you need that?

  • You can mirror your system build on another machine if you would like to make a backup with fresh config files
  • You can help someone build an exact replica of your system
  • You are upgrading or changing the distribution that you are using right now, let’s say you move from Debian to Ubuntu – you just have to make a list and install packages!

It all can be done surprisingly easy

sudo dpkg -l | awk '/^ii/ { print $2 }' > ~/package-list

The above will create a file called package-list in your home folder that will have names of all packages that you have installed on your system. Copy that file to a system on which you wish to install said packages

sudo xargs apt-get install -y < ~/package-list

The above will simply ask the system to install everything from the file package-list on the system. If you want to add more packages then simply supply more names in the file, just make sure all names are in a separate line

Using a pipe

Pipeing is done by using the character |, this is not an uppercase i nor it is a lowercase l. It is a separate symbol on your keyboard. A vertical line. Look for it around the enter key.

It basically passes the standard output of a command on its left to a command on its right. Look at the following code

command1 | command2

This takes the standard output (a result) of command1 and passes it as an attribute to command2. It might not seem like much, but consider a example of browsing for installed packages above, I included it first for a reason.

You could list all packages that you have installed on your machine and manually look through it for a specific one or pass the output to grep command so that it shows only what you are looking for, or nothing as this is also information. Remember that lack of information IS the information as well – you have to judge for yourself.

Now consider a file with a list. Let’s say 200-something lines and this time you only know that it starts with some letter combination. Do you really want to look through the whole file or do you want to

cat file_name | grep xyz | sort

to search the file file_name for the string xyz and have the output neatly sorted alphabetically? Exactly. Pipes are great, learn to use them.

See who logged in recently

Logs provide a lot of useful information. They should be your go-to place for troubleshooting, as they will tell you what errors are present and what to look for online. One more use of logs is to see who managed to log in, and when. The following log file contains both failed and successful login attempts

/var/log/auth.log

Unfortunately output is usually quite long, as it records bots that try to access your server using logins and passwords from the most commonly used lists. To filter out successful logins run the following

sudo cat /var/log/auth.log | grep "Accepted password"

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.