Much to the chagrin of those who would like to malign the Linux operating system, it's actually quite easy to use.Thanks to modern GUI desktop environments and applications, anyone can jump into the fray and know what they're doing.
On the rare occasions when trouble arises, you might want to know a few commands to help you out.Theproblem is that there are so many commands available within the realm of Linux, which makes it challenging to know which commands are the best options.
Also: Thefirst 5 Linux commands every new user should learn
Sure, you can learn any of the commands that display system information (such as top, free, iostat, htop, vmstat, and iftop), but those tools will only get you so far.What is more valuable is skipping to the command that can really help you if something goes wrong.With that introduction out of the way, let's get to the commands.
Back when I first started using Linux,dmesgwas my best friend. Essentially,dmesgis used to examine all messages that are created after the bootloader phase of the kernel. In other words, you might find a clue for anything you could possibly troubleshoot right here.
Also: I'm a command-line pro and this is the best terminal app I've ever used, thanks to AI
Unlike thedmesgof old, you now have to run the command with sudo privileges, so:
sudo dmesg
This will print quite a bit of output you can scroll through, making it a bit challenging to find what you're looking for, and much of what you read will most likely seem like gibberish. Thegood news is that errors print out in red, so you can quickly scroll to find anything that might be wrong.
Also: 5 things to do with the Linux terminal on your Android phone - including my favorite
There's a way to make this even easier. Let's say you're experiencing an error, and you want to see if it is logged viadmesgas it happens. To do that, issue the command:
dmesg -w
This will display the output fromdmesgas it happens, so when an error occurs, you'll see it written in the terminal window and can troubleshoot from there.
Thedmesg command is a great place to start troubleshooting in Linux.
Speaking of following output, thetailcommand allows you to follow the output written to any log file. Let's say you're having issues with your Samba share and want to see what's happening in real time. The first thing you would want to do is find out which log file to read. In that case, you could issue the command:
ls /var/log/samba
In that folder you'll find a number of log files (for the Samba server and any/all machines connected to the share).Let's say I want to view the content of the Samba daemon log. For that, I would issue the command:
tail -f /var/log/samba/log.smbd
Also: 5 Linux commands for quickly finding the system information you need to know
As the errors happen, they'll be printed in the terminal. As you can see, I have an unknown parameter in my smb.conf file, namedshare modes. I can open that file, remove the parameter, restart Samba, and the error is no more.
Tail is a great way to view information written to a log in real time.
Remember, to get out of the tail command, you have to use the Ctrl+c keyboard combination.
For me,psis a gateway to other commands. Thepscommand displays a snapshot of any given current process. You could usepsto list every running process or feed it togrepto list only specific processes.
But what's it good for?
Also: Two tricks that make using the Linux command line a lot easier
Let's say you have an application that has crashed and won't close. You click that little X in the upper-right (or upper-left) corner of the window, but it just won't go away. The first thing you need to do is find the PID of that process so you can then take care of the problem. That's wherepscomes in handy. Butpsby itself isn't very helpful. Why? If you just runps, it will only list the processes associated with the terminal you're using. Instead, you need to use some specific options, which are:
ps aux
Also: How to schedule Linux commands - and when you should
The ps command is essential for finding information about applications that may not be behaving as they should.
This command prints out a lot of information, all of it in columns. You'll see several columns, but the ones you'll want to pay attention to are PID and COMMAND. With the information from those two columns, you can locate the process's ID causing you problems. Once you've found that process, you can then kill it.
Also: 5 Linux commands for managing users
If the output ofps auxis overwhelming, you can pipe that output togrepand list only certain processes. Let's say LibreOffice is causing you problems. You can list only those processes associated with LibreOffice like this:
ps aux | grep LibreOffice| grep LibreOffice
The killcommand is very powerful. When you have a stubborn application that has crashed and won't close (or hasn't crashed but is consuming too much memory), thekillcommand will force that application to close.
Also: How I automate basic tasks on Linux with bash scripts - and why you should try it
But to use thekillcommand, you must first have the PID of the application in question (which you locate with theps auxcommand). Let's say the PID of a wayward LibreOffice application is 604187. To kill that process, the command would be:
kill 604187
The app should close, and you're good to go.
Also: 5 Linux commands for quickly finding the system information you need to know
The systemctlcommand is not only good for starting and stopping applications; it can also help you troubleshoot. Let's say Samba isn't working as expected. Issue the command:
systemctl status smbd
The above command will list whether the service is running, its PID, the number of associated tasks, how much memory and CPU it's using, and the CGroups to which it belongs. Even better, if there are any issues with the process,systemctlwill give you the information you need to troubleshoot the problem further (usually with the help ofjournalctl).
Also: Why I use the Linux tree command daily -- and what it can do for you
There you have it. These five commands will serve as a great place to start with your Linux troubleshooting. Yes, there are quite a few more tools that are available, but for those just starting with Linux, you might want to know these commands first.
cd stands for "change directory" and allows you to navigate through directories. pwd stands for "print working directory" and displays the current working directory.
Use the commandls.
Use the commandmkdir.
The -doption creates only the top-level directory, whereas creating a parent directory separately ensures that all necessary subdirectories are created as well. If you use the-poption, it will create parent directories as well as the subdirectory.
Use the commandcd [directory_name].
../ refers to going up one level in the directory hierarchy, whereas ./ (dot/period) refers to staying in the same directory.
Use the commandtouch [filename].
The -eoption allows you to specify an editor to open in place of creating a blank text file.
Use the commandrm [filename]to delete a file andrm -rf [directory]to remove both the top-level directory and any child directory or file within.
Pipes allow you to pass output from one command as input for another command, enabling complex data processing pipelines.
Use > to redirect standard output and >> to append new output. Also, use << to read the contents of a file into a variable or pipe.