1, 修改ssh链接端口号
[root@linux ~]# vi /etc/ssh/sshd_config
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options change a
# default value.
Port 8284      //以前这个前面是有 # 号的,而且默认是 22 ,修改一下就ok了
#Protocol 2,1
#ListenAddress 0.0.0.0
重起ssh服务,修改端口才生效
[root@linux ~]# /etc/init.d/sshd restart
Stopping sshd:                                             [ OK ]
Starting sshd:                                             [ OK ]
[root@linux ~]#
为了检验是否正确,可以使用 netstat -an 命令查看一下。
 
2, 修改默认root用户不能通过ssh登陆
修改之前,请先创建一个用户,ssh登陆之后,在su到root用户来操作。
[root@linux ~]#  vi /etc/ssh/sshd_config
#LoginGraceTime 2m
#PermitRootLogin yes     修改为
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
 
重启ssh服务,方法上面有的。
 
3,. bash_history 文件不记录历史命令
/root/.bash_profile  文件里面添加一行    
 HISTSIZE=0     就OK啦
 
退出重新登录,这样设置后,向上向下箭头就不再显示曾经输入的命令
 
4, 记录命令执行的时间
[root@linux ~]#  vi /etc/bashrc
 
后面添加
HISTFILESIZE=2000
HISTSIZE=2000
HISTTIMEFORMAT="%Y%m%d-%H%M%S:"
export HISTTIMEFORMAT
 
退出重新登录测试
[root@linux ~]#  cat .bash_history
#1260352080
history
#1260352089
cat /dev/null >.bash_history
#1260352093
exit
 
这个时间叫做Unix time,是从1970年1月1日临时起,到现在一共经过了多少秒
因为1969年是Unix系统诞生,因此1970年1月1日被规定为Unix系统诞生的时间的初始
Linux系统因为和Unix系统的相似性,也完全采用这种方式来记录时间
按照年月日方式来显示时间,执行History命令来查看,就可以了,例如:
 
[root@mailsvr12 ~]# history
    1  20091209-174800:history
    2  20091209-174809:cat /dev/null >.bash_history
    3  20091209-174813:exit
    4  20091209-174818:history
    5  20091209-175018:cat .bash_history
    6  20091209-175132:history
 
注意:本方法必须在服务器刚刚新安装好时候,就设置这个参数。如果是已经运行了很久的服务器才添加这个参数,则以前的那些命令历史记录是不显示时间的。
可以先清空.bash_history文件,以后的记录就会正常
方法:
[root@mailsvr12 ~]# cat /dev/null /root/.bash_history
 
5, 禁止ping
[root@mailsvr12 ~]#  echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
 
如果想恢复,只需要将上面命令的1修改为0,在执行一遍。
 
6, 限制允许的用户ssh登录
[root@mailsvr12 ~]# vi /etc/sshd_config
在最后一行加入一行,
AllowUsers user
 
#user是允许连接的用户
如果是多个用户的话:AllowUsers A B C
注意中间是空格,而不是分号,接下来重启ssh
[root@mailsvr12 ~]# /etc/init.d/sshd restart
 
配置完成之后,只有允许的用户才可以登录,其他用户包括root用户都是不能登录的。其他用户登录之后,su到root用户。
 
7, ssh使用证书登录,无需输入密码,限制使用密码来登录
 
先生成证书
 
[root@app1 ~]# ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
ce:f4:9f:0c:b7:53:81:3b:c1:cf:d2:21:d0:5e:38:70 root@app1
 
[root@app1 ~]# cd /root/.ssh/
我们需要修改公钥的文件名,因为openssh缺省的文件名是authorized_keys
[root@app1 .ssh]# cp id_rsa.pub authorized_keys
 
把id_rsa.pub(公钥),id_rsa(私钥)这两个文件下载回到本地,放到同一个文件夹下,这是关键。例如我下载到D:\soft\ssh\
 
接下来配置SecureCRT
 
新建一个会话
 
 
取消口令 然后选中公钥,点击右面的属性。
 
 
选择使用会话公钥设置,浏览到私钥的路径。确定之后,测试下,不需要输入密码,即可登录进去。
 
取消使用密码登录方法
vi/etc/ssh/sshd_config
将#PasswordAuthentication yes改为
PasswordAuthentication no
 
重启ssh
 
完成之后,只能使用证书来登录,就算使用root用户密码也是无法登录的。这里大家也可以使用其他用户来验证,不一定使用root用户。
 
未完待续