Basic Linux Commands – CompTIA A+ 220-1002 – 1.9

Most features of the Linux operating system can be managed from the command line. In this video, you’ll learn about some of the most popular Linux commands along with demonstrations of each command on a live Linux distribution.

<< Previous Video: Linux Tools Next: Physical Security >>

Using the Linux command line is very similar to using the command line in Windows. To start the command line in Linux, there’s probably a utility called Terminal, XTerm, or something similar that will bring up a command line view in the operating system. The commands you’ll run at the command line in Linux are similar, if not identical, to the commands in MacOS. This is probably because MacOS is derived from a version of Unix called BSD that stands for Berkeley Software Distribution and Linux was created to be very similar to Unix.

This particular video will focus on commands in Linux, but if you’re running MacOS, you’ll see that many of these are identical. If you’re not running Linux on your desktop, but you’d like to follow along with this video, you can download a live CD version of a Linux distribution or install Linux onto a virtual machine. In this video, I’ll be using a virtual machine that’s running Ubuntu. So you could try downloading that distribution and you should be able to see similar information to what I’m seeing on my screen.

Many versions of Linux include a built-in help called man that stands for online manual, and you’re able to use the man command in conjunction with the command that you’d like to know about. For example, if you’d like to know more about the grep command, you can use man grep to pull up the online manual on grep.

If you’ve used Windows, you know the dir command provides a list of the files in the folders in a particular directory. In Linux this command is ls for list directory contents. This ls command will list out the files and directories, and if your terminal supports it, it may even be color coded. In my example, anything that’s a directory is a blue color and anything that is a file is a white color. There’s a more detailed version of ls that gives you more information about file ownership and permissions, and you can see this by typing in ls dash l.

You’ll find that if you use this dash l option, you may get pages of information go by. One way that you can paginate this and many other Linux commands is to put the pipe symbol and then put the word “more.” This will allow you to see the output, but it will stop after every page and allow you to step through this one page at a time. If you’re in the middle of one of these pages and you would like to exit back to the command line, you can press the Q button or use Control-C.

The command to change from one directory to another is the cd command. This is exactly the same as the cd command that’s used in Windows. Instead of using backslashes in Windows, however, Linux uses forward slashes. So as you’re typing in all of the directory names, make sure you separate them with the forward slash, which is the one next to the question mark on the keyboard. For example, to use the cd command, we type in cd and the name of the directory. If you want to move to the var log directory, you would type in cd space slash var slash log.

Here’s my Ubuntu Linux desktop, and on this version of Linux there is an option at the bottom to show the applications. And the application that I’d like to run is the Terminal. And now I’m at the command line of the Linux operating system. Every Linux distribution has a slash var slash var directory, so we’ll use the cd command with the slash var and slash log to move to that directory name. And if we perform an ls, we can view all of the files that are listed in that particular directory.

If we’d like an expanded view of this directory, we can use the ls command with the dash l option and view exactly the same files but with much more information on the screen. This view shows you the permissions associated with this file, and we’ll learn more about those permissions in just a moment– the owner of the file and the group associated with the file. We can see the size of the file and a timestamp and then finally the name of the file.

There were a lot of files in that log directory, and there may be information that we would like to find in one of those log files. One of the way that you can search through a text file is to use the grep command. This will search through one or many files at a time to find a piece of text that you may be looking for. To use grep, you would use grep, the pattern that you’re looking for, and the name of the file. For example, if you were looking for the word “failed” within the file auth.log, you would use grep failed auth.log to be able to identify all of the lines of information in that file that match the word “failed.”

There’s a file in this log directory that I would like to look through to find the term “Bluetooth.” The file I’m looking through is the dmesg file. If we use the more command and dmesg and hit Enter, you’ll see as I hit the space bar that there are pages and pages. I have already paged through three or four of these, and I’m only 9% through this particular file. I’m going to hit the Q button to exit out of that view, and now I need to do a much more detailed search.

So I’m going to use the grep command. I’m going to choose to look for the term “Bluetooth,” and this is case sensitive so you need to make sure you put it in exactly the way it is in the file. And then I’m going to specify the dmesg file. Now the results I get are only these particular lines. Those are the only lines in that entire file that happen to match the term “Bluetooth” that I was looking for.

