Linux 禁止Root远程登录,限制IP登录

禁止root远程登录

vi /etc/ssh/sshd_config
PermitRootLogin yes改为PermitRootLogin no
重启sshd服务
service sshd restart

ssh和vsftp限制IP登录白名单和黑名单

  • 修改文件:/etc/hosts.deny,加上最后两行,阻止所以进入,如下:
# hosts.deny    This file describes the names of the hosts which are
#               *not* allowed to use the local INET services, as decided
#               by the '/usr/sbin/tcpd' server.
#
# The portmap line is redundant, but it is left to remind you that
# the new secure portmap uses hosts.deny and hosts.allow. In particular
# you should know that NFS uses portmap!

sshd:ALL
vsftpd:ALL
  • 修改 /etc/hosts.allow,加上允许访问的IP,如下:
# hosts.allow   This file describes the names of the hosts which are
#               allowed to use the local INET services, as decided
#               by the '/usr/sbin/tcpd' server.
#
#sshd
sshd:192.168.0.125:allow
sshd:192.168.0.126:allow

#vsfptd
vsftpd:192.168.0.125:allow
vsftpd:192.168.0.126:allow

ssh限制IP和用户登录

只允许指定用户进行登录(白名单):

在/etc/ssh/sshd_config配置文件中设置AllowUsers选项,(配置完成需要重启 SSHD 服务)格式如下:如果是阿里云

#AllowUsers   aliyun    admin@192.168.1.1 				//限制阿里云和该IP登录
AllowUsers    admin@192.168.1.1 						//限制只能该IP登录
#DenyUsers 		 admin@192.168.1.1 						//禁止该IP登录
service sshd restart

添加用户到sudo list

修改sudoers文件

  • 切换到root用户下
  • 添加sudo文件的写权限,命令是:
chmod u+w /etc/sudoers
  • 编辑sudoers文件
vi /etc/sudoers
  • ps:这里说下你可以sudoers添加下面四行中任意一条
youuser            ALL=(ALL)                ALL
%youuser           ALL=(ALL)                ALL
youuser            ALL=(ALL)                NOPASSWD: ALL
%youuser           ALL=(ALL)                NOPASSWD: ALL

第一行:允许用户youuser执行sudo命令(需要输入密码).

第二行:允许用户组youuser里面的用户执行sudo命令(需要输入密码).

第三行:允许用户youuser执行sudo命令,并且在执行的时候不输入密码.

第四行:允许用户组youuser里面的用户执行sudo命令,并且在执行的时候不输入密码.

Attention: 在有的环境中,配置为youuser ALL=(ALL) ALL

也可以达到sudo无需输入密码的效果,但不能保证所有环境都有效,肯定有效的做法是:

youuser ALL=(ALL) NOPASSWD: ALL

sudo密码超时设置

sudo vi sudo

Defaults        env_reset,timestamp_timeout=20

注意:你可以以分钟设置为你所需的任何时间,它会在超时之前一直等待。 如果要为每个执行的 sudo 命令弹出密码提示,你也可以将时间设置为 0,或者通过设置值 -1 永久禁用密码提示。