Introduction
This guide is the first in a series aimed at teaching the basics of Linux, starting with how to use the terminal, the command line, and executing commands. For beginners, becoming familiar with the terminal is essential as it's the primary method of interacting with a Linux server.
To fully benefit from this tutorial, having access to a terminal is necessary.
Terminal Emulator
A terminal emulator enables the use of the terminal within a GUI. Most users operating a GUI-based OS will need a terminal emulator to access the Linux server functionalities.
Commonly Used Terminal Emulators:
- Mac OS X: Terminal(default), iTerm 2, Alacritty, Kitty.
- Windows: ConEmu, PuTTy, Kitty, Terminus, Babun, Xshell.
- Linux: Gnome Terminal, Konsole, XTerm, Alacritty, Kitty.
These emulators typically support features like tabbed windows and text highlighting.
The Shell
The shell is a command-line interface that interprets user commands and scripts, directing the operating system on how to execute them. Popular shells include the Bourne-Again shell (bash), Z shell (zsh), and Fish, each with unique features and command interpretation methods.
Note: On this tutorial we'll be using bash but I'll also include fish examples, if you never heard of fish before I strongly recommend giving it a try.
This tutorial utilizes bash, the default shell for most Linux distributions, including Ubuntu, Fedora, and RHEL.
The Command Prompt
Upon logging into a server, users are greeted by the Message of the Day (MOTD) followed by the command prompt. Here, users can issue commands. The prompt usually displays the username, hostname, and the current directory, and ends with a prompt symbol ($ for regular users and '#' for root).
user@project:~$
Here is a breakdown of the composition of the command prompt:
- user: The username of the current user.
- project: The hostname of the server.
- ~: The current directory. In bash, which is the default shell, the ~, or tilde, is a special character that expands to the path of the current user’s home directory; in this case, it represents
/home/ilia
. - $: The prompt symbol. This denotes the end of the command prompt, after which the user’s keyboard input will appear.
Here is an example of what the command prompt might look like, if logged in as root and in the /var/log directory
:
root@webapp:/var/log
The command prompt ends with a '#' symbol for the root user, which indicates a superuser account in Linux. This account is equipped with unrestricted access to perform any administrative task across the system, essentially holding all permissions to manage server operations.
Running Commands
Commands are executed at the command prompt by entering an executable file name, either a binary or a script. Linux comes with numerous built-in commands for system navigation, software installation, and configuration.
Example Usage:
Running cd without arguments returns the user to their home directory. ls lists files in the current directory.
// program to find the factors of an integer
// take input
const num = prompt('Enter a positive number: ');
console.log(`The factors of ${num} is:`);
// looping through 1 to num
for(let i = 1; i <= num; i++) {
// check if number is a factor
if(num % i == 0) {
console.log(i);
}
}
Output
…
grub-mkrescue sdiff zgrep
grub-mkstandalone sed zipdetails
grub-mount see zless
grub-ntldr-img select-editor zmore
grub-render-label semver znew
grub-script-check sensible-browser
ip displays usage information when run without arguments.
$ ip
Commands can include arguments to specify operations and options to modify command behavior, such as ls -la /home
to list detailed information about files in the /home
directory.
Environment Variables
Environment variables affect command execution and process behaviors. Users can view and set these variables in their session. For example, modifying the PATH variable allows the shell to locate executable files in additional directories.
Example Commands:
env to view all environment variables.
echo $PATH
to display the PATH variable.
export PATH=$PATH:/opt/app/bin
to add a directory to the PATH.
Setting environment variables typically affects only the current session unless changes are made permanent in configuration files.
This overview covers the fundamental aspects of using the Linux terminal, preparing users to connect to a Linux server and begin exploring more complex commands and scripts.