ssh服务器禁止root直接登录

 1.直接方法:

    a):  修改ssh服务器sshd的配置文件: /etc/ssh/sshd_config           

                 -bash-3.2# vi /etc/ssh/sshd_config 

                       ...
                  PermitRootLogin no
                  PermitEmptyPasswords no
                      ...


-bash-3.2# cat /etc/ssh/sshd_config#       $OpenBSD: sshd_config,v 1.73 2005/12/06 22:38:28 reyk Exp $# This is the sshd server system-wide configuration file.  See# sshd_config(5) for more information.# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin# The strategy used for options in the default sshd_config shipped with# OpenSSH is to specify options with their default value where# possible, but leave them commented.  Uncommented options change a# default value.#Port 22#Protocol 2,1Protocol 2#AddressFamily any#ListenAddress 0.0.0.0#ListenAddress ::# HostKey for protocol version 1#HostKey /etc/ssh/ssh_host_key# HostKeys for protocol version 2#HostKey /etc/ssh/ssh_host_rsa_key#HostKey /etc/ssh/ssh_host_dsa_key# Lifetime and size of ephemeral version 1 server key#KeyRegenerationInterval 1h#ServerKeyBits 768# Logging# obsoletes QuietMode and FascistLogging#SyslogFacility AUTHSyslogFacility AUTHPRIV#LogLevel INFO# Authentication:#LoginGraceTime 2m#PermitRootLogin yes         #允许root从ssh客户端登录 PermitRootLogin no           #禁止root用户通过ssh客户端登录#StrictModes yes#MaxAuthTries 6#RSAAuthentication yes#PubkeyAuthentication yes#AuthorizedKeysFile     .ssh/authorized_keys# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts#RhostsRSAAuthentication no# similar for protocol version 2#HostbasedAuthentication no# Change to yes if you don't trust ~/.ssh/known_hosts for# RhostsRSAAuthentication and HostbasedAuthentication#IgnoreUserKnownHosts no# Don't read the user's ~/.rhosts and ~/.shosts files#IgnoreRhosts yes# To disable tunneled clear text passwords, change to no here!#PasswordAuthentication yes#PermitEmptyPasswords noPermitEmptyPasswords no        #不允许空口令用户从ssh登录PasswordAuthentication yes# Change to no to disable s/key passwords#ChallengeResponseAuthentication yesChallengeResponseAuthentication no# Kerberos options#KerberosAuthentication no#KerberosOrLocalPasswd yes#KerberosTicketCleanup yes#KerberosGetAFSToken no# GSSAPI options#GSSAPIAuthentication noGSSAPIAuthentication yes#GSSAPICleanupCredentials yesGSSAPICleanupCredentials yes# Set this to 'yes' to enable PAM authentication, account processing,# and session processing. If this is enabled, PAM authentication will# be allowed through the ChallengeResponseAuthentication mechanism.# Depending on your PAM configuration, this may bypass the setting of# PasswordAuthentication, PermitEmptyPasswords, and# "PermitRootLogin without-password". If you just want the PAM account and# session checks to run without PAM authentication, then enable this but set# ChallengeResponseAuthentication=no#UsePAM noUsePAM yes# Accept locale-related environment variablesAcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGESAcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENTAcceptEnv LC_IDENTIFICATION LC_ALL#AllowTcpForwarding yes#GatewayPorts no#X11Forwarding noX11Forwarding yes#X11DisplayOffset 10#X11UseLocalhost yes#PrintMotd yes#PrintLastLog yes#TCPKeepAlive yes#UseLogin no#UsePrivilegeSeparation yes#PermitUserEnvironment no#Compression delayed#ClientAliveInterval 0#ClientAliveCountMax 3#ShowPatchLevel no#UseDNS yes#PidFile /var/run/sshd.pid#MaxStartups 10#PermitTunnel no#ChrootDirectory none# no default banner path#Banner /some/path# override default of no subsystemsSubsystem       sftp    /usr/libexec/openssh/sftp-server


b): 重启shh服务器sshd:

       用命令:

-bash-3.2# /etc/init.d/sshd restart
停止 sshd:                                                [确定]
启动 sshd:                                                [确定]
-bash-3.2#

或者:

-bash-3.2# service sshd restart
停止 sshd:                                                [确定]
启动 sshd:                                                [确定]
-bash-3.2#

2.间接方法: 禁用root 用户:

  a): 方法一: 找一个特殊用户使其提升为root权限,将其uid,gid都改为0,来替代root;

               接着,在/etc/passwd 文件开头找的root开头的行,一般是第一行,前加#,将彻底禁用root用户,不推荐使用该方法。

 该方法可能造成 sudo 功能失效。

 

 b):方法二:修改 /etc/shadow 文件

   在root开头的行首 加 !或者*,

 

c):方法三: 利用passwd命令

           锁住root用户: sudo passwd -l root

           解锁root用户: sudo passwd -u root        

3.总结: 推荐使用直接方法,和间接方法三。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值