In this blog post, we will delve into two powerful command-line tools used in Linux systems: `systemctl` and `firewall-cmd`. We will explore their functionalities, flags, and provide examples to illustrate their usage.
## Systemctl: Controlling systemd Services
`systemctl` is a command-line utility that allows you to control the `systemd` system and service manager¹². `systemd` is the first process that gets started during bootup (with PID 1) and it's responsible for bringing the Linux host up to the state where it can be used².
### Flags and Usage
Here are some commonly used flags with `systemctl`:
- `start [service]`: Starts a service¹.
- `stop [service]`: Stops a service¹.
- `restart [service]`: Restarts a service¹.
- `reload [service]`: Reloads a service configuration¹.
- `enable [service]`: Enables a service to start at boot¹.
- `disable [service]`: Disables a service from starting at boot¹.
- `status [service]`: Checks the status of a service¹.
- `list-units --type=service --all --state=exited,inactive`: Lists all units, including inactive ones¹.
### Examples
systemctl status httpd
```
The output might look something like this¹⁷:
[root@host ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2020-05-08 14:15:54 EDT; 1 weeks 2 days ago
Docs: man:httpd (8)
man:apachectl (8)
Process: 3767 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 1321 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─1321 /usr/sbin/httpd -DFOREGROUND
├─3769 /usr/sbin/httpd -DFOREGROUND
```
## 2. Starting a service
To start a service, you can use the `systemctl start` command¹⁹:
systemctl start httpd
```
This command does not produce any output if the operation is successful.
## 3. Stopping a service
The `systemctl stop` command is used to stop a running service²⁸:
systemctl stop httpd
```
Like the start command, this command also does not produce any output if the operation is successful.
## 4. Restarting a service
To restart a service, you can use the `systemctl restart` command⁶:
systemctl restart httpd
```
This command does not produce any output if the operation is successful.
## 5. Checking if a service is active
The `systemctl is-active` command is used to check if a service is currently running²⁴:
systemctl is-active httpd
```
The output will be `active` if the service is running, and `inactive` if it is not.
## 6. Enabling a service
The `systemctl enable` command is used to set a service to start automatically at system boot¹⁴:
systemctl enable httpd
```
This command does not produce any output if the operation is successful.
## 7. Checking if a service is enabled
The `systemctl is-enabled` command is used to check if a service is enabled to start at boot²⁴:
systemctl is-enabled httpd
```
The output will be `enabled` if the service is set to start at boot, and `disabled` if it is not.
## 8. Disabling a service
The `systemctl disable` command is used to prevent a service from starting automatically at boot[^20^]:
systemctl disable httpd
```
This command does not produce any output if the operation is successful.
# Start a service
sudo systemctl start apache2
# Stop a service
sudo systemctl stop apache2
# Restart a service
sudo systemctl restart apache2
# Reload a service configuration
sudo systemctl reload apache2
# Enable a service to start at boot
sudo systemctl enable apache2
# Disable a service from starting at boot
sudo systemctl disable apache2
# Check the status of a service
sudo systemctl status apache2
```
## Firewall-cmd: Managing Firewalld
`firewall-cmd` is a command-line client of the `firewalld` daemon⁴⁵. It provides an interface to manage the runtime and permanent configurations⁴.
### Flags and Usage
Here are some commonly used flags with `firewall-cmd`:
- `--state`: Checks whether the `firewalld` daemon is active⁴.
- `--reload`: Reloads firewall rules and keeps state information⁴.
- `--complete-reload`: Reloads firewall completely, even netfilter kernel modules⁴.
- `--runtime-to-permanent`: Saves active runtime configuration and overwrites permanent configuration with it⁴.
- `--new-zone [zone]`: Creates a new zone⁶.
- `--zone=[zone] --add-service=[service]`: Adds a service to a specific zone⁵.
- `--zone=[zone] --list-services`: Lists all services in a specific zone⁵.
### Examples
To check the state of the firewall, use the following command:
firewall-cmd --state
```
This command will return either `running` or `not running`, indicating whether the `firewalld` service is active.
## Listing All Firewall Configurations
To list all the current firewall configurations, use:
firewall-cmd --list-all
```
This command will display the default zone, interfaces, sources, services, ports, and other settings.
## Listing All Zones
To list all predefined zones, use:
firewall-cmd --list-all-zones
```
This command will display all available zones along with their configurations.
## Adding Services
To add a service (like HTTP or FTP) to the firewall, use:
firewall-cmd --add-service=http
firewall-cmd --add-service=ftp --permanent
```
The first command allows HTTP traffic temporarily, while the second command allows FTP traffic permanently.
## Reloading Firewall
After making changes, you can apply them by reloading the firewall:
firewall-cmd --reload
```
## Adding and Removing Ports
To add or remove a specific port, use:
firewall-cmd --add-port=21/tcp --permanent
firewall-cmd --remove-port=21/tcp --permanent
```
These commands add and remove TCP port 21 permanently.
## Creating and Setting Default Zone
To create a new zone and set it as the default, use:
firewall-cmd --permanent --new-zone=test1
firewall-cmd --set-default-zone=test1
```
These commands create a new zone named 'test1' and set it as the default zone.
## Adding Interface to Zone
To add an interface to a specific zone, use:
firewall-cmd --permanent --zone=test1 --add-interface=enp0s8
```
This command adds the 'enp0s8' interface to the 'test1' zone.
In conclusion, `systemctl` and `firewall-cmd` are powerful tools that provide control over services and firewall rules in a Linux system. Understanding these tools is essential for effective system administration.