Introduction
Greetings everyone. Let’s start with a very basic guide on how to start your sysadmin journey. Whether you’re a curious newcomer or a battle-hardened sysadmin, mastering essential Linux commands is like acquiring a set of superpowers for efficiently navigating, controlling, and understanding your system. In the upcoming tutorials, we’ll be discussing how to create and maintain a server on your own. So learning these essential Linux commands will come in handy and will significantly enhance your productivity.
From managing files/ directories to monitoring processes or system resources, these commands are the building blocks of your journey ahead. You can try out these things on your terminal to get hands-on. Take careful measures if you see the ⚠ sign (warning sign) along with the command.
Basic Commands
List Files And Directories
ls
ls
is used to list files and directories in your current location. You can add options (or arguments) as a suffix to get more out of the command. E.g.: ls -a
. Even though there are so many such options that we can choose from, these are the most commonly used.
-a
to view all the files including hidden files (files that have names starting with a dot. etc…)-l
to show the files in a list view for detailed information-h
to show the file size in a human readable format. (E.g.: 1K 123M 3G etc…)
You can combine multiple options to get all the information at once. My favorite combination is given below (fig. 1).
ls -lah
Changing Directories
cd <directory>
You can use cd <directory>
to navigate through your file system. E.g.: cd /var/log
(no need to type angle brackets <>).
If you want to go up (back) a directory just use cd ..
Creating A Directory
mkdir <directory_name>
To make a directory in the current directory you are in you can use the mkdir
command. Pair this with options like -p
to create parent directories if they are not available. E.g.: if you want to create a directory ‘rabbit’ inside the ‘animals’ directory, mkdir -p /animals/rabbit
Creating a file
touch <file_name>
To make a file in the current directory use this. I don’t think you need any more explanation other than that. E.g.: touch grass.txt
Downloading a file
To download a file from a URL using the wget
command, you would use the following syntax:
wget <URL>
Replace <URL>
with the actual URL of the file you want to download. E.g.: wget https://animals.com/imported-grass.txt
Removing a file/ directory ⚠
rm <file_name>
rm -r <directory_name>
If you want to remove a file, just type file name along with rm
command. If you want to remove a directory, don’t forget to use option -r
as shown above.
Copying a file/ directory
cp <source_file> <destination_path>
cp -r <source_directory> <destination_path>
If you want to copy a file, just use cp
E.g.: cp grass.txt /animals/rabbit
If you want to copy an entire directory to another, don’t forget to use -r
option as shown above.
Moving or renaming a file/directory
mv <source> <destination>
To move something, use mv with the source name and then the destination path.
E.g.: mv grass.txt /animals/lion
Note that for renaming also you can use the same command. For renaming, simply provide the new name as the destination. E.g.: mv grass.txt meat.txt
sudo: Holy grail of commands ⚠
sudo
Gain superuser privileges with this command, short for “Superuser Do”
sudo rm -r /animals
Just don’t forget, as Uncle Ben once said, “With great power comes great responsibility”. RIP his soul (fig. 2).
Intermediate commands
Updating and Upgrading Packages
Regularly updating your system ensures that you have the latest security patches and software improvements. Use the following command to update and upgrade your packages:
sudo apt update && sudo apt upgrade -y
This is a good place to teach another new thing. Take note of the &&
command in the middle. Using that we can combine multiple commands and execute them all at once. This will come in handy every now and then.
Managing Packages
Installing and removing software packages is a fundamental task on any Linux system. Here we’ll be using the apt package manager. Use these commands to work with packages:
# Install a package
sudo apt install <package_name>
# Remove a package
sudo apt purge <package_name>
See what I just did there? If there is a #
symbol in front of a line, it means that the entire line is a comment. You are not required to type those. Those are purely for taking notes.
Rebooting the System
When necessary, restart your system with:
sudo reboot
Checking Disk Usage
Things will clutter up eventually. You will feel like it’s “way” impossible to figure out where all the space went. I wish I had known this command when I was learning sysadmin in my internship. So I don’t wanna put my readers in a hole with no way out. So here is another crucial command, monitor disk space usage on your system with these commands:
# Check overall disk usage
df -BM
# Check directory size
du -hs </path/to/dir>
System Monitoring with “htop”
Just like your system disk space, it’s a good thing to monitor your system vitals once in a while when you are a beginner trying out different stuff. To keep an eye on system resource usage we will be using htop
tool.
# Install htop
sudo apt-get install htop
# Run htop
htop
This will open up the system monitor as shown below (fig. 3). You can use or + to close System Monitor.
Advanced commands
File Permissions ⚠
Once in a while you might have to modify file permissions. So here are the commands:
chmod <permissions> <file_or_directory>
# for directories and their contents inside (recursive changes)
chmod -R <permissions> <file_or_directory>
Here permissions are represented as numbers or in symbolic notation. To make things digest let’s focus on numbers at this stage. E.g.: chmod 775 /animals/rabbit
As in the above example, permissions are typically represented by a 3-digit string, where each digit-column represents a category: user, group, and others.
- User (Owner): The user who owns the file or directory.
- Group: Users who belong to the group associated with the file or directory.
- Public: Public permissions.
The value for each digit-column can be calculated as the sum of the values associated with the respective permissions.
User (Owner) | Group | Public |
---|---|---|
Read (4) | Read (4) | Read (4) |
Write (2) | Write (2) | Write (2) |
Execute (1) | Execute (1) | Execute (1) |
Suppose you want,
- Only read permissions for “User” (= 4)
- Read and write permissions for “Group” (= 4+2)
- No permissions for “Public” (= 0)
This combination will result in 460
as the required 3-digit permissions. E.g.: chmod 460 /animals/rabbit
It will be confusing at first. But with some hands-on you will get it eventually. Don’t forget to bookmark this page https://chmod-calculator.com/ to get your required calculations at ease.
File Ownership ⚠
In Linux and Unix-like operating systems, each file and directory is associated with an owner and a group. Understanding file ownership is essential for managing access rights and security within a system. Let’s understand the concept of file ownership, including users, groups, and how to manage them effectively.
chown <new_username>:<new_group_username> <directory>
# for directories and their contents inside (recursive changes)
chown -R <new_user>:<new_group> <directory>
Let’s change ownership of our animals
directory to a farmer
. E.g.: sudo chown -R farmer:farmworkers /animals
Compressing and Extracting Files
To compress files or a directory use the following command:
# Compress files
zip -r my_archive.zip <directory_to_archive>
Once compressed there should be a way to extract the files. Typically you will come across multiple types of zipped files when dealing with servers. So in order to extract them use the following commands. Note the file type and use it accordingly.
# Extract files
unzip file_name.zip
tar zxvf file_name.tgz
tar -xvf file_name.tar
tar --use-compress-program=unzstd -xvf file_name.zst
Ok, now the basics are over (even though the last title was advanced), let’s move on to another serious stuff.
Using Nano Text Editor
When it comes to editing files inside Linux distros, there are two major players Vim and Nano editor. Nano is a more user friendly way to start things (fig. 4). Why? well Nano welcomes you with open arms and simple exits, no ‘Houdini’ skills are required
Opening and closing the Nano editor is way simpler than you think.
nano <destination_file_path>
Just type the file path along with nano
prefix. E.g.: nano /animals/rabbit/grass.txt
You can make whatever changes to your file and then to save the changes simply hit + and then to confirm the file name. Then to exit the file hit +
(Optional)
I like to customize my Nano editor by enabling line numbers in Nano editor. This will be helpful in fixing issues/ bugs as we go on since the errors will appear with the exact line number. Customize your text editor settings and enable line numbers in Nano.
nano ~/.nanorc
The above command will open Nano editor default settings. Add the following in a new line to enable line numbers.
set linenumbers
Save the file by hitting + and then . Finally + to exit the file.
Wrap-Up
Mastering these essential Linux commands empowers users to efficiently manage their systems, perform file operations, monitor resources, edit files etc… Whether you’re a beginner or an experienced Linux enthusiast, these commands will be the foundation for your journey ahead. Experiment with these commands to gain confidence in your Linux skills and enhance your productivity. Let’s meet in a future post with all these commands.
References
- Larson, Q., (2017). One out of every 20,000 Stack Overflow visitors is just trying to exit Vim. [Online] Available at: https://medium.com/free-code-camp/one-out-of-every-20-000-stack-overflow-visitors-is-just-trying-to-exit-vim-5a6b6175e7b6 [Accessed 18 7 2023].
- Wallen, J., (2010). Linux 101: Introduction to sudo. [Online] Available at: https://www.linux.com/training-tutorials/linux-101-introduction-sudo/ [Accessed 20 7 2023].