建立服務定義。

建立服務定義。

我想讓centos在啟動後且無需登入的情況下顯示tui(ncurses程式)。我不想顯示登入提示。

程式功能:網路設定、重新啟動、關機

有什麼方法可以實現這個目標嗎?

答案1

M. Dickey 的 systemd 答案有點模糊。以下是 systemd 作業系統的詳細資訊。

顯示登入提示的服務是[email protected]範本的一個實例,例如[email protected]登入服務。tty6要在這樣的終端設備上將其替換為您自己的服務,您需要做兩件事。

建立服務定義。

這相當簡單:

# /etc/systemd/系統/[電子郵件受保護]

[單元]
描述=%I 上的自訂使用者介面
文檔=https://unix.stackexchange.com/a/318336/5132
衝突=getty@%I.service
之前=getty.target

[服務]
ExecStart=/usr/local/sbin/my-custom-user-interface
標準輸入=tty
標準輸出=tty
重新啟動=始終
重啟秒=1
UtmpIdentifier=%I
TTYPath=/dev/%I
TTY重置=是
TTYV掛斷=是
TTYVTDisallocate=是
發送SIGHUP=是

[安裝]
WantedBy=多用戶.target

這是一個範本服務單元,它將使用核心虛擬終端設備的名稱作為其模板參數進行實例化。

確保您的服務在引導時自動啟動。

這是更複雜的部分。

對於初學者來說,您需要決定哪個將在其上運行的核心虛擬終端設備。幾個訊息在這裡發揮作用:

  • 在 Fedora 世界中,自 2008 年以來,圖形使用者介面使用第一個核心虛擬終端設備tty1.您必須選擇不使用該虛擬終端設備和不使用(或手動調整)圖形使用者介面。
  • systemd 的 logind 嘗試在核心虛擬終端上要求啟動登入服務,最多可達設定檔NAutoVTs中設定的最大值logind.conf。這是tty1預設tty6的。
  • 這使得tty7Fedora 世界變得自由。但在 Debian 世界中,儘管 Debian 現在是一個 systemd 作業系統,但圖形使用者介面使用tty7.您可能需要考慮移植到 Debian。
  • systemd 世界試圖使核心虛擬終端 #6 成為「始終存在」的登入提示符,肌肉記憶讓您點擊Control+ Alt+F6來取得該提示符。

假設您選擇tty5.

每當使用者切換到核心虛擬終端 #5 時,您都需要停止從按需啟動 登入([email protected]這是 的另一個名稱) 。您可以透過將設定從 6 降低到(比如說)4 來[email protected]實現此目的。NAutoVTslogind.conf

您需要確保您的服務在系統引導時自動啟動,即「啟用」:

系統控制啟用[電子郵件受保護]

備擇方案

還有其他方法可以做到這一點。

非模板化服務

而不是透過替換為(例如)來[email protected]my-terminal-ui.service終端設備名稱硬連線到單元中。%Itty5

我將其作為模板定義,因為這樣可以更輕鬆地做出與tty5.

使用其中之一autovt@tty而不是自己的名字

您可以決定用作服務定義的名稱。這將覆蓋模板的實例化。/etc/systemd/system/[email protected][email protected]

這種方法的優點是,您根本不需要費心NAutoVTs啟用該服務,並且只要使用者切換到核心虛擬終端#5,就可以保持登入狀態以按需啟動您的服務。

缺點是設計了按需啟動機制不運行登入進程在未使用的核心虛擬終端上。因此,您的服務不會在系統引導時運行,而只會在您第一次啟動核心虛擬終端 #5 時運行。這種按需啟動可能不是您想要的。您的客製化系統的其他部分可能期望您的服務已經運行例如,從引導程式開始。

編輯一個getty@tty

人們沉迷於做事的方式,/etc/ttys並將/etc/inittab改變[email protected]視為做事的方式。他們運行這樣的命令

系統控制編輯[電子郵件受保護]

