Linux for Network Engineers: Basic Linux Commands

In this post we are taking a step back from networking on Linux and talking about some basic commands that are necessary to  navigate ourselves around it. Although we’ll be coming across many more commands along the way, these are some basics that are the most helpful to know. Let’s jump into the commands:

Important Linux Commands to Know

File List (ls)

The command “ls” lists all files in a directory. Here is how you can use it, along with some examples.

I see that there are three files in this directory “news.txt,” “notes.txt,” and “temp.” If I require more information about these files, I can add the “-l” flag which stands for long:

Let’s break this down.

This part tells us what type of file it is, and what the permission rights are. The first dash is empty for “news.txt” and “notes.txt” because they are files. “temp” is a directory and this is noted with a “d”. Then there are three triplets of letters that tell us what file permissions the user, the group, and everyone else has. For example the “temp” directory has:

rwx: read, write, and execute permissions for the directory owner (which is the user pi)

r-x:read, execute permissions for the directory group (which is the user pi)

r-x:read, execute permissions for everyone else

The number next to the file permissions dictates how many links each file has. You can think about this as the number of files and directories inside a directory.

Next is the file owner and the group of the file.

The following numbers are the file sizes in bytes (directories show up with the same number and that doesn’t represent the total of the files and directories they include).

And finally we have the last time the file or directory was modified followed by the name.

If you add the “-a” option, which stands for all, as well you get the following:

As you can see now this reveals some files and directories that start with a dot “.” in their name. These are hidden, and they are only listed when we use the “-a” flag.

Command Manual (man)

If you are looking to find information around the options of a command, I’d say Google is your friend. However, every Linux command has a manual with all of the nitty gritty details, which can be accessed if you type:

So, if you are looking for a command’s information details at your fingertips, man becomes your second best friend.

Directory Creation and Navigation (mkdir, cd, pwd)

To create a new directory, type the make directory command:

To get into the new directory, type the change directory command:

Hint: if you forgot which directory are you in, you can print the working directory with the “pwd” command:

With the “ls -la” command we see on top two mysterious listed directories with names “.” and “. .” These are shortcuts that help us refer to the current directory and the parent directory. For example, if you want to change to the parent directory you type:

File Manipulation (cp, mv, rm)

To copy a file, type:

To rename or move a file, type:

To delete a file, type:

Note: Linux doesn’t have a recycle bin, so if you delete a file, it’s gone without warning and without an easy way to bring it back.

Root commands (sudo)

Now, let’s try to change the ownership of the file “notes.txt” from user root to user pi. As a reminder here are its file permission:

Which means,  the owner of the file is the root user. Here is how you change ownership:

We got an error message that says that we don’t have the permission to do that. We either need to login as root, or use the “sudo” prefix as follows:

As you can see, the owner now is the user pi, and the group has remained root. “sudo” gives the ability to user pi to run a command as superuser provided that user pi has been given that permission. It stands for “superuser do” and you have to be careful when you use it: with great power comes great responsibility!

The commands you will come across and use are countless, but these few commands that I have listed are the bare minimum that you need to know to navigate a Linux host comfortably. Happy troubleshooting!

NetBeez Tech Overview and Benefits