Up to this point, we’ve been using commands that can be run by anybody on the Linux system, but there are some commands that you’ll run as a system administrator that need elevated rights. To be able to run those commands, you need to know about su and sudo. The su command is one that changes the shell to be one of a super user. The super user is very similar to the administrator account that would be in Windows. You can also use the su command to be able to change to the same permissions as a different user.

If you don’t use any user name with the su command, it assumes that you want to be in the shell as the root user. You’ll continue to run with these elevated rights until you exit from this particular shell by using the exit command. If you just need to execute a single command as the super user or the root user, you can use the sudo command. This will allow you to run the command in elevated rights, and as soon as that command is over, you’ll be back to your normal rights and permissions.

One of these commands that needs elevated rights is the shutdown command. You may be working on a remote device and you want to be able to reboot that device during the troubleshooting process. The shutdown command is very similar to the one you might run in Windows, although the syntax is slightly different. If you use the command sudo, shutdown, and 2, you will shut down that system, turn it off after two minutes have gone by. If you want the system to be rebooted, you’ll need to use the shutdown command with the dash r option.

So sudo shutdown dash r 2 means that it will shut down in two minutes and then reboot the system. On some Linux versions after running the shutdown command, it will sit at a prompt and wait for that shutdown to occur. And if you wanted to completely stop that process from occurring, you can use Control-C. On other versions of Linux, you may have to run another shutdown command, shutdown dash c to be able to cancel that shutdown process.

If I wanted to reboot the Linux system I’m connected to, I can use the sudo command, the shutdown, I want to use the dash r option to reboot it, and I’ll choose two for the number of minutes. When I press Enter since I’m using sudo, it wants to know my sudo password. I’ll put that password in, and it says the shutdown has now been scheduled. “Use shutdown dash c to cancel.” And I would like to cancel this since we have much more to do in this video. So I’ll use the sudo command shutdown dash c, and now that shutdown process has been canceled.

When you’re working at the command line, it can sometimes be confusing as to exactly what directory you happen to be working in. One way that you can view this is to use the pwd command. It stands for print working directory, and you can use this at any time to see exactly the directory name you happen to be in.

There’s a command with a similar name that has a very different function. This command is the passwd command. That changes passwords for accounts, and as the system administrator, you may be changing passwords on accounts very often. To change the password on your account, you can simply type in passwd. Or if you’d like to change the password on another account, you can use passwd with that username.

Let’s run the pwd command in Linux. I’m in my Ubuntu desktop again. I’m going to click on the Show Applications and start the Terminal. When you start the Terminal, you’ll notice that by default it does show you the name of the working directory as part of the command line option. But you’ll notice the working directory that it shows is a tilde. A tilde is a shortcut for your home directory. If you’d like to see the actual directory name, we could use pwd for print working directory, and the full name of this directory is slash home slash professor.

The passwd command is quite different. If I run that command, it says that I’m changing the password for my current account name, which is professor. And at this point, I would put in my current password and a new password, and it would change the password associated with this account.

In the Linux operating system, if you want to rename a file from one name to another you would use the move command or mv. The syntax for the move command is mv, the source file, and the destination file. So if you wanted to change a file from first.txt to second.txt, you would use mv first.txt second.txt, press Enter, and then the file name would be changed from first to second.

I have a file in my Documents folder that I would like to change from first.txt to second.txt. If I perform an ls, you can see there are many directories in this particular computer. The Documents folder is the one that has my file, so I’m going to type in cd space doc and then hit Tab to auto-complete.

Inside of the Documents folder is an audio and video folder, and then there’s first.txt is the file that I would like change. We’re going to type in mv first.txt, and we’re going to change that file name to second.txt. And if I hit Enter, I simply get a prompt back, but if I perform that ls command again, you can see that that file has indeed been renamed to second.txt.

If you need to make a copy of a file, you would use the copy command, which is cp. The copy command has the syntax very similar to the move command. It’s cp, the source file name, and the destination file name. If I wanted another copy of that second.txt file, I can use the cp command, type in second.txt, and we’re going to create another version of this file called third.txt. And if I hit Enter and then perform another ls, you will see I have a second.txt and a third.txt, and you can see these files are exactly the same in its size.

To be able to remove files in Linux, you would use the rm command. So use rm and the name of the file, and that file will be deleted. You can’t remove a directory unless that directory is empty, so you would have to remove out all of the files in that directory and then finally remove that directory. Or you can use the rm with the dash r option to remove the directory and everything within that directory as well.

