Cài đặt ban đầu Ubuntu 22.04: Quản lý dịch vụ

bởi | LPI, Ubuntu

Home » LPI » Ubuntu » Cài đặt ban đầu Ubuntu 22.04: Quản lý dịch vụ

Nắm được cách để khởi động và dừng dịch vụ trong hệ điều hành Ubuntu 22.04 LTS

Hiển thị services

– Hiển thị danh sách các dịch vụ hiện đang hoạt động

root@localhost:~# systemctl -t service
  UNIT                                                                         >
  apparmor.service                                                             >
  apport.service                                                               >
  blk-availability.service                                                     >
  cloud-config.service                                                         >
  cloud-final.service                                                          >
  cloud-init-local.service                                                     >
  cloud-init.service                                                           >
  console-setup.service                                                        >
  cron.service                                                                 >
  dbus.service                                                                 >
  finalrd.service                                                              >

.....
.....

  unattended-upgrades.service                                                  >
  user-runtime-dir@1000.service                                                >
  user@1000.service                                                            >
  vgauth.service                                                               >

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
57 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

– Hiển thị danh sách tất cả các dịch vụ

root@localhost:~# systemctl list-unit-files -t service
UNIT FILE                                  STATE           VENDOR PRESET
apparmor.service                           enabled         enabled
apport-autoreport.service                  static          -
apport-forward@.service                    static          -
apport.service                             generated       -
apt-daily-upgrade.service                  static          -
apt-daily.service                          static          -
apt-news.service                           static          -
autovt@.service                            alias           -

.....
.....

xfs_scrub@.service                         static          -
xfs_scrub_all.service                      static          -
xfs_scrub_fail@.service                    static          -

211 unit files listed.

Khởi động dịch vụ

Hiện thị trạng thái hiện tại của service

– Sử dụng systemctl status để kiểm tra trạng thái của một dịch vụ