並添加一個覆蓋聲明

[服務]
# 這個空白作業不是列印錯誤。
執行開始=
ExecStart=/usr/local/sbin/my-custom-user-interface

這有幾個問題。

提供服務那叫蓋蒂不運作 { a, min, f}getty程式並不是一個非常容易維護的想法。多年後,當您忘記自己在這裡做了什麼,或者當其他人接管這樣一個系統的管理時,「getty」服務並不總是運行實用程式getty這一點並不明顯。

不要將其視為[email protected]一個空間該終端上運行的任何內容,根據需要進行修改。這並不是真正的系統思考方式。 [email protected]執行一個“getty”程式。運行完全不同的程式且不執行類似「getty」服務的服務本身應該是一個單獨的服務定義。

此外:單獨的服務定義允許人們不將服務定義為類似「getty」服務的服務。上述服務定義複製了「getty」服務的大部分行為,包括努力清潔你的螢幕(正如世界希望你做的那樣)。但是,您可能不希望TTYReset=yes繼續utmpx使用像您這樣的服務,這實際上不是「登入」服務,並且可能不希望如此積極地清除其螢幕。

此外:單獨的服務定義將來更容易更改。使用上述模板,從tty5(如果不滿意)更改為tty8很簡單。只需實例化它 ontty8而不是 on tty5。從 中取消一組本地自訂[email protected]然後重新製作它們會有些困難[email protected]

別名autovt@tty5my-terminal-ui@tty5

這是從編輯開始的邏輯進展[email protected],顯然autovt@tty別名是無論要從這裡開始允許用其他東西取代的機制getty@tty

但它也有與[email protected]直接使用名稱相同的上述缺點,因為它涉及登入的嘗試不啟動登入服務這可能不是你想要的。它實際上是無論是什麼意思要求-從這裡開始透過登入

進一步閱讀

答案2

getty您可以透過用您自己的程式替換用於的登入程序來完成此操作。例如,在我的 Debian 機器上,getty呼叫位於/etc/inittab

# /sbin/getty invocations for the runlevels.
#
# The "id" field MUST be the same as the last
# characters of the device (after "tty").
#
# Format:
#  <id>:<runlevels>:<action>:<process>
#
# Note that on most Debian systems tty7 is used by the X Window System,
# so if you want to add more getty's go ahead but skip tty7 if you run X.
#
1:2345:respawn:/sbin/getty 38400 tty1
2:23:respawn:/sbin/getty 38400 tty2
3:23:respawn:/sbin/getty 38400 tty3
4:23:respawn:/sbin/getty 38400 tty4
5:23:respawn:/sbin/getty 38400 tty5
6:23:respawn:/sbin/getty 38400 tty6

# Example how to put a getty on a serial line (for a terminal)
#
#T0:23:respawn:/sbin/getty -L ttyS0 9600 vt100
#T1:23:respawn:/sbin/getty -L ttyS1 9600 vt100

# Example how to put a getty on a modem line.
#
#T3:23:respawn:/sbin/mgetty -x0 -s 57600 ttyS3

而 Debian 實際上使用agetty

NAME   
       agetty - alternative Linux getty


SYNOPSIS
       agetty  [-8chiLmnsUw]  [-a  user]  [-f  issue_file] [-H login_host] [-I
       init] [-l login_program] [-t timeout] port baud_rate,...  [term]

並且可以使用以下命令呼叫不同的程序-l選項:

   -l, --login-program login_program
          Invoke the specified login_program instead of /bin/login.   This
          allows the use of a non-standard login program (for example, one
          that asks for a dial-up password or that uses a different  pass‐
          word file).

您的專用程式可以在每個“tty”上運行。如果您這樣做,您應該注意確保可以直接登入機器進行維護。這可能是在單一用戶模式下,或透過 ssh。這些getty定義不適用於 ssh。

