Linux查看发行版本的信息及登录提示
2019-03-24 17:15:37 阿炯

Linux 有太多的发行版本,但主流的版本也太多,多的是一些主流版本的派生版本,也就是那些非主流的,就是同一发行厂商下的不同版本之前也有差别。之前的/etc/issue,issue.net中可以看到发行版本信息直接写在其中,以centos 7(两个文件内容已经完全一样了)为例:
\S
Kernel \r on an \m

而之前是这样的:
CentOS release 6.9 (Final)
Kernel \r on an \m

部分版本支持:lsb_release -a 命令查看,而Redhat Linux系列版本支持下面的指令,这个命令应该只有部分Linux可以查看,不是所有版本Linux都可以使用该命令 。

# cat /etc/redhat-release|centos-release 命令查看

如果有这些版本信息文件的话:
# cat /etc/*release

# rpm -q redhat-release

# rpm -q centos-release
centos-release-7-3.1611.el7.centos.x86_64

Debian系的操作系统查看版本信息

# cat /etc/issue
Debian GNU/Linux 9 \n \l

# cat /etc/debian_version
9.0

# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

# apt-get install lsb-release
$ lsb_release -da
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 9.0 (stretch)
Release:        9.0
Codename:       stretch

For systemd debian version you may also use hostnamectl:
# hostnamectl
   Static hostname: debian
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 6866ec1d654b4a2ea52d591dc0146d82
           Boot ID: 51bfbc1e197b4d378e95073da0df0288
    Virtualization: xen
  Operating System: Debian GNU/Linux 9 (stretch)
            Kernel: Linux 4.9.0-1-amd64
      Architecture: x86-64


在ssh登录时为何不显示这些版本信息而是直接将此文件内容输出打印到终端上。当在sshd的主配置文件中启动'Banner /etc/issue'这一行时,会发现在登录时直接将此文件的内容展示出来,而不会对其中的标识指令做解析。这个已经在centos 7和crux 3.4上验证过,这就要从issue文件合适的情况说起了。

关于issue(.net)

The file /etc/issue is a text file which contains a message or system identification to be printed before the login prompt. It may contain various @char and \char sequences, if supported by getty.

原文的最后一句是重点:'if supported by getty',It depends on the getty-type program employed on the system.

/etc/issue文件是一个纯文本文件,它还接受某些转义序列(见下文),以便插入有关系统的信息。还有文件issue.net,可以在远程登录时使用。但即使在配置文件中设置了相应选项时,ssh才会使用它,并且不会解释下面显示的转义序列。

man issue
The file /etc/issue is a text file which contains a message or system identification to be printed before the login prompt. It may contain various @char and \char sequences, if supported by the getty-type program employed on the system.

man agetty
ISSUE ESCAPES The issue-file (/etc/issue, or the file set with the -f option) may contain certain escape codes to display the system name, date, time etcetera. All escape codes consist of a backslash () immediately followed by one of the characters listed below.

以下序列由agetty(通常解析/etc/issue的程序)识别,此信息来自man agetty,可以在其中找到有关登录过程的额外信息。
...
n Insert the nodename of the machine, also known as the hostname. Same as uname -n.
O Insert the DNS domainname of the machine.
s Insert the system name (the name of the operating system). Same as uname -s. See also the \S escape code.
m Insert the architecture identifier of the machine. Same as uname -m.
r Insert the release number of the OS. Same as uname -r.
t Insert the current time.

/etc/issue – escape code

The issue-file (/etc/issue or the file set with the -f option) may contain certain escape codes to display the system name, date and time etc. All escape codes consist of a backslash (\) immediately followed by one of the letters explained below.

\b : baudrate of the current line.
\d : current date.
\s : system name, the name of the operating system.
\l : name of the current tty line.
\m : architecture identifier of the machine, eg. i486
\n : nodename of the machine, also known as the hostname.
\o : domainname of the machine.
\r : release number of the OS, eg. 1.1.9.
\t : current time.
\u : number of current users logged in.
\U : string “1 user” or “ users” where is the number of current users logged in.
\v : version of the OS, eg. the build-date etc.

# vim /etc/issue
加入以下的文字
Welcome to FreeOA!
Today is \d \t @ \n

因此,issue只适用于tty终端,而不适用于pts终端。即其中的信息只显示在显示器终端登录前的提示上,而非ssh这种网络登录之后的显示上(不知道telnet会是怎么显示的)。这里列出一些简单的终端设备类型。

tty: terminal(终端),console(控制台)
pty: pseudo terminal (ssh,gnome-terminal,konsole,xfce4-terminal,lxterminal)
ptmx: pseudo terminal master x (/dev/ptmx)
pts: pseudo terminal slave (/dev/pts/0)

