定制登录root用户是的显示信息

2019年6月28日

centos ssh登录后添加提示信息
当我们使用 root 登录ssh时 成功后将会看到类似如下信息:

Using username "root".
Last login: Thu Mar 27 15:49:13 2014 from xxx.xxx.xxx.xxx

其实这里可以显示更多信息
如果是静态信息就是没有变量的文本那么可以写在 /etc/motd 这个文件里(一般默认这里是空的啥也没有) 例如:

vim /etc/ssh/sshd_config 将文件中PrintMotd 属性修改为 yes

#AllowTcpForwarding yes

#GatewayPorts no

#X11Forwarding no X11Forwarding yes

#X11DisplayOffset 10

#X11UseLocalhost yes

PrintMotd yes

## /etc/ssh/sshd_config 不做更改好像也行

vim /etc/motd
Hello,Welcome to login this server

[root@domain ~]#

保存退出

重新开一个新的登录界面 将会显示如下信息:

Using username "root".
Last login: Thu Mar 27 15:49:13 2014 from xxx.xxx.xxx.xxx

 

Hello,Welcome to login this server

[root@domain ~]#

 

如果是有变量的 编辑一个脚本文件 位置自定义

1) 编辑脚本

vim /home/root/login

 

#!/bin/bash
echo "Welcome [$USER] To Login To This Server!"
MainIp=`ifconfig venet0:0 | grep "inet addr" | awk '{print $2}' | awk -F: '{print $2}'`
Uptime=`uptime`
System=`head -1  /etc/issue`
echo "Hostname..........: $HOSTNAME"
echo "Uptime............: $Uptime"
echo "Server IP.........: $MainIp"
echo "Operating System..: $System"
echo "Username..........: $USER"

2) 赋予权限并加入到root的.bashrc文件中

chmod a+x /home/root/login

 

vim ~/.bashrc (红色部分 # login show 下面)

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# login show
/home/root/login

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

再次登录将可以看到如下信息

Using username "root".
Last login: Thu Mar 27 16:38:50 2014 from xxx.xxx.xxx.xxx
Welcome [root] To Login To This Server!
Hostname..........: domain.com
Uptime............:  16:49:51 up 20 days,  5:37,  2 users,  load average: 2.68, 2.52, 2.94
Server IP.........: xxx.xxx.xxx.xxx
Operating System..: CentOS release 6.4 (Final)
Username..........: root
[root@domain ~]#

到此结束 不过这么做将造成另一个问题 无法使用 sftp
罪魁祸首就是.bashrc文件.
在bash的联机手册里有一段,说的是关于通过RSHD登录系统时,系统要执行.bashrc文件.而这个文件是在启动一个交互SHELL时才要执行的.而在SSH登录时,系统就要执行.bashrc文件.而SSH登录系统是非交互的.如果执行.bashrc文件时,产生输入与输出就会出在我遇到的这个问题.
解决办法是:
在.bashrc文件中,测试一下是交互SHELL还是非交互的,如果是非交互的,则直接退出即可.
一般在.bashrc文件的最前边加入下面这行就可以了:
[ -z "$PS1" ] && return #我试验了此条,一般用户修改自己家目录下的.bashrc成功!
或者:
[ $- != *i* ] && return

vim ~/.bashrc

 

# .bashrc

# User specific aliases and functions
[ -z "$PS1" ] && return

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# login show
/home/root/login

# Source global definitions
# Source global definitions
if [ -f /etc/bashrc ]; then
         . /etc/bashrc
fi

这样 sftp 就可以正常使用了

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注