root@localhost:~# systemctl status ufw.service
● ufw.service - Uncomplicated firewall
     Loaded: loaded (/lib/systemd/system/ufw.service; enabled; vendor preset: e>
     Active: active (exited) since Wed 2024-05-01 04:29:35 UTC; 21min ago
       Docs: man:ufw(8)
    Process: 665 ExecStart=/lib/ufw/ufw-init start quiet (code=exited, status=0>
   Main PID: 665 (code=exited, status=0/SUCCESS)
        CPU: 4ms

May 01 04:29:35 localhost systemd[1]: Starting Uncomplicated firewall...
May 01 04:29:35 localhost systemd[1]: Finished Uncomplicated firewall.
root@localhost:~# systemctl status nftables.service
○ nftables.service - nftables
     Loaded: loaded (/lib/systemd/system/nftables.service; disabled; vendor pre>
     Active: inactive (dead)
       Docs: man:nft(8)
             http://wiki.nftables.org

Khởi động

– Khởi động cho một dịch vụ nếu bạn cần. Ví dụ: khởi động dịch vụ [nftables.service] như ví dụ bên dưới.

root@localhost:~# systemctl start nftables.service
root@localhost:~# systemctl status nftables.service
● nftables.service - nftables
     Loaded: loaded (/lib/systemd/system/nftables.service; disabled; vendor pre>
     Active: active (exited) since Wed 2024-05-01 05:10:45 UTC; 3s ago
       Docs: man:nft(8)
             http://wiki.nftables.org
    Process: 1365 ExecStart=/usr/sbin/nft -f /etc/nftables.conf (code=exited, s>
   Main PID: 1365 (code=exited, status=0/SUCCESS)
        CPU: 28ms

May 01 05:10:45 localhost systemd[1]: Starting nftables...
May 01 05:10:45 localhost systemd[1]: Finished nftables.

– Để khởi động lại một dịch vụ sử dụng command sau:

root@localhost:~# systemctl restart nftables.service
root@localhost:~# systemctl status nftables.service
● nftables.service - nftables
     Loaded: loaded (/lib/systemd/system/nftables.service; enabled; vendor pres>
     Active: active (exited) since Wed 2024-05-01 05:15:33 UTC; 3s ago
       Docs: man:nft(8)
             http://wiki.nftables.org
    Process: 1416 ExecStart=/usr/sbin/nft -f /etc/nftables.conf (code=exited, s>
   Main PID: 1416 (code=exited, status=0/SUCCESS)
        CPU: 22ms

May 01 05:15:33 localhost systemd[1]: Starting nftables...
May 01 05:15:33 localhost systemd[1]: Finished nftables.

– Để service tự khởi động sau khi reboot server:

root@localhost:~# systemctl enable nftables.service
Created symlink /etc/systemd/system/sysinit.target.wants/nftables.service → /lib/systemd/system/nftables.service.
root@localhost:~# systemctl status nftables.service
● nftables.service - nftables
     Loaded: loaded (/lib/systemd/system/nftables.service; enabled; vendor pres>
     Active: active (exited) since Wed 2024-05-01 05:10:45 UTC; 57s ago
       Docs: man:nft(8)
             http://wiki.nftables.org
   Main PID: 1365 (code=exited, status=0/SUCCESS)
        CPU: 28ms

May 01 05:10:45 localhost systemd[1]: Starting nftables...
May 01 05:10:45 localhost systemd[1]: Finished nftables.

– Để service tự khởi động sau khi reboot server và khởi động service ngay:

# systemctl enable --now nftables.service

2 Stop, disable dịch vụ

– Dừng và TẮT tự động khởi động cho một dịch vụ nếu bạn không cần. Ví dụ: tắt dịch vụ [nftables.service] như ví dụ bên dưới.

root@localhost:~# systemctl stop nftables.service
root@localhost:~# systemctl status nftables.service
○ nftables.service - nftables
     Loaded: loaded (/lib/systemd/system/nftables.service; enabled; vendor pres>
     Active: inactive (dead) since Wed 2024-05-01 05:17:56 UTC; 2s ago
       Docs: man:nft(8)
             http://wiki.nftables.org
    Process: 1416 ExecStart=/usr/sbin/nft -f /etc/nftables.conf (code=exited, s>
    Process: 1426 ExecStop=/usr/sbin/nft flush ruleset (code=exited, status=0/S>
   Main PID: 1416 (code=exited, status=0/SUCCESS)
        CPU: 21ms

May 01 05:15:33 localhost systemd[1]: Starting nftables...
May 01 05:15:33 localhost systemd[1]: Finished nftables.
May 01 05:17:56 localhost systemd[1]: Stopping nftables...
May 01 05:17:56 localhost systemd[1]: nftables.service: Deactivated successfull>
May 01 05:17:56 localhost systemd[1]: Stopped nftables.
root@localhost:~# systemctl disable nftables.service
Removed /etc/systemd/system/sysinit.target.wants/nftables.service.
root@localhost:~# systemctl status nftables.service
○ nftables.service - nftables
     Loaded: loaded (/lib/systemd/system/nftables.service; disabled; vendor pre>
     Active: inactive (dead)
       Docs: man:nft(8)
             http://wiki.nftables.org

May 01 05:10:45 localhost systemd[1]: Starting nftables...
May 01 05:10:45 localhost systemd[1]: Finished nftables.
May 01 05:15:33 localhost systemd[1]: Stopping nftables...
May 01 05:15:33 localhost systemd[1]: nftables.service: Deactivated successfull>
May 01 05:15:33 localhost systemd[1]: Stopped nftables.
May 01 05:15:33 localhost systemd[1]: Starting nftables...
May 01 05:15:33 localhost systemd[1]: Finished nftables.
May 01 05:17:56 localhost systemd[1]: Stopping nftables...
May 01 05:17:56 localhost systemd[1]: nftables.service: Deactivated successfull>
May 01 05:17:56 localhost systemd[1]: Stopped nftables.
lines 1-16/16 (END)

+ để tắt và dừng sử dụng service ngay command như sau:

[root@dlp ~]# systemctl disable --now nftables.service

Tham khảo thêm

Cài đặt ban đầu Ubuntu 22.04: Enable root user