Linux设置长时间不操作自动断开连接 -程序员宅基地

技术标签: shell  开发工具  操作系统  

简单梳理/etc/profile、/etc/bashrc、/etc/profile.d/、~/.bash_profile、~/.bashrc
CentOS7系统

1、/etc/profile

[root@centos7 ~]# cat /etc/profile

/etc/profile

System wide environment and startup programs, for login setup

Functions and aliases go in /etc/bashrc

It's NOT a good idea to change this file unless you know what you

are doing. It's much better to create a custom.sh shell script in

/etc/profile.d/ to make custom changes to your environment, as this

will prevent the need for merging in future updates.

pathmunge () {

case ":${PATH}:" in
    *:"$1":*)
        ;;
    *)
        if [ "$2" = "after" ] ; then
            PATH=$PATH:$1
        else
            PATH=$1:$PATH
        fi
esac

}

if [ -x /usr/bin/id ]; then

if [ -z "$EUID" ]; then
    # ksh workaround
    EUID=`/usr/bin/id -u`
    UID=`/usr/bin/id -ru`
fi
USER="`/usr/bin/id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"

fi

Path manipulation

if [ "$EUID" = "0" ]; then

pathmunge /usr/sbin
pathmunge /usr/local/sbin

else

pathmunge /usr/local/sbin after
pathmunge /usr/sbin after

fi

HOSTNAME=/usr/bin/hostname 2>/dev/null
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then

export HISTCONTROL=ignoreboth

else

export HISTCONTROL=ignoredups

fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

By default, we want umask to get set. This sets it for login shell

Current threshold for system reserved uid/gids is 200

You could check uidgid reservation validity in

/usr/share/doc/setup-*/uidgid file

if [ $UID -gt 199 ] && [ "/usr/bin/id -gn" = "/usr/bin/id -un" ]; then

umask 002

else

umask 022

fi

for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do

if [ -r "$i" ]; then
    if [ "${-#*i}" != "$-" ]; then
        . "$i"
    else
        . "$i" >/dev/null
    fi
fi

done

unset i
unset -f pathmunge
2、 /etc/bashrc

[root@centos7 ~]# cat /etc/bashrc

/etc/bashrc

System wide functions and aliases

Environment stuff goes in /etc/profile

It's NOT a good idea to change this file unless you know what you

are doing. It's much better to create a custom.sh shell script in

/etc/profile.d/ to make custom changes to your environment, as this

will prevent the need for merging in future updates.

are we an interactive shell?

if [ "$PS1" ]; then
if [ -z "$PROMPT_COMMAND" ]; then

case $TERM in
xterm*|vte*)
  if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
      PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
  elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
      PROMPT_COMMAND="__vte_prompt_command"
  else
      PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
  fi
  ;;
screen*)
  if [ -e /etc/sysconfig/bash-prompt-screen ]; then
      PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
  else
      PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
  fi
  ;;
*)
  [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
  ;;
esac

fi
# Turn on parallel history
shopt -s histappend
history -a
# Turn on checkwinsize
shopt -s checkwinsize
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[u@h W]\$ "
# You might want to have e.g. tty in prompt (e.g. more virtual machines)
# and console windows
# If you want to do so, just add e.g.
# if [ "$PS1" ]; then
# PS1="[u@h:l W]\$ "
# fi
# to your custom modification shell script in /etc/profile.d/ directory
fi

if ! shopt -q login_shell ; then # We're not a login shell

# Need to redefine pathmunge, it get's undefined at the end of /etc/profile
pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}

# By default, we want umask to get set. This sets it for non-login shell.
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
   umask 002
else
   umask 022
fi

