No. 7. LINUX Basics for Hackers (Managing User Environment Variables)

11 July 2023 13 minutes Author: Lady Liberty

Efficient management of environment variables in Linux

In Linux, user environment variables play an important role in configuring the operating environment and behavior of programs. Effective management of these variables allows you to customize the system to your needs and ensure convenient use of programs. In our complete guide, we’ll take a detailed look at the process of managing user environment variables in Linux and show you how to configure these variables effectively. Understanding User Environment Variables: User environment variables in Linux are special values that affect the behavior of programs and the user’s working environment. Understanding these variables and their impact allows you to better configure your system and use programs with greater comfort. Managing User Environment Variables: We will look at different ways to manage user environment variables in Linux.

Using the export and unset commands, you can set and change the values of environment variables. We’ll also show how to configure these variables using configuration files such as ~/.bashrc or /etc/profile. Popular Environment Variables and Their Uses: We will cover some popular user environment variables in Linux like PATH, HOME, LANG and others. You will learn how to properly configure these variables for convenient use of programs and languages on a Linux system. Final Guidelines: Managing user environment variables in Linux is an important part of system setup and user comfort. Using our comprehensive guide, you’ll gain all the information and skills you need to effectively configure user environment variables on a Linux system. Customize your system as you need and enjoy the convenient use of the applications.

View and change environment variables

You can view all default environment variables by typing env in your terminal from any directory.

Example:

kali >env
XDG_VTNR=7
SSHAGENT_PID=922
XDG_SESSION_ID=2
XDG_GREETER_DATA_DIR=/var/lib/lightdm/data/root
GLADE_PIXMAP_PATH=:echo
TERM=xterm
SHELL=/bin/bash
­­snip­­
USER=root
­­snip­­
PATH=/usr/local/sbin :usr/local/bin:/usr/sbin:/sbin/bin
­­snip­­
HOME=/root
­­snip­­

Environment variables are always capitalized, as in HOME, PATH, SHELL, etc. These are just the default environment variables that come to your system. The user can also create their own variables, and as you’ll see, we need another command to include them in the output.

View all environment variables

To view all environment variables, including shell variables, local variables, and shell functions, such as any user variables and command aliases, use the set command. This command will list all the environment variables unique to your system, which in most cases will get you out of the way until you can see it all on one screen. You can ask to see each variable, line by line, in a more accessible way, using set and a pipeline to the more command, as follows:

kali >set|more
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdlist:complete_fullquote:expand_aliases:extglob.....
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
­­snip­

The list of variables will now fill one screen, line by line, and then stop. When you press ENTER, the terminal jumps to the next line, going to the next variable, so you can scroll by pressing or holding ENTER. As you’ll recall from Chapter 2, whenever you use the more command for output, you can type q to quit (or quit) and return to the command line.

Filtering by certain variables

While using set   with more counts produces more manageable results than going through the huge chunk of variable names you get with set alone, it can still be pretty boring if you’re looking for a specific variable. Instead, you can use the grep filter command to find the variable of interest.

Let’s take the HISTSIZE variable as an example. This variable contains the maximum number of commands your command history file will store. These are any commands you previously typed at the command line in this session and can be recalled using the up and down arrow keys. Note that HISTSIZE does not store the commands themselves, but only the number of them that can be stored.

Concatenate the installed output with grep to find the HISTSIZE variable, for example:

kali >set | grep HISTSIZE HISTSIZE =1000

As you can see, this command finds the HISTSIZE variable and displays its value. The default value for this variable is probably set to 1000 on your system. This indicates that the terminal will store your last 1,000 commands by default.

Changing the values of variables for a session

Now let’s see how to change the value of a variable. As mentioned,  the HISTSIZE variable contains the value of the number of commands to store in the history archive. Sometimes you don’t want your system to store past commands, perhaps because you don’t want to leave any evidence of your activity on your own system or the target system. In this case, you can set the HISTSIZE variable to 0 so that the system does not store any of your previous commands. Since this variable has a single value, changing it requires assigning it a new value in the familiar way shown in Listing 71.

kali >HISTSIZE=0

Listing 71: Changing the value of HISTSIZE

Now when you try to use the up and down arrow keys to recall your commands, nothing happens because the system no longer stores them. It’s hidden, although it can be inconvenient.

Constant changes of variable values

When you change an environment variable, that change only happens in that particular environment; in this case, that environment is a bash shell session. This means that when you close the terminal, any changes you made are lost and the values revert to their default values. If you want to make the changes final, you need to use the export command. This command exports the new value from your current environment (bash shell) to the rest of the system, making it available in every environment until you change and export it again.

Variables are strings, so if you’re being careful, it’s a good idea to save the contents of a variable to a text file before changing it. For example, since we are going to change the PS1 variable that controls the information displayed in the tooltip, first run the following command to save the existing values to a text file in the current user’s home directory:

kali >echo $HISTSIZE> ~/valueofHISTSIZE.txt

That way, changes can always be undone. If you want to be even more careful and create a text file with all the current settings, you can save the output of the installed command to a text file with the following command:

kali >set> ~/valueofALLon01012017.txt

After you’ve changed a variable, as we did in Listing 71, you can make the change permanent by typing export followed by the name of the variable you changed, as shown here:

kali >export HISTSIZE

Now  the HISTSIZE variable will still be set to 0 when you exit this environment and enter another environment. If you want to reset the HISTSIZE variable to 1,000, just type this:

kali >HISTSIZE=1000
kali >exportHISTSIZE

This piece of code will set your HISTSIZE variable to 1,000 and export it to all your environments.

Changing the shell request

