文章目录[隐藏]

1使用SSH登录服务器
2添加一般用户

例:以添加一个叫“serversman”的一般用户为例进行说明
※添加的用户名可以是任意的。

[root@vps]# useradd serversman
3为已添加的一般用户设置密码
 
[root@vps]# passwd serversman
Changing password for user serversman.
New UNIX password: 自由设置一般用户密码
Retype new UNIX password: 请再次输入以上密码进行确认
passwd: all authentication tokens updated successfully.
4从服务器注销
 
[root@vps]# exit
5通过创建的一般用户登录,升级为root权限
 
[serversman@vps]$ su -
Password: root权限用的登录密码
[root@vps]#

※用户名部分如果显示root,则表示升级成功

6变更sshd_config的设置内容
 
[root@vps]# vi /etc/ssh/sshd_config

在显示画面的第39行处,“#PermitRootLogin yes” 项目下面添加
“PermitRootLogin no”并保存设置

#PermitRootLogin yes
PermitRootLogin no
7重新启动sshd
 
[serversman@vps]# /etc/init.d/sshd restart
Stopping sshd:        [OK]
Starting sshd:        [OK]

之后不能再直接以root权限登录服务器
※一般用户可以通过使用su指令升级为root权限
※#/etc/rc.d/init.d/sshd restart也是可以使用的

【为什么要限制】
针对来自外部的非法访问,通过以下设置可提高防范能力,更安全地使用服务器
1利用SSH登录到服务器

※请一定以root登录

2显示host.allow文件的编辑画面
 
[root@vps]# vi /etc/hosts.allow
3设置允
许的主机名

※host.allow比host.deny的设置内容更优先。

# 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.
#
>ALL设置想要许可的主机名
4显示host.deny文件的编辑画面
 
[root@vps]# vi /etc/hosts.deny
5设置被拒绝的主机名

※host.allow比host.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!
ALL: ALL

如上可设置允许和拒绝通过SSH访问服务器的主机列表
※如果未在host.allow中设置允许访问的主机,仅在host.deny中设置ALL: ALL,一旦注销就不能再访问服务器,请务必注意。

 
 

变更ssh使用的端口号

 
【为什么要变更】
第三方使用端口扫描确认是否在运行SSH服务是,大多情况下只对最高1023的周知端口进行端口扫描。因为,把SSH中使用的端口号从22号端口变更为1024号以上的端口,有效对来自第三方的攻击防患于未然。
1利用SSH登录到服务器

※请一定以root登录

2显示sshd_config文件的编辑画面

输入并执行vi /etc/ssh/sshd_config,就能显示sshd_config文件的编辑画面。
※请注意在vi和/etc之间要输入空格。

[root@vps]# vi /etc/ssh/sshd_config
如果打开的是空白画面,请输入执行q或者q!,返回通常画面,然后请再次执行步骤2的命令。
3显示行编号

ssh_config文件的画面打开后,如果键入:set number(※注意一定以:[冒号]开头键入),就能显示行编号。
※务必在指令模式下运行

#     $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.
set number
4输入希望的端口号

确认文件中第13行处有 “#Port 22” 的字样,用计算机键盘上的箭头键将光标移动到#Port 22的最后,然后按下键盘 “A” 键,进入输入模式。 
进入后此时按下Enter键,另起一行输入希望的端口号。
端口号设置必须以大写的P开头,键入Port XXX,※ XXX 的位置请写入希望的端口号。

※图片中是使用端口号3843的例子。
  希望使用其他端口号时,请指定与正在使用的端口号不重复的编号。

3     #Port 22
14    Port 3843
15    #Protocol 2,1
16    Protocol 2
17    #AddressFamily any

- - INSERT - -

※输入模式下,在画面左下方将显示“--INSERT--”或者“--插入--”。

5输入后按下键盘上的Esc键,进入指令模式

输入后按下键盘的Esc,进入指令模式。
请确认画面左
下方的 “-- INSERT --” 或者 “-- 插入 --” 消失。

进入指令模式后,键入 :wq

13    #Port 22
14    Port 3843
15    #Protocol 2,1
16    Protocol 2
17    #AddressFamily any

:wq
6键入/etc/init.d/sshd restart,重新启动sshd。
 
[serversman@vps]# /etc/init.d/sshd restart
Stopping sshd:                  [OK]
Starting sshd:                   [OK]
※重新启动指令也可以使用/etc/rc.d/init.d/sshd restart
※使用Debian GNU Linux时,在这里要使用/etc/init.d/ssh restart进行重新启动。

如上可变更连接SSH时使用的端口号。
再次通过SSH访问ServersMan@VPS服务器时,请使用变更后的端口号访问。