sshd服务

http://blog.sina.com.cn/s/blog_5dbd618201011mwn.html

1 ssh服务

ssh(tcp 端口号22):安全的命令解析器

为客户机提高安全的shell环境,用于远程管理

SSH基于公钥加密(非对称加密)技术: 数据加密传输; 客户端和服务器的身份验证;
公钥 和 私钥   是成对生成的,这两个密钥互不相同,两个密钥可以互相加密和解密;
                   不能根据一个密钥而推算出另外一个密钥;
                   公钥对外公开,私钥只有私钥的持有人才知道;

2.安装 

[root@yuguangyuan ssh]# yum install openssh* -y

3.查看安装结果

[root@yuguangyuan ssh]# rpm -qa | grep ssh
openssh-clients-5.3p1-118.1.el6_8.x86_64
openssh-server-5.3p1-118.1.el6_8.x86_64
openssh-ldap-5.3p1-118.1.el6_8.x86_64
libssh2-1.4.2-1.el6.x86_64
openssh-5.3p1-118.1.el6_8.x86_64
openssh-askpass-5.3p1-118.1.el6_8.x86_64

3.1.查看安装生成的文件

[root@yuguangyuan ssh]# rpm -ql openssh
/etc/ssh
/etc/ssh/moduli
/usr/bin/ssh-keygen
/usr/libexec/openssh

3.2 常用配置文件

/etc/ssh/ssh_config 客户端配置文件

/etc/ssh/sshd_config 服务端配置文件

4.启动

service sshd restart/stop/start/status

/etc/init.d/sshd restart/stop/start/status

/usr/sbin/sshd restart/stop/start/status

[root@yuguangyuan ssh]# service sshd status
openssh-daemon (pid 1394) is running...
[root@yuguangyuan ssh]# /etc/init.d/sshd status
openssh-daemon (pid 1394) is running...

4.1设置开机自启动

[root@yuguangyuan ssh]# chkconfig sshd on
[root@yuguangyuan ssh]# chkconfig --list sshd
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@yuguangyuan ssh]#

4.2查看端口号:

http://www.cnblogs.com/peida/archive/2013/03/11/2953420.html(ss命令)

[root@yuguangyuan ssh]# netstat -anlptu | grep sshd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1394/sshd 
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 1317/sshd 
tcp 0 52 192.168.1.130:22 192.168.1.107:11068 ESTABLISHED 1317/sshd 
tcp 0 0 :::22 :::* LISTEN 1394/sshd 
tcp 0 0 ::1:6010 :::* LISTEN 1317/sshd 
[root@yuguangyuan ssh]# ss -tlunp | grep sshd
tcp LISTEN 0 128 :::22 :::* users:(("sshd",1394,4))
tcp LISTEN 0 128 *:22 *:* users:(("sshd",1394,3))
tcp LISTEN 0 128 127.0.0.1:6010 *:* users:(("sshd",1317,8))
tcp LISTEN 0 128 ::1:6010 :::* users:(("sshd",1317,7))
[root@yuguangyuan ssh]#

5.使用:

[root@yuguangyuan ssh]# useradd ygy && echo 123456 | passwd --stdin ygy
Changing password for user ygy.
passwd: all authentication tokens updated successfully.
[root@yuguangyuan ssh]# ssh ygy@192.168.1.130
ygy@192.168.1.130's password:
[ygy@yuguangyuan ~]$ w
07:18:14 up 53 min, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.1.107 06:25 0.00s 0.12s 0.00s ssh ygy@192.168.1.130
ygy pts/1 192.168.1.130 07:18 0.00s 0.00s 0.00s w

使用root登陆 ssh 192.168.1.130

ssh -l ygy 192.168.1.130

ssh ygy@192.168.1.130

6.安全配置

 6.1修改默认端口

[root@yuguangyuan ssh]# vim sshd_config 

重启:service sshd restart

[root@yuguangyuan ssh]# netstat -tlunp | grep sshd
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 1317/sshd
tcp 0 0 0.0.0.0:222 0.0.0.0:* LISTEN 1659/sshd
tcp 0 0 ::1:6010 :::* LISTEN 1317/sshd
tcp 0 0 :::222 :::* LISTEN 1659/sshd

指定端口-p

 [root@yuguangyuan ssh]# ssh -p 222 192.168.1.130

6.2不用root登陆

useradd yy 创建用户

passwd yy 创建密码

修改/etc/passwd 设置root /sbin/nologin

        设置 yy:x:0:0

这样要登陆就能使用yy冒充root登陆。

错误登陆者

lastb

查看一下这个文件的大小,如果很大,说明服务器被攻击了

[root@ygy130 ~]# ll -h /var/log/btmp 

下面引出fail2ban的服务。

原文地址:https://www.cnblogs.com/yuguangyuan/p/5880085.html