Your shell prompt, another environment variable, gives you useful information such as the user you’re running as and the directory you’re currently working in. A typical shell request in Kali has the following format:

username@hostname:current_directory

If you are running as root, this will prompt the following by default:

root@kali:current_directory

You can change the name in the default shell prompt by setting the PS1 variable to a value. The variable PS1 has a set of placeholders for the information to be displayed in the tooltip, including the following:

  • \u The name of the current user

  • \h Host name

  • \W The base name of the current working directory

This is very useful if you have shells on multiple systems or are logged in as multiple accounts. By setting different \u   and \h values for different shells or accounts, you can tell at a glance who you are and what your current system is.

Let’s have a little fun and change the tooltip in your terminal. For example, you can enter the following:

kali >PS1=”World’s best hacker: #”

World’s Best Hacker: #

Now, every time you use this terminal, you will be reminded that you are the “World’s Best Hacker”. But any subsequent terminal you open will still have the default command line because  the PS1 variable only holds a value for the terminal session. Remember that until you export a variable, it is only valid for this session. If you really like this new command line and want to see it in every terminal, you need to export it.

Example:

kali >експорт PS1

This will make the changes persistent across sessions. How about a little more fun? Let’s say you really want your terminal to look like a Windows cmd prompt. In this case, you can change the query name to C: and save the \w so that the query reflects your current directory, as shown in Listing 72.

kali >exportPS1='C:\w>'
C:/tmp>

Listing 72: Changing the query and displaying the current directory. Your hint showing the current directory can be generally helpful, especially for a newbie, so it should be taken into account when you change the PS1 variable.

Changing the path

One of the most important variables in your environment is your PATH variable, which controls where on your system your shell will look for commands you type, such as cd, ls, and echo. Most commands are located in the sbin or bin subdirectory, for example

/usr/local/sbin or usr/local/bin. If bash does not find a command in one of the directories in your PATH variable, it will return a command not found error, even if the command exists in a directory and not in your PATH. You can find out which directories are stored in your PATH variable by echoing its contents.

Example:

Kali >echo $PATH /usr/local/sbin:usr/local/bin:/usr/sbin:/sbin/bin

These are the directories where your terminal will look for any command. For example, when you type ls, the system knows to look in each of these directories for the ls command, and when it finds ls, the system executes it. Each directory is separated by a colon (:) and don’t forget to add the $ content symbol to the PATH.

Adding to the PATH variable

You can probably see why it’s important to know what’s in your PATH variable: if you’ve downloaded and installed a new tool, say newhackingtool, in the /root/newhackingtool directory, you can only use commands from that tool when you’re in this directory because this directory is not in the PATH variable. Every time you want to use this tool, you have to go to /root/newhackingtool first, which is a bit inconvenient if you want to use the tool often.

To be able to use this new tool from any directory, you need to add the directory containing the tool to your PATH variable.

To add newhackingtool to the PATH variable, type the following:

kali >PATH=$PATH:/root/newhackingtool

This assigns the original PATH variable plus the /root/newhackingtool directory to the new PATH variable, so the variable contains everything it did before plus the new tools directory. If you check the contents of the PATH variable again, you will see that this directory has been added to the end of the PATH

As shown here:

Калі >відлуння $PATH /usr/local/sbin:usr/local/bin:/usr/sbin:/sbin/bin:/root/newhackingtool

You can now run newhackingtool programs from anywhere on your system, instead of going to its directory. The bash shell will appear in all the directories listed for your new tool!

How not to add to the PATH variable

One common mistake new Linux users make is to assign a new directory like /root/newhackingtool directly to the PATH variable like this:

kali >PATH=/root/newhackingtool
kali >echo$PATH
/root/newhackingtool

If you use this command, your  PATH variable will only contain /root/newhackingtool directory and will no longer contain system binaries such as /bin, /sbin and others that contain critical commands. When you then proceed to use any of the system commands, you will receive a command not found error, as shown below, unless you first change to the system binaries directory when running the command:

When >CD bash: cd: command not found

Remember to add to the PATH variable, not replace it. If in doubt, save the contents of the variable somewhere before changing it.

Creating a Custom Variable

You can create your own custom, user-defined variables in Linux by simply assigning a value to a new variable that you call. This can be useful when you’re doing more advanced shell scripts or find yourself frequently using a long command that you get tired of typing over and over again. The syntax is simple: enter the name of your variable, then the assignment symbol (=), then the value you want to enter in the variable.

As shown here:

kali >MYNEWVARIABLE

Hacking is the most valuable skill set in the 21st century”

This assigns a string to the variable MYNEWVARIABLE. To see the value in this variable, use the echo command and the $ content symbol with the variable name

How we did it before:

If >echo $MYNEWVARIABLE

Hacking is the most valuable skill set in the 21st century

Just like our system environment variables, custom variables must be exported to preserve them for new sessions. If you want to remove this new variable or any variable, use the Remove command. Always think before you delete a system variable, because your system will probably work much differently afterwards.

kali >unsetMYNEWVARIABLE
kali >echo$MYNEWVARIABLE
kali >

As you can see, when you type unset MYNEWVARIABLE, you remove the variable along with its value. If you use echo on the same variable, Linux will now return an empty string.

Summary

You may find the environment variables foreign, but it’s worth getting familiar with them. They control how your Linux desktop looks, acts, and feels. You can manipulate these variables to tailor your environment to your needs by modifying them, exporting them, and even creating your own. In some cases, they can be useful for covering your tracks as a hacker.

We used materials from the book “LINUX BASICS FOR HACKERS” written by William Pollock

Other related articles
Found an error?
If you find an error, take a screenshot and send it to the bot.