SHELL=/bin/bash
# Only display echos from profile.d scripts if we are no login shell
# and interactive - otherwise just process them to set envvars
for i in /etc/profile.d/*.sh; do
    if [ -r "$i" ]; then
        if [ "$PS1" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge

fi

vim:ts=4:sw=4

3、~/.bash_profile

.bash_profile

Get the aliases and functions

if [ -f ~/.bashrc ]; then

    . ~/.bashrc

fi

User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
4、~/.bashrc

.bashrc

User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

Source global definitions

if [ -f /etc/bashrc ]; then

    . /etc/bashrc

fi
5、/etc/profile.d/

[root@centos7 ~]# ls -al /etc/profile.d/
total 84
drwxr-xr-x. 2 root root 4096 May 22 09:47 .
drwxr-xr-x. 75 root root 8192 May 22 10:22 ..
-rw-r--r--. 1 root root 771 Apr 11 13:09 256term.csh
-rw-r--r--. 1 root root 841 Apr 11 13:09 256term.sh
-rw-r--r--. 1 root root 196 Mar 25 2017 colorgrep.csh
-rw-r--r--. 1 root root 201 Mar 25 2017 colorgrep.sh
-rw-r--r--. 1 root root 1741 Apr 11 04:20 colorls.csh
-rw-r--r--. 1 root root 1606 Apr 11 04:20 colorls.sh
-rw-r--r--. 1 root root 80 Apr 11 12:18 csh.local
-rw-r--r--. 1 root root 1706 Apr 11 13:09 lang.csh
-rw-r--r--. 1 root root 2703 Apr 11 13:09 lang.sh
-rw-r--r--. 1 root root 123 Jul 31 2015 less.csh
-rw-r--r--. 1 root root 121 Jul 31 2015 less.sh
-rw-r--r-- 1 root root 148 May 22 09:47 path.sh
-rw-r--r--. 1 root root 81 Apr 11 12:18 sh.local
-rw-r--r--. 1 root root 105 Apr 11 07:54 vim.csh
-rw-r--r-- 1 root root 269 May 22 09:42 vim.sh
-rw-r--r--. 1 root root 164 Jan 28 2014 which2.csh
-rw-r--r--. 1 root root 169 Jan 28 2014 which2.sh
/etc/profile

    |- System wide environment and startup programs, for login setup

    |- 用于登录设置的全系统环境和启动程序

/etc/bashrc

    |- System wide functions and aliases

    |- 系统范围的函数和别名

/etc/profile.d/

    |- It's much better to create a custom.sh shell script in /etc/profile.d/ to make custom changes to your environment, as this will prevent the need for merging in future updates.

    |- 最好在/etc/profile.d/中创建一个custom.Shell脚本,以便对环境进行自定义更改,因为这将避免在将来的更新中合并。

~/.bash_profile

    |- User specific environment and startup programs

    |- 用户特定环境和启动程序

~/.bashrc

    |- User specific aliases and functions

    |- 用户特定别名和函数

系统启动时加载 /etc/profile -> 内部加载 /etc/profile.d/ 路径下的*.sh脚本;

用户登录时加载 ~/.bash_profile -> 内部加载 ~/.bashrc -> 内部加载 /etc/bashrc -> 内部加载 /etc/profile.d/ 路径下的*.sh脚本;

用户退出时加载 ~/.bash_logout ;

结论:

在 /etc/profile 中配置系统变量;

在 ~/.bash_profile 中配置用户变量;

SSH连接linux时,长时间不操作就断开的解决方案(增强版)
1、第一次尝试失败
修改/etc/ssh/sshd_config文件,

找到

ClientAliveInterval 0

ClientAliveCountMax 3

并将注释符号("#")去掉,

将ClientAliveInterval对应的0改成60,

ClientAliveInterval指定了服务器端向客户端请求消息 的时间间隔, 默认是0, 不发送.
ClientAliveInterval 60表示每分钟发送一次, 然后客户端响应, 这样就保持长连接了.
ClientAliveCountMax, 使用默认值3即可.

ClientAliveCountMax表示服务器发出请求后客户端没有响应的次数达到一定值, 就自动断开.
正常情况下, 客户端不会不响应.

重起sshd服务:

service sshd restart

依旧没多久就断开窗口

2、第二次尝试成功
为了增强Linux系统的安全性,我们需要在用户输入空闲一段时间后自动断开,这个操作可以由设置TMOUT值来实现。将以下字段加入到/etc/profile 中即可(对所有用户生效)。

export TMOUT=900 # 设置900秒内用户无操作就字段断开终端

readonly TMOUT # 将值设置为readonly 防止用户更改

注意:设置了readonly 之后在当前shell下是无法取消的,需要先将/etc/profile 中设置readonly行注释起来或直接删除,logout 后重新login 。

$ export TMOUT=900

$ readonly TMOUT

$ unset TMOUT

-bash: unset: TMOUT: cannot unset: readonly variable

vim /etc/profile.d/tmout.sh

TMOUT=300

#readonly  TMOUT

export  TMOUT

source /etc/profile.d/tmout.sh

TMOUT:设置超时时间

readonly:设置变量为只读

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cpongo1/article/details/89537455

智能推荐

linux 源码包安装mysql_LINUX下源码包安装mysql-程序员宅基地

文章浏览阅读44次。tar xvfz mysql-5.1.50-linux-i686-glibc23.tar.gz -C /usr/local/解压到的目录cd /usr/local/[root@drbd2 local]# ln -sv mysql-5.1.50-linux-i686-glibc23 mysql(创建连接)groupadd mysql 添加mysql的用户组useradd -g mysql mysql..._linux安装mysql 源码安装

CF163E. e-Government AC自动机+fail树+分块-程序员宅基地

文章浏览阅读186次。原题:http://codeforces.com/contest/163/problem/E题解:给k个字符串,维护3种操作,添加一个字符串,删除一个字符串,查询模式串在主串中出现了几次。暴力来做,跑ac自动机,统计所有的失败指针对应的字符串。考虑如何优化,将失败指针反向,那么模式串的子树就是答案。用深搜序转换成线性,再用对应的数据结构维护就可以,可以用线段树,树状数组,数列分块等。我用的数列...

vue项目中:‘vue-cli-service‘ 不是内部或外部命令,也不是可运行的程序或批处理文件的报错_vue-cli-service' 不是内部或外部命令,也不是可运行的程序 或批处理文件-程序员宅基地

文章浏览阅读308次。解决办法:将项目里的“node_modules”文件夹删除,然后重新运行npm install如果安装了淘宝镜像,可以运行cnpm install最后再运行npm run dev_vue-cli-service' 不是内部或外部命令,也不是可运行的程序 或批处理文件

MVC 向页面传值方式总结(1)-程序员宅基地

文章浏览阅读182次。ViewData传值.HomeController.cs Code:publicActionResult Index(){ ViewData["Title"] ="Home Page"; ViewData["Message"] ="Welcome to ASP.NET MVC!";returnView();}..._homepage放在viewdata

FTP SFTP TFTP的区别_sftp依赖tftp吗-程序员宅基地

文章浏览阅读1.5k次。转自 : http://blog.csdn.net/sztsian/article/details/6640832FTP 是完整、面向会话、常规用途文件传输协议。而 TFTP 用作 bones bare - 特殊目的文件传输协议。 交互使用 FTP。 TFTP 允许仅单向传输的文件。 FTP 提供身份验证。而TFTP 不。 FTP 使用_sftp依赖tftp吗

idea对CPU的占用率过大问题的解决_idea process total cpu usage-程序员宅基地

文章浏览阅读2.3w次,点赞6次,收藏14次。背景:昨晚有一次敲完代码运行完程序后,idea非常卡顿,连输入代码都不能进行,因为有点晚并且累了,所有就关了电脑,今天早上再次打开idea,还是一样卡顿,因为idea的试用期明天就到期了,所有就先破解了idea,破解完之后,还是非常流畅的,但是一运行程序就拉胯了,就先百度看一下解决方案,以下几个解决方案试了,基本问题还是没有解决,就在群里问了几个大佬,方案不是重装系统就是换电脑,同时开始了电脑的配置讨论,重装系统和换电脑的什么是不可能的,所有只有自己慢慢摸索了。1 修改idea配置文件安装目录下的bin_idea process total cpu usage

随便推点

学习Vue你不得不知道的export和import知识_vue export import-程序员宅基地

文章浏览阅读962次。最近公司新上项目,作为后端的我也不得不为了生活开始学习vue手,但是手首先第一步启动项目时就遇到了一个难题Module build failed如图:陆续在网上找到一些方法,也许对别人有效到那时与我无效,但是我还是把他们列出来供各位参考,大家如果也遇到这样的问题可以一个一个的尝试:一、删除项目下面的文件夹:node_module 然后从新npm install(可能有效,个人亲测无效)二、命令行移除文件夹node_modules:rimraf node_modules命令移出node_module_vue export import

Tengine-Lite安装与配置-程序员宅基地

文章浏览阅读1.5k次。一.OpenCL部署Tengine1.安装OpenCLsudo apt install rockchip-mali-midgard14sudo apt install rockchip-mali-midgard-devsudo apt install ocl-icd-opencl-dev opencl-headerssudo apt install clinfoclinfo #显示OpenCL设备信息sudo apt install libclblas-dev #安装opencvsudo _tengine-lite

DB2中用java类实现oracle中REGEXP_LIKE效果_db2 regexp_like-程序员宅基地

文章浏览阅读1.3k次。查询按照IBM官方文档予以实现了。参考文档https://www.ibm.com/developerworks/cn/data/library/techarticle/dm-1011db2luwpatternmatch/#four_db2 regexp_like

Gradle - 编译报org.jetbrains.plugins.gradle.tooling.util.ModuleComponentIdentifierIm的问题解决_cause: org.jetbrains.plugins.gradle.tooling.util.m-程序员宅基地

文章浏览阅读2.5k次。新下载的一个项目报错:Cause: org.jetbrains.plugins.gradle.tooling.util.ModuleComponentIdentifierIm Lorg/gradle/api/artifacts/ModuleIdentifier;其实是gradle版本不一致导致的。修改为统一的 版本就好。..._cause: org.jetbrains.plugins.gradle.tooling.util.modulecomponentidentifierim

logutils java_简单的日志工具类LogUtils-程序员宅基地

文章浏览阅读398次。包含两个类,LogUtils 和 LogLevel1.使用枚举类来设置日志级别ALL("全部", 0),INFO("信息", 1),WARN("警告", 2),ERROR("错误", 3)/*** * 日志级别 * ALL("全部", 0) * INFO("信息", 1) * WARN("警告", 2) * ERROR("错误", 3) * * @since 2018年4月9日 9:31:46..._logutils

fastapi之token验证_fastapi+vuejwt登陆token验证-程序员宅基地

文章浏览阅读7.7k次。fastapi之token验证概述全局依赖概述官方文档里面提供了使用jwt的方式进行的验证比较复杂,这里提供了一种原理相同,但是方式更简单的验证方式,一般更常见于开放api接口的时候的验证使用全局依赖创建一个文件存放全局依赖:# depends.pyimport hashlibimport hmacfrom fastapi import HTTPException, Header..._fastapi+vuejwt登陆token验证

推荐文章

热门文章

相关标签