本文实现通过串口自动登录Ubuntu系统,并自动运行程序
1. 自动登录
1. 写脚本autologin
#!/bin/bash
/bin/login -f username #你的用户名
保存到/usr/bin/下,并且用`chmod +x autologin`设置可执行权限
2. 配置systemd服务
修改系统中/lib/systemd/system/serial-getty\@.service 文件内容,修改ExecStart项,实现自动启动有两种方式:
- ExecStart=-/sbin/agetty -a root 115200 %I $TERM 其中root可以替换为任何用户,这种方式实现自动登录。
- ExecStart=-/sbin/agetty -n -l /usr/bin/autologin 115200 %I $TERM` 这种方式通过执行autologin脚本来实现登录。
$ sudo vim /lib/systemd/system/serial-getty\@.service
# SPDX-License-Identifier: LGPL-2.1+
#
# 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 getty-pre.target
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
# IgnoreOnIsolate causes issues with sulogin, if someone isolates
# rescue.target or starts rescue.service from multi-user.target or
# graphical.target.
Conflicts=rescue.service
Before=rescue.service
[Service]
# The '-o' option value tells agetty to replace 'login' arguments with an
# option to preserve environment (-p), followed by '--' for safety, and then
# the entered username.
#ExecStart=-/sbin/agetty -o '-p -- \\u' --keep-baud 115200,38400,9600 %I $TERM
#ExecStart=-/sbin/agetty -a root 115200 %I $TERM
ExecStart=-/sbin/agetty -n -l /usr/bin/autologin 115200 %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
配置服务器自动启动
$ sudo systemctl enable getty@tty1.service
Created symlink /etc/systemd/system/getty.target.wants/getty@tty1.service → /lib/systemd/system/getty@.service.
重启验证服务
Last login: Fri Aug 13 00:33:41 UTC 2021 from 192.168.31.150 on pts/2
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.19.95+ aarch64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
* Super-optimized for small spaces - read how we shrank the memory
footprint of MicroK8s to make it the smallest full K8s around.
https://ubuntu.com/blog/microk8s-memory-optimisation
HwHiAiUser@davinci-mini:~$
2. 程序自启动
通过在登录用户home目录下的.profile脚本添加程序启动命令来启动程序。
修改.profile,在文件尾部添加以下内容
#注意此处加判断是防止程序重复启动,由于.profile中的内容,在每次登录都会执行,如果用户通过ssh等其他方式登录会导致程序重复启动。
# $DISPLAY是可选项,如果是GUI的程序建议加上
if [ -z "$DISPLAY" ] && [ $(tty) == /dev/tty1 ]; then
#此处添加你的启动程序的命令
echo "run your application here!"
fi
3. 重启,确实是不是自动登陆并执行了?
Last login: Fri Aug 13 00:33:41 UTC 2021 from 192.168.31.150 on pts/2
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.19.95+ aarch64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
* Super-optimized for small spaces - read how we shrank the memory
footprint of MicroK8s to make it the smallest full K8s around.
https://ubuntu.com/blog/microk8s-memory-optimisation
run your application here!