Linux在不修改原service配置文件的情况下修改service参数

来自三线的随记

Test Envionments

# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core)

# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"


Related keys

  1. Drop-In
  2. /etc/systemd/system/{servicename}.d


实验

systemctl status sshd

获知 service 名字 sshd.service

[root@image-1 sshd.service.d]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2019-12-19 18:41:50 CST; 6 days ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 6871 (sshd)
   CGroup: /system.slice/sshd.service
           └─6871 /usr/sbin/sshd -D

Dec 26 14:57:34 image-1 systemd[1]: sshd.service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.
Dec 26 15:00:31 image-1 systemd[1]: sshd.service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.
Dec 26 15:02:54 image-1 systemd[1]: [/etc/systemd/system/sshd.service.d/test.conf:3] Invalid URL, ignoring: 12345
Dec 26 15:03:34 image-1 systemd[1]: [/etc/systemd/system/sshd.service.d/test.conf:3] Invalid URL, ignoring: manman:abc(8)
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

( 此 service 之前已经做过实验,所以会有一些奇怪的 warning )

然后

mkdir /etc/systemd/system/sshd.service.d
touch /etc/systemd/system/sshd.service.d/test.conf
systemctl daemon-reload
[root@image-1 sshd.service.d]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
  Drop-In: /etc/systemd/system/sshd.service.d
           └─test.conf
   Active: active (running) since Thu 2019-12-19 18:41:50 CST; 6 days ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 6871 (sshd)
   CGroup: /system.slice/sshd.service
           └─6871 /usr/sbin/sshd -D

Dec 26 14:57:34 image-1 systemd[1]: sshd.service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.
Dec 26 15:00:31 image-1 systemd[1]: sshd.service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.
Dec 26 15:02:54 image-1 systemd[1]: [/etc/systemd/system/sshd.service.d/test.conf:3] Invalid URL, ignoring: 12345
Dec 26 15:03:34 image-1 systemd[1]: [/etc/systemd/system/sshd.service.d/test.conf:3] Invalid URL, ignoring: manman:abc(8)
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

这个时候可以发现回显结果多了一个 Drop-In 字段

然后写入到 test.conf 里面的参数就会应用到 sshd.service 中

有点意思 23333


继续

将 test.conf 内容修改

[root@image-1 sshd.service.d]# cat test.conf 
[Unit]
Description=Just a kidding\n
systemctl daemon-reload
[root@image-1 sshd.service.d]# systemctl status sshd
● sshd.service - Just a kidding\n
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
  Drop-In: /etc/systemd/system/sshd.service.d
           └─test.conf
   Active: active (running) since Thu 2019-12-19 18:41:50 CST; 6 days ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 6871 (sshd)
   CGroup: /system.slice/sshd.service
           └─6871 /usr/sbin/sshd -D

Dec 26 14:57:34 image-1 systemd[1]: sshd.service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.
Dec 26 15:00:31 image-1 systemd[1]: sshd.service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.
Dec 26 15:02:54 image-1 systemd[1]: [/etc/systemd/system/sshd.service.d/test.conf:3] Invalid URL, ignoring: 12345
Dec 26 15:03:34 image-1 systemd[1]: [/etc/systemd/system/sshd.service.d/test.conf:3] Invalid URL, ignoring: manman:abc(8)
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

可以看到这个 services 的 Description 已经发生了改变

好玩

And so on

cp /usr/lib/systemd/system/sshd.service /test.conf
systemctl daemon-reload

可以看到sshd.service有warning

sshd.service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.

这个 Type 和 execStart 就跟这篇文章有关联了 Linux systemctl services


To Be Continued

挖坑

两个service配置的覆写优先级等等等等

这个新加的 conf 的权限需求

这些是尚未知道的