Now that we’ve copied the second.txt file to third.txt, let’s remove that second.txt file. I’ll use the rm command, second.txt, and we’ll hit Enter. And it asks if I would like to remove this write-protected regular file, second.txt. I do, and if we perform the ls again, you will see the second.txt. file is no longer on my drive.

If you want to make a directory, you can use the mkdir command, which is very similar to the make directory command in DOS or Windows. You would use the mkdir command and the directory that you would like to create. So if you want to create a directory called “notes” use mkdir notes.

In the Documents directory that we’ve been looking at, there is already an audio folder and a video folder. I’d like to also create a directory for notes. So if we use mkdir notes and then perform an ls dash l, you can see that I now have a new folder that’s been created called “notes.”

When we looked earlier at the ls command, we were able to see the file permissions area of that particular command. It’s over on the left-hand side. And it’s a series of rs, ws, xs, and they’re all in a particular order on the screen. These are referring to the mode of these particular objects, these objects being files on our system, and the r, the w, and the x are associated with read permissions, write permissions, and execute permissions. In Linux, you can have separate permissions associated with the owner of that particular object, the group that owns that object, and then everyone else who may have access to that object.

Let’s say that we were changing the mode or their permissions to a particular file. To be able to do that in Linux, use the chmod command. So chmod and then the mode you would like to use, which is a series of numbers or letters, and then the name of the file. So chmod 744 Let’s break down what this particular command would do with the permissions for

If we look at the command mode that we are setting is 744. The first number is the permissions that will be associated with the user who owns the file. The second number is associated with the group that is associated with the file, and the last number is associated with everyone else who is not part of that user or group.

The number is actually a representation of reading, writing, and execute. And if we were to look at this, you could see it is the same as a binary representation if you know how to do binary math. For example 0 is no permission access, which means you don’t have access to read, write, or execute, and the highest level of access is a 7, which is turning on read, write, and execute access for that user, that group, or everyone else. Let’s look at the permissions that we set for

Looking at this output of the ls command, we can see that the owner of the file is professor and the group associated with the file is staff. We know that we assigned the mode 7 for the user or owner, so the professor user has mode 7, which allows professor to read, write, and execute this file. The 4 in the middle is associated with the group staff. So anybody in the staff group would be able to perform the same function as a mode 4, which is read only.

And in this particular case, we also have 4 as the last number, so anyone who is not the owner of the file or in the same group as the file would also have access 4, which would be a read only. Although at first it seems like a jumble of letters have been placed on the screen next to the user, the group, and the file name, after some practice you do find that you’re able to pick out very quickly what the rights and permissions are and you can easily view them as you’re viewing the ls dash l command.

There are also a number of shortcuts that you could use with the chmod command. We use chmod with the number 744 to specify that the user had read, write, and execute. That was our 7. The group had read only. That was the middle 4. And everyone else also had read only. That was the last 4. But you could also abbreviate some of this access by using some of the letters.

For example, chmod a means that we are going to associate a permission for all users, and the dash w means no writing to the particular file that we’ve specified. Another example of this is to use the u, which means the user or owner of the particular file. So we could chmod for the user with a plus x, which means that we’re going to enable execute access for the user for

Let’s look at the files that are in my Documents folder. We have this third.txt file, which is both in the professor name and professor group. And you can see that we have an rw with no x, we have an r and then no options, and then finally the last three options have nothing listed either. This means that the owner has read and write access to the file, the group has read access to the file, and then everyone else has no access to this file. We can use the chmod command to change these parameters.

Let’s use chmod with 7 to specify that the owner would get all access to the file. We’ll use 4 to change this to be read, write access for the group, and we’ll use 0 to maintain that there would be no access for anyone else. And then we’ll specify third.txt. If we look at the permissions again, you’ll see that they have change to rwx for the owner, r-blank-blank for the group which is read only, and then no access for anyone else.

Not only can you change the permissions associated with the user and group, you could change the user and group who owns that particular file. You would do this with the chown command. To use chown, you would type in sudo, chown, and then the owner colon group that you’d like to associate to the particular file. For example, if you use sudo chown professor, it will change the ownership of this file for the user to be professor.

On my computer, I have a third.txt file. And you can see the owner of the file is root and the group associated with the file is staff. I’d like to change the ownership of the file to be my name, professor, instead of the user root. So I’ll perform a sudo. I want to see chown this to professor, and then the file name is third.txt. And now if I look at the file, you can see that the username associated with the ownership of this file is now set to professor.

