文件系统(4)——Ubuntu system service

本文详细介绍了Linux中systemd服务的工作原理,特别是serial-getty@.service服务如何实现串口登录。通过示例展示了如何使用agetty程序配置自动登录,并解释了agetty的各种配置选项。此外,还讨论了如何设置开机自动运行程序,为Linux系统的自动化管理提供了指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

systemctl指令

列出所有服务,包括了启用和禁用的服务:
systemctl list-unit-files --type=service

只想查看启用的服务:
systemctl list-units --type=service --state=running

systemctl enable/disable/start/stop ***.service

system service一些应用场景

1、设置串口命令行自动登录

参考链接:
linux_systemd启动方式解析,如何开机自动登录,开机自动运行程序
https://blog.youkuaiyun.com/a617996505/article/details/88423794
涉及文件:
OK3399-linux-ubuntu/lib/systemd/system/serial-getty@.service

serial-getty@.service是串口登录的服务,内容如下:

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Serial Getty on %I
Documentation=man:agetty(8) man:systemd-getty-generator(8)
Documentation=http://0pointer.de/blog/projects/serial-console.html
BindsTo=dev-%i.device
After=dev-%i.device systemd-user-sessions.service plymouth-quit-wait.service
After=rc-local.service

# If additional gettys are spawned during boot then we should make
# sure that this is synchronized before getty.target, even though
# getty.target didn't actually pull it in.
Before=getty.target
IgnoreOnIsolate=yes

[Service]
ExecStart=-/sbin/agetty --autologin forlinx --keep-baud 115200,38400,9600 %I $TERM
Type=idle
Restart=always
UtmpIdentifier=%I
TTYPath=/dev/%I
TTYReset=yes
TTYVHangup=yes
KillMode=process
IgnoreSIGPIPE=no
SendSIGHUP=yes

[Install]
WantedBy=getty.target

该服务通过运行/sbin/agetty程序,登录终端。

  787 ttyFIQ0  Ss     0:00 /bin/login -f
  789 tty1     Ss+    0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux

forlinx@forlinx:~$ ps
  PID TTY          TIME CMD
  826 ttyFIQ0  00:00:00 bash
  925 ttyFIQ0  00:00:00 ps

看一下agetty程序的配置选项:

forlinx@forlinx:~$ /sbin/agetty --help

Usage:
 agetty [options] <line> [<baud_rate>,...] [<termtype>]
 agetty [options] <baud_rate>,... <line> [<termtype>]

Open a terminal and set its mode.

Options:
 -8, --8bits                assume 8-bit tty
 -a, --autologin <user>     login the specified user automatically
 -c, --noreset              do not reset control mode
 -E, --remote               use -r <hostname> for login(1)
 -f, --issue-file <file>    display issue file
 -h, --flow-control         enable hardware flow control
 -H, --host <hostname>      specify login host
 -i, --noissue              do not display issue file
 -I, --init-string <string> set init string
 -J  --noclear              do not clear the screen before prompt
 -l, --login-program <file> specify login program
 -L, --local-line[=<mode>]  control the local line flag
 -m, --extract-baud         extract baud rate during connect
 -n, --skip-login           do not prompt for login
 -N  --nonewline            do not print a newline before issue
 -o, --login-options <opts> options that are passed to login
 -p, --login-pause          wait for any key before the login
 -r, --chroot <dir>         change root to the directory
 -R, --hangup               do virtually hangup on the tty
 -s, --keep-baud            try to keep baud rate after break
 -t, --timeout <number>     login process timeout
 -U, --detect-case          detect uppercase terminal
 -w, --wait-cr              wait carriage-return
     --nohints              do not print hints
     --nohostname           no hostname at all will be shown
     --long-hostname        show full qualified hostname
     --erase-chars <string> additional backspace chars
     --kill-chars <string>  additional kill chars
     --chdir <directory>    chdir before the login
     --delay <number>       sleep seconds before prompt
     --nice <number>        run login with this priority
     --reload               reload prompts on running agetty instances
     --list-speeds          display supported baud rates
     --help                 display this help
     --version              display version

For more details see agetty(8).

所以如果要配置自动登录的话就可以修改这个服务,添加" -a, --autologin "即可.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值