如何在 Ubuntu 和其他 Linux 发行版中启动、停止和重启服务
服务
![](/data/attachment/album/201912/28/081645vee5lkult55kituu.jpg)


服务是必不可少的后台进程，它通常随系统启动，并在关机时关闭。如果你是系统管理员，那么你会定期处理服务。如果你是普通桌面用户，你可能会遇到需要重启服务的情况，例如[安装 Barrier 来用于在计算机之间共享鼠标和键盘](https://itsfoss.com/keyboard-mouse-sharing-between-computers/)。或[在使用 ufw 设置防火墙](https://itsfoss.com/set-up-firewall-gufw/)时。


今天，我将向你展示两种管理服务的方式。你将学习在 Ubuntu 或任何其他 Linux 发行版中启动、停止和重启服务。



> 
> systemd 与 init
> 
> 
> 如今，Ubuntu 和许多其他发行版都使用 systemd 而不是旧的 init。
> 
> 
> 在 systemd 中，可以使用 `systemctl` 命令管理服务。
> 
> 
> 在 init 中，你可以使用 `service` 命令管理服务。
> 
> 
> 你会注意到，即使你的 Linux 系统使用 systemd，它仍然可以使用 `service` 命令（与 init 系统一起使用的）。这是因为 `service` 命令实际上已重定向到 `systemctl`。systemd 引入了向后兼容性，因为系统管理员们习惯使用 `service` 命令。
> 
> 
> 在本教程中，我将同时展示 `systemctl` 和 `service` 命令。
> 
> 
> 


我用的是 Ubuntu 18.04，但其他版本的过程也一样。


### 方法 1：使用 systemd 在 Linux 中管理服务


我从 systemd 开始，因为它被广泛接受。


#### 1、列出所有服务


为了管理服务，你首先需要知道系统上有哪些服务可用。你可以使用 systemd 的命令列出 Linux 系统上的所有服务：



```
systemctl list-unit-files --type service -all
```

![systemctl list-unit-files](/data/attachment/album/201912/28/081710jtozi0gb00j9cxob.png)


此命令将输出所有服务的状态。服务状态有<ruby> 启用 <rt>  enabled </rt></ruby>、<ruby> 禁用 <rt>  disabled </rt></ruby>、<ruby> 屏蔽 <rt>  masked </rt></ruby>（在取消屏蔽之前处于非活动状态）、<ruby> 静态 <rt>  static </rt></ruby>和<ruby> 已生成 <rt>  generated </rt></ruby>。


与 [grep 命令](https://linuxhandbook.com/grep-command-examples/) 结合，你可以仅显示正在运行的服务：



```
sudo systemctl | grep running
```

![Display running services systemctl](/data/attachment/album/201912/28/081711eoog3bt2z333348o.jpg)


现在，你知道了如何引用所有不同的服务，你可以开始主动管理它们。


**注意：** 下列命令中的 `<service-name>` 应该用你想管理的服务名代替。（比如：network-manager、ufw 等）


#### 2、启动服务


要在 Linux 中启动服务，你只需使用它的名字：



```
systemctl start <service-name>
```

#### 3、停止服务


要停止 systemd 服务，可以使用 `systemctl` 命令的 `stop` 选项：



```
systemctl stop <service-name>
```

#### 4、重启服务


要重启 systemd 服务，可以使用：



```
systemctl restart <service-name>
```

#### 5、检查服务状态


你可以通过打印服务状态来确认你已经成功执行特定操作：



```
systemctl status <service-name>
```

这将以以下方式输出：


![systemctl status](/data/attachment/album/201912/28/081715rgx1maxpramgox4g.jpg)


这是 systemd 的内容。现在切换到 init。


### 方法 2：使用 init 在 Linux 中管理服务


init 的命令和 systemd 的一样简单。


#### 1、列出所有服务


要列出所有 Linux 服务，使用：



```
service --status-all
```

![service –status-all](/data/attachment/album/201912/28/081716p629oqhw59isg629.png)


前面的 `[ – ]` 代表**禁用**，`[ + ]` 代表**启用**。


#### 2、启动服务


要在 Ubuntu 和其他发行版中启动服务，使用命令：



```
service <service-name> start
```

#### 3、停止服务


停止服务同样简单。



```
service <service-name> stop
```

#### 4、重启服务


如果你想重启服务，命令是：



```
service <service-name> restart
```

#### 5、检查服务状态


此外，要检查是否达到了预期的结果，你可以输出服务状态：



```
service <service-name> status
```

这将以以下方式输出：


![service status](/data/attachment/album/201912/28/081717w0j0v0rdbvby0hgy.jpg)


最重要的是，这将告诉你某项服务是否处于活跃状态（正在运行）。


### 总结


今天，我详细介绍了两种在 Ubuntu 或任何其他 Linux 系统上管理服务的非常简单的方法。 希望本文对你有所帮助。


你更喜欢哪种方法？ 让我在下面的评论中知道！




---


via: <https://itsfoss.com/start-stop-restart-services-linux/>


作者：[Sergiu](https://itsfoss.com/author/sergiu/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

Services are essential background processes that are usually run while booting up and shut down with the OS.

If you are a sysadmin, you’ll deal with the service regularly.

If you are a normal desktop user, you may come across the need to restart a service like [setting up Barrier for sharing mouse and keyboard between computers](https://itsfoss.com/keyboard-mouse-sharing-between-computers/) or when you are [using ufw to setup firewall](https://itsfoss.com/set-up-firewall-gufw/).

Today I will show you two different ways you can manage services. You’ll learn to start, stop and restart services in Ubuntu or any other Linux distribution.

- Method 1 uses systemd and you manage sevices with
`systemctl`

command - Method 2 uses init and you manage services with
`service`

command

[Check if You Are Using Systemd or Not in LinuxIs your Linux system using systemd or sysv or some other init system? Here’s how to find out.](https://linuxhandbook.com/check-if-systemd/)![](https://linuxhandbook.com/content/images/2022/09/check-if-systemd-or-init.png)


![](https://linuxhandbook.com/content/images/2022/09/check-if-systemd-or-init.png)

## Method 1: Managing services in Linux with systemd

I am starting with systemd because of the obvious reason of its widespread adoption.

### 1. List all services

In order to manage the services, you first need to know what services are available on your system.

You can use the systemd command to list all the services on your Linux system:

`systemctl list-unit-files --type service -all`


![systemctl list-unit-files](https://itsfoss.com/content/images/wordpress/2019/12/systemctl_list_services.png)

This command will output the state of all services. The value of a service’s state can be enabled, disabled, masked (inactive until mask is unset), static and generated.

Combine it with the [grep command](https://linuxhandbook.com/grep-command-examples/?ref=itsfoss.com) and you can** display just the running services**:

`sudo systemctl | grep running`


![Display running services systemctl](https://itsfoss.com/content/images/wordpress/2019/12/systemctl_grep_running.jpg)

Now that you know how to reference all different services, you can start actively managing them.

**Note:** **<service-***name>** in the commands should be replaced by the name of the service you wish to manage (e.g. network-manager, ufw etc.).*

**2. Start a** service

To start a service in Linux, you just need to use its name like this:

`systemctl start <service-name>`


### 3. **Stop** a service

To stop a systemd service, you can use the stop option of systemctl command:

`systemctl stop <service-name>`


### 4. Re**start** a service

To restart a service in Linux with systemd, you can use:

`systemctl restart <service-name>`


### 5. Check the status of a service

You can confirm that you have successfully executed a certain action by printing the service status:

`systemctl status <service-name>`


This will output information in the following manner:

![systemctl status](https://itsfoss.com/content/images/wordpress/2019/12/systemctl_status.jpg)

That was systemd. Let’s switch to init now.

[systemd vs init Controversy [A Layman’s Guide]Still confused about what is systemd and why it is often at the center of controversy in the Linux world? I try to answer in simpler words.](https://itsfoss.com/systemd-init/)![](https://itsfoss.com/content/images/2023/07/understanding-systemd.png)


![](https://itsfoss.com/content/images/2023/07/understanding-systemd.png)

## Method 2: Managing services in Linux with init

The commands in init are also as simple as system.

`service`

command is actually redirected to systemctl. It’s sort of backward compatibility introduced by systemd because sysadmins were habitual of using the service command.### 1. List all services

To list all the Linux services, use

`service --status-all`


![service --status-all](https://itsfoss.com/content/images/wordpress/2019/12/service_status_all.png)

The services preceded by **[ – ]** are **disabled** and those with **[ + ]** are **enabled**.

**2. Start** a service

To start a service in Ubuntu and other distributions, use this command:

`service <service-name> start`


**3. Stop** a service

Stopping a service is equally easy.

`service <service-name> stop`


### 4. Re**start** a service

If you want to restart a service, the command is:

`service <service-name> restart`


### 5. Check the status of a service

Furthermore, to check if your intended result was achieved, you can output the service** **status**:**

`service <service-name> status`


This will output information in the following manner:

![service status](https://itsfoss.com/content/images/wordpress/2019/12/service_status.jpg)

This will, most importantly, tell you if a certain service is active** (**running**)** or not.

**Wrapping Up**

Today I detailed two very simple methods of managing services on Ubuntu or any other Linux system. I hope this article was helpful to you.

Which method do you prefer? Let us know in the comment section below!