As the system administrator, there’ll be times when you need to look at the network configuration of a particular Linux system. If this Linux device has a wireless adapter card, you want to use the iwconfig command to view or modify any of the wireless configuration settings. This will show you SSID information, the channel that’s in use, and other wireless settings as well. To be able to modify these, you would use the iwconfig command associated with the name of the particular wireless adapter, and then you can modify this, for example change the SSID to studio-wireless.

To view network information associated with a wired network interface, you want to use the ifconfig command. You would use ifconfig and the name of the adapter and it will provide information about IP addressing, subnet masking, and other details associated with IP on this interface. With some Linux distributions, the ifconfig command is not available by default. Those versions of Linux have migrated to the IP command. So to view information that’s similar to the ifconfig command, you would use IP space address.

If you do run into a Linux distribution that doesn’t have the command that you’re looking for, you might install that version of the program using the apt-get tool. This is the Advanced Packaging Tool. This allows you to install and remove applications from your Linux distribution. To be able to install an application, you would sudo the apt-get, install, and then the name of the application you’d like to install.

The distribution of Ubuntu that I’m using doesn’t have the ifconfig command as part of the distribution, but if I sudo apt-get and install net-tools, I’ll be able to install the package that includes the ifconfig command. Now, if I use ifconfig and hit Enter, the command is now available and I can view the IP information associated with this computer.

If you’ve ever used the Windows Task Manager then you’re probably familiar with the information you would get from the ps command inside of Linux. This will allow you to view all of the processes running on this Linux system and the process ID associated with those processes. To view the processes just for the current user who’s logged in, you can simply use the ps command. But if you want to view all of the processes running on the computer, you can use ps dash e. This will probably be pages of information, so you may want to pipe ps dash e to the more command.

If I just use ps, it will show me all the processes running for this single user on this Linux workstation. And you can see I am at the bash shell and I just ran the ps command. So it showed me the process of me running the actual command to view the process. To view all of the processes on this computer, I can use ps dash e and view all of the processes. But you can see this was many pages of information, which is why ps dash e piped to more may give you a little more time to flip through all of the pages one at a time to see exactly what’s going on.

Linux uses many different configuration files to be able to change the configurations of certain applications. To be able to edit those files, you may want to use a full screen editor like vi. This is the visual mode editor that allows you to view a full screen editor with copy, paste, and many other capabilities. To start the editor, we would use the vi command and the name of the file. So to edit file, I would use vi

Once I’m inside the editor, I can insert text by pressing the i key and then typing the text. Once I’m finished with the insert process, you press Escape to get out of insert mode. Once you’ve finished your editing and you would like to save the file and quit the vi editor, you would type of colon to go into command mode, type a W which would write the information in this file or save the file, and a Q, which will quit the editor.

On my computer I have this file third.txt, and if we look at third.txt, you can see there is a lot of text in this file. To edit this file I can type in vi third.txt, and now I’m in a full-screen editor. And if I hit the i command, I can insert information into this file. We’ll type in “Inserted Text.” To exit from this insert mode, I would hit the Escape key, and now if I would like to write this file and quit the editor, I can press colon wq. And now if I look at third.txt again, you can see that my inserted text has been saved in this text file.

If you’re a system administrator who is imaging partitions on a Linux device at the command line, you’re probably going to use the dd command. This is a command that will convert and copy a file, but it’s also used to backup and restore images for an entire partition. The dd command includes some parameters, such as if which specifies a source file name, of which is the output file name, and then any options.

For example, to create an image of a partition you can use dd with if specifying slash dev slash sda as the name of that partition and then of to specify the location for the image file, which in this case would be slash tmp slash sda-image.img. To be able to restore that partition, we would simply reverse this information by specifying dd. The input is going to be the name of the image file, and the output is going to be the name of the partition or slash dev slash sda.

As a system administrator, there will certainly be times when you need to terminate an application that’s running on your Linux operating system. To be able to close the program, we would use the killall command. For example, if you wanted to kill all instances of Firefox, you would use sudo, killall, and Firefox. There is also a graphical version of this you can use called xkill, but if you’re working at the command line, you may just want to use the kill command and the process ID associated with that process. I do have Firefox that’s running here in the background, so if we use sudo, killall, and Firefox, and run that, you can see that it instantly kills all processes associated with Firefox.