DEV Community

Cover image for Terminal Commands I Use On Linux for System Management - Part 1
dev_neil_a
dev_neil_a

Posted on • Updated on

Terminal Commands I Use On Linux for System Management - Part 1

Overview

This article will cover five of the tools I typically use when working on a remote (or local) system at the terminal, be that Linux or macOS.

I haven't included tools like cd, ls, more / less, apt etc. as I wanted to cover tools that are more targeted at administrative tasks and services management.

With that said, let's make a start with the first tool, SSH.

Secure Shell (SSH)

What is SSH?

Secure shell (SSH) is used to remotely access a remote system that runs an SSH server. You use it to gain command line access as if you were using a terminal directly on that system.

How to use SSH

To use SSH, the client will need to be installed on your system and the remote system will need to have the SSH server installed and the service running. Once they are both installed and the remote system has the service running, you can log into the remote system from you local system using the following command:

ssh username@remote-system-name-or-ip
Enter fullscreen mode Exit fullscreen mode

If this is your first time logging into that system via SSH, you will be asked to add the SSH fingerprint to the know hosts (a list of systems you have SSH'd into before). Type yes and press enter.

You will be asked for the users password. If certificate authentication is used, you might not get asked for a password but it depends on how that was setup.

Once done, you will be logged in and ready to go with a command prompt that looks similar to this:

SSH To Remote Server

Tmux

What is Tmux?

Tmux is a terminal multiplexer that allows you to create multiple consoles inside of a single terminal window.

An example of why Tmux is useful is that you could run a monitoring tool, such as htop in one console, use tail to view the bottom of a a log file in another and have a config file open that you are editing to fix an issue in another console.

How to use Tmux

To use Tmux, you will need to install it first as it's not normally pre-installed with most Linux distributions. The package name for Tmux is tmux on most distributions. Install it with the package manager used by your Linux distribution, such as apt on Debian or Ubuntu-based distributions.

Once installed, you can run tmux from the terminal as follows:

tmux
Enter fullscreen mode Exit fullscreen mode

Once loaded, you will see a green bar along the bottom and a command prompt like the below:

Tmux Default

This is the default window. You can now create multiple consoles in that window. There are two ways to add a console.

The first is to add one vertically. This is done by pressing CTRL+B and then pressing %.
The second is to add one horizontally. This is done by pressing CTRL+B and then pressing ".

To navigate between the consoles, press CTRL+B and then use the arrow keys to go between them.

A green box will be shown around the active console, along with the consoles cursor being shown.

In the below example, there are three consoles, with one vertical split (left side running nano) and then the right side has a horizontal split (top is running htop, bottom running the man page for tail). As you can see each console is actively running a different tool.

Tmux Three Consoles

Useful Keyboard Shorcuts:

  • CTRL+B then press % to create a new vertically-split console
  • CTRL+B then press " to create a new horizontally-split console
  • CTRL+B the an arrow key to switch between consoles
  • CTRL+B then press x to close the currently selected console (confirm by pressing y)

HTop

What is HTop?

HTop is a monitoring tool that can be used to monitor CPU, memory and display what processes are running. If you are a Windows user, think of it as task manager.

It is an alternative to top which has been used on Linux systems for a very long time. HTop is more graphically easy on the eyes than top.

How to use HTop

HTop, may or may not be installed on your system. If it isn't, you can install it using your systems package manager. The package name used on most Linux distributions is htop.

Once installed, you can run htop by running htop in a terminal. The interface will look similar to the below:

HTop Default View

The first thing I would recommend is to switch over to tree view (press F6 on the keyboard) so that you can see the processes in a more structured (tree) view.

You can navigate through the processes using the arrow keys on your keyboard.

The main uses I have for htop are:

  • Looking at CPU and memory usage
  • Sorting running processes by a particular measurement (press F6 to go into the sort menu)
  • Locating a process that is stuck and terminating it by killing the process (press F9 on the process)

Nano

What is Nano?

Nano is a simple to use text editor that you can run in a terminal. I use this most of the time but sometimes use VIM but that depends on the system I am on and if I can't install nano due to access rights. Not here to start a text editor war, I just prefer nano!

The main use I have for nano is modifying text and configuration files on a Linux or macOS system.

How to use Nano

To use nano, type nano, followed by the path to the file and the file name. For example:

nano -l /home/me/test.py
Enter fullscreen mode Exit fullscreen mode

If you want to have the line numbers show as well, run the above command with -l:

nano -l /home/me/test.py
Enter fullscreen mode Exit fullscreen mode

This will then load that file in nano. For example:

nano editor with open file

You can edit text by using the arrow keys to get to a particular line and then make changes as you would any other editor, be that Notepad, TextEdit, gEdit or even Word.

When you are done making changes to the file, press CTRL+X. You will then be asked to save and then the filename to use.

Useful Keyboard Shortcuts

  • CTRL+O: Save the file without quitting (equivalent of CMD+S on mac or CTRL+S on Linux or Windows)
  • CTRL+K: Cut a piece of text (or a line) to be put somewhere else. (equivalent of CMD+X on mac or CTRL+X on Linux or Windows)
  • CTRL+U: Paste a piece of text (or a line). (equivalent of CMD+V on mac or CTRL+V on Linux or Windows)
  • CTRL+W: Search for a particular piece of text in the open file. (equivalent of CMD+F on mac or CTRL+F on Linux or Windows)
  • CTRL+_: Go to a particular line number in the open file.

Systemctl

What is Systemctl?

Systemctl is a service manager that is used on Linux to control the starting, stopping, status and startup of services that are present on that system.

The main reason I use this is to check if a service is running when troubleshooting an issue or restarting a service if a change to a configuration file has been made.

How to use Systemctl

To use systemctl, you need to use sudo for most (not all) commands to work. It is just a good idea to do so to save a bit of time.

First, to see all of the running services on the system, run the following command:

sudo systemctl status
Enter fullscreen mode Exit fullscreen mode

Next, to see if a service is running or not, you use the status function in systemctl. For example, you want to check the running status of the SSH server service on the system you are on:

sudo systemctl status sshd
Enter fullscreen mode Exit fullscreen mode

This will return the below. The main part you are looking for is on the line starting with Active. If the service is running, it will say active (running) next to it.

Systemctl status of SSH service

If you want to restart the sshd service (ideally don't as you might get disconnected but I'm showing you only as an example), you can do so by running the below command:

sudo systemctl restart sshd
Enter fullscreen mode Exit fullscreen mode

If you need to stop a service, you can do so by running:

sudo systemctl stop sshd
Enter fullscreen mode Exit fullscreen mode

If a service is showing as stopped, you can start it by running:

sudo systemctl start sshd
Enter fullscreen mode Exit fullscreen mode

The last part of systemctl I want to cover is how to set a service to startup or not when the system is rebooted or turned on.

If you recently installed a piece of software through a package manager (or any other method), the service may or may not be set to run when the system is booting up. To check this for the sshd service, run:

sudo systemctl status sshd
Enter fullscreen mode Exit fullscreen mode

Systemctl status of SSH service

In the output, if you look at the line starting with Loaded, at the end it shows as 'enabled; vendor preset: enabled'. This indicates that the service is set to startup on reboot (enabled) and the vendor has set it to be enabled when it is installed.

Lets have a look at another service. The below example will show the status of the PostgreSQL service (a database server):

sudo systemctl status postgresql
Enter fullscreen mode Exit fullscreen mode

Systemctl status of PostgreSQL service

If you look at the line starting Active, it shows as active (running). This is fine for the moment but if we look at the line starting with Loaded, unlike the sshd service example from before, it doesn't show as 'enabled; vendor preset: enabled'. Instead it shows as 'disabled; vendor preset: enabled'.

What this indicates is that the service, when installed, is enabled by default but has been manually set to 'disabled' so that when the system is rebooted, it will not run.

Let's say we want this service to be running when the system reboots. This can be done manually by running the following command (the && sudo systemctl status postgresql just shows the status after enabling the service):

sudo systemctl enable postgresql && sudo systemctl status postgresql
Enter fullscreen mode Exit fullscreen mode

Systemctl status of PostgreSQL service

As you can see, the line starting with Loaded now shows as 'enabled; vendor preset: enabled'. At the next reboot of the system, the PostgreSQL service will startup without you having to manually start it.

If you want to set a service to not start when the system is rebooted, you can do so with the following command (postgresql used for an example, change this to whichever service you need to stop):

sudo systemctl disable postgresql && sudo systemctl status postgresql
Enter fullscreen mode Exit fullscreen mode

Systemctl status of PostgreSQL service

From the above output, you now see that the service is set to 'disabled; vendor preset: enabled'. The service will not start when the system is rebooted, until you either start the service or enable it.

Top comments (2)

Collapse
 
netch80 profile image
Valentin Nechayev

As a much older:) Unix user I use screen instead of tmux and joe instead of nano, where possible. screen is much simpler in some setups, for example, when multiple levels of enclosed terminal bunches are needed, with different switch prefixes. OTOH joe has clearer command set (well, matter of taste). (Definitely, they are not vim replacers but occupy another, simpler niche.)

Collapse
 
dev_neil_a profile image
dev_neil_a

Thanks for the reply. I'll have a look at both as I'm always on the lookout for tools that can help get things done quicker or in a simpler way.