在 CentOS7 中,使用 systemd,事情的組織方式有所不同,但最終結果是相同的。使用時systemctl list-unit-files,相關服務為[email protected]。與任何其他服務一樣,這可以客製化。使用locate來獲取線索:

/etc/selinux/targeted/modules/active/modules/getty.pp
/etc/systemd/system/getty.target.wants
/etc/systemd/system/getty.target.wants/[email protected]
/usr/lib/systemd/system/console-getty.service
/usr/lib/systemd/system/[email protected]
/usr/lib/systemd/system/getty.target
/usr/lib/systemd/system/[email protected]
/usr/lib/systemd/system/[email protected]
/usr/lib/systemd/system/multi-user.target.wants/getty.target
/usr/lib/systemd/system-generators/systemd-getty-generator

中的檔案/usr/lib/systemd/system/包含對的呼叫agetty(在 CentOS 上也有一個手冊頁)。您可以修改這些:

ExecStart=-/sbin/agetty --noclear --keep-baud pts/%I 115200,38400,9600 $TERM

符號連結/usr/lib/systemd/system/multi-user.target.wants/getty.target及其目標getty.target不包含對 的呼叫agetty

CentOS6 介於兩者之間:我的機器有mingetty和沒有systemd。從手冊頁:

MINGETTY(8)                Linux Programmer's Manual               MINGETTY(8)


NAME   
       mingetty - minimal getty for consoles

SYNOPSIS
       mingetty  [--noclear] [--nonewline] [--noissue] [--nohangup] [--nohost-
       name]    [--long-hostname]     [--loginprog=/bin/login]     [--nice=10]
       [--delay=5]  [--chdir=/home]  [--chroot=/chroot] [--autologin username]
       [--loginpause] tty


DESCRIPTION
       mingetty is a minimal  getty  for  use  on  virtual  consoles.   Unlike
       agetty(8),  mingetty  is  not  suitable  for serial lines.  I recommend
       using mgetty(8) for this purpose.

例如agetty,它可以定制:

   --loginprog /bin/login
          Change the login app.

從它的設定檔開始:

# tty - getty
#
# This service maintains a getty on the specified device.
#
# Do not edit this file directly. If you want to change the behaviour,
# please create a file tty.override and put your changes there.

stop on runlevel [S016]

respawn
instance $TTY
exec /sbin/mingetty $TTY
usage 'tty TTY=/dev/ttyX  - where X is console id'

這導致我們尋找有關 的信息tty.override,根據此

是您提供的腳本/etc/init/tty.override(它顯示了 tty1 上自動登入的範例,從中您可以大致了解該腳本可以執行的操作)。$TTY順便說一下,變數是在(/etc/init/start-ttys.conf這些initscripts變數所屬的套件沒有詳細文件)中設定的。

答案3

因此,經過一番谷歌搜尋後,我找到了兩個解決方案:

https://serverfault.com/questions/323289/replacing-tty-with-a-script-in-centos-6

https://www.centos.org/forums/viewtopic.php?t=2414

最後我提出以下幾點:

1)使用以下內容建立“/etc/init/launch.conf”:

start on stopped rc RUNLEVEL=[2345]
stop on runlevel [S016]
respawn
console owner
exec /usr/bin/openvt -c 1 -w -f -- /usr/sbin/setup

在此範例中,它運行文字模式設定工具

2) 編輯 '/etc/init/start-ttys.conf' 將 tty 替換為 launch:

start on stopped rc RUNLEVEL=[2345]

env ACTIVE_CONSOLES=/dev/tty[1-6]
env X_TTY=/dev/tty1
task
script
    . /etc/sysconfig/init
    for tty in $(echo $ACTIVE_CONSOLES) ; do
        [ "$RUNLEVEL" = "5" -a "$tty" = "$X_TTY" ] && continue
        #initctl start tty TTY=$tty
        initctl start launch TTY=$tty
    done
end script

我不知道這是否是最好的解決方案,但它正在工作並且啟動後會顯示安裝程式。

相關內容