/dev/tty*     终端
/dev/ttyS*    串口终端
/dev/ptmx     pty主设备
/dev/pts/*    pty从设备

你在Linux桌面打开一个gnome-terminal标签页,也会在/dev/pts下生成一个对应的字符设备文件。

以debian 8为例

$ cat /etc/issue
Debian GNU/Linux 8 \n \l

The escapes in /etc/issue are getty escapes, not the typical C-style or shell-style escapes. In particular:
\n inserts the hostname (“nodename”)
\l inserts the tty line

在默认的 /etc/issue 文件情况下,可以看到如下的登录终端提示:
Debian GNU/Linux 8 hostname tty1

以上面的centos 7为例

CentOS Linux 7 (Core)
Kernel 3.10.0-957.el7.x86_64 on an x86_64

只查看内核信息:
$ cat /proc/version


Linux 远程登录成功后的提示

这里的提示并不对bash shell的提示符的定制,而是要展示给登录者的信息,debian 9 默认的登录成功提示:

Linux htvm 4.9.0-3-amd64 #1 SMP Debian 4.9.30-2+deb9u2 (2017-06-26) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.


如何修改或关闭这两个提示呢?

debian 9的ssh配置文件中即使将这两项启用:
PrintMotd no
#Banner none

登录时也会显示上面的消息。另外PrintLastLog该项是否开启与否均不会显示上次登录信息。只有将/etc/motd文件移走后,才不会显示。只要此文件存在且其中有内容时,无论是这个选项依然是'no'都会显示其中的内容在登录后的终端上。

编辑这两个文件:
/etc/motd (Message of the Day)
/etc/ssh/sshd_config: Change the setting PrintLastLog to "no", this will disable the "Last login" message.

然后重启sshd生效。另外可在用户的家目录中创建.hushlogin文件来禁止消息的提示。

另建一个消息文件显示给登录后的用户,编辑 /etc/ssh/sshd_config文件,开启提示:
#Banner /some/path

关于motd文件

motd - message of the day

The contents of /etc/motd are displayed by login(1) after a successful login but just before it executes the login shell.

The abbreviation "motd" stands for "message of the day", and this file has been traditionally used for exactly that (it requires much less disk space than mail to all users).

默认的/etc/motd文件就是上面所示的各种版权法律信息(斜体部分)。

后来引入update-motd软件包,它有如下的功效:
an entry in /etc/cron.d calling update-motd every 10 minutes.
a folder /etc/update-motd.d

The script runs all the scripts in /etc/update-motd.d using run-parts, then stores all output in /var/run/motd.dynamic.

/var/run 是链接到/run目录的。

当使用PAM系统调用时 pam_motd 就会提示出motd文件里的内容(非/etc/motd)。

In modern Linux, PAM is in charge of login authentication.

SSH uses the pam_motd module
/etc/pam.d/sshd
session    optional     pam_motd.so  motd=/run/motd.dynamic
session    optional     pam_motd.so  motd=/run/motd.dynamic

Ok, now we have yet another file, /run/motd.dynamic.

For this reason, even in Debian if we create the folder /etc/update-motd.d, which does not exist by default, it will be run at every login in the same way as if we had update-motd package installed.

Confusing huh? Well it gets worse because over time, Debian have changed things, like the scripts populated in /etc/update-motd.d and the use of special files.

相关的文件

/etc/motd – The classic, static file. Does not exist anymore in Ubuntu 16.04 LTS, not even as a symbolic link to /var/run/motd. If it is created, however its contents will be printed too.

/var/run/motd.dynamic – This is what is shown on login currently. It is updated by /etc/init.d/motd at every boot. It is also updated by PAM by running the scripts in /etc/update-motd.d/, if they exist.

如何定制其内容

It is pretty easy to customize our MOTD.

First, make sure to disable /etc/init.d/motd,  as it writes to /var/run/motd.dynamic.
/etc/update-motd.d/10-uname
...
# Update motd
uname -snrvm > /var/run/motd.dynamic
...

如果motd以服务的形式安装的,不喜欢可以考虑将其禁用:systemctl disable motd

Then, just delete current static MOTD, create /etc/update-motd.d folder and place scripts in it.
mkdir /etc/update-motd.d
rm -f /etc/motd # in Debian still exists

Scripts are run by run-parts, so they need not have an extension. They must start with digits so they will be run in order.Finally, you can opt to get rid of the 'last login' message if you want(/etc/ssh/sshd_config)
PrintLastLog no

This is a simple example
cat > /etc/update-motd.d/10logo <<EOF
#!/bin/sh
echo
cat /etc/issue
EOF

cat > /etc/update-motd.d/20updates <<'EOF'
#!/bin/sh
echo
echo "uptime is $( uptime )"
echo "date is $( date )"
EOF

chmod a+x /etc/update-motd.d/*

下次登录就会有下面的提示

Linux htvm 4.9.0-3-amd64 #1 SMP Debian 4.9.30-2+deb9u2 (2017-06-26) x86_64

uptime is  16:36:50 up 20:42,  3 users,  load average: 0.03, 0.02, 0.01
date is Sun Mar 24 16:36:50 CST 2019


自定义motd
Customize Login Message of the Day Debian/CentOS


HOSTNAME=`uname -n`
ROOT=`df -Ph | grep sda1 | awk '{print $4}' | tr -d '\n'`

echo "
===============================================
 - Hostname............: $HOSTNAME
 - Disk Space..........: $ROOT remaining
===============================================
"

Debian

Debian makes this pretty easy. We can simply throw the above script into the directory /etc/update-motd.d/.

mv on_login.sh /etc/update-motd.d/02-info
chmod +x /etc/update-motd.d/02-info

Once you log out and back in, we'll see that info!

CentOS

CentOS takes just a little more work to setup.

We need to turn off (yes, off) SSH's PrintMotd option by editing /etc/ssh/sshd_config:
PrintMotd no

This stops printing from the plaintext /etc/motd and lets us print our own content.

We just need to restart sshd as so that takes affect:
service sshd restart

Now we'll place our shell script into /etc/profile.d.
chmod a+x /etc/profile.d/login-info.sh

Then once we login, we'll see the output of your script!


参考来源:

Customize your MOTD login message in Debian and Ubuntu