sshd服务

sshd服务

服务的软件包 openssh-server

ssh —Secure SHell 安全shell连接

功能:实现加密方式的远程连接

ssh远程连接的软件
linux:openssh-clients
windows:xmanager Xshell putty SecureCrt

默认端口及协议
tcp/udp 22

环境:
server 192.168.1.251 server.up.com
client 192.168.1.250 client.up.com

一般情况下,安装操作系统时默认就安装了ssh。

一、查看软件包是否安装

rpm -qa | grep openssh
openssh-server-6.6.1p1-31.el7.x86_64 #服务器端软件包
openssh-clients-6.6.1p1-31.el7.x86_64 #客户端软件包

二、服务器端的配置文件

vim /etc/ssh/sshd_config
17 #Port 22 #默认端口22
18 #AddressFamily any #支持任意地址簇(ipv4/ipv6)
19 #ListenAddress 0.0.0.0 #监听在任何ipv4地址上
20 #ListenAddress :: #监听在任何ipv6地址上
23 #Protocol 2 #协议版本
43 SyslogFacility AUTHPRIV #定义日志的对象
44 #LogLevel INFO #定义日志的级别
48 #LoginGraceTime 2m #连接2分钟不输入密码就断开
49 #PermitRootLogin yes #允许管理员root登陆
50 #StrictModes yes #用严格模式
51 #MaxAuthTries 6 #最大尝试6次
52 #MaxSessions 10 #允许最大会话10个
78 #PermitEmptyPasswords no #密码为空不能登陆
79 PasswordAuthentication yes #是否采用密码认证
115 X11Forwarding yes #开启x11转发协议
127 #ClientAliveCountMax 3 #客户端连接服务器输入密码尝试的次数
129 #UseDNS yes #采用dns解析
130 #PidFile /var/run/sshd.pid #pid文件位置

查看网络连接状况:

[root@server .ssh]# netstat -tuanp |grep sshd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 32057/sshd

-a (all)显示所有选项
-t tcp协议
-u udp协议
-l listening
-n 以数字方式显示,不解析
-p 进程名

[root@server .ssh]# lsof -i:22
有返回内容表示服务处于监听状态


UseDNS yes 默认启用dns查询 解析dns导致连接服务器有延时 将此值改为no就可以去掉延时

指纹保存在客户端上 /root/.ssh/known_hosts
不启用指纹 将客户端的配置文件中 /etc/ssh/ssh_config
StrictHostKeyChecking no

ssh的基本使用

守护进程:sshd
# systemctl start sshd
# systemctl restart sshd
# systemctl status sshd 查看服务的状态

远程登陆
指定用户身份的远程登陆
不登陆远程执行命令
远程拷贝

1、远程登陆
[root@localhost ~]# ssh 192.168.1.250
The authenticity of host '192.168.1.250 (192.168.1.250)' can't be established.
ECDSA key fingerprint is 34:e5:01:71:33:ef:76:42:12:da:aa:f9:d2:d8:d3:96.
Are you sure you want to continue connecting (yes/no)? yes    #表示是第一次连接该主机,会问你是否保存指纹信息
Warning: Permanently added '192.168.1.250' (ECDSA) to the list of known hosts.
root@192.168.1.250's password:   #需要输入远程主机的密码
 

1)退出远程登陆
[root@server ~]# exit
 

2)已知主机列表文件------指纹存放的文件
[root@localhost ~]# cd /root/.ssh/
[root@localhost .ssh]# ls
known_hosts
文件是~/.ssh/known_hosts

如果遇到以下错误的话:是由于指纹信息对不上导致的
[root@localhost .ssh]# ssh 192.168.1.250
key_from_blob: remaining bytes in key blob 108
key_read: type mismatch: encoding error
key_from_blob: remaining bytes in key blob 108
key_read: type mismatch: encoding error
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!                      @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
62:4c:4d:13:e9:a2:7d:cc:a8:0a:43:0d:12:9c:c0:ae.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /root/.ssh/known_hosts:2
ECDSA host key for 192.168.1.250 has changed and you have requested strict checking.
Host key verification failed.

如何解决上述错误:(二选一即可)
    a)删除known_hosts文件中与该ip相对应的行
    b)清空known_hosts文件
    [root@localhost .ssh]# > known_hosts
2、指定用户身份登陆
前提:远程主机上需要存在该用户
1)在远程主机上添加一个用户,并设置密码----192.168.1.250
[root@server ~]# useradd jim
[root@server ~]# echo "jim" | passwd --stdin jim
更改用户 jim 的密码 。
passwd:所有的身份验证令牌已经成功更新。
2)连接-------192.168.1.250
[root@localhost ~]# ssh jim@192.168.1.250
    jim@192.168.1.250's password:jim 
    Last login: Fri Jun 30 14:43:07 2017
[jim@server ~]$ exit
登出
Connection to 192.168.1.250 closed.
3、不登陆远程执行命令
[root@localhost ~]# date ; ssh 192.168.1.250 date
2017年 07月 12日 星期三 13:01:26 CST
[root@192.168.1.250's password: 
2017年 07月 12日 星期三 11:36:25 CST
[root@localhost ~]# ssh 192.168.1.250 reboot
4、图形化ssh连接 -X
# ssh 192.168.1.250 xeyes   #打不开
root@192.168.1.250's password: 
Error: Can't open display:

[root@localhost ~]# ssh -X 192.168.1.250 xeyes 
root@192.168.1.250's password: 
能够打开远程主机上xeyes程序
5、远程拷贝
scp   [-r]   源   目的
1)本地文件拷贝给远程主机   上传
[root@localhost ~]# echo 192.168.1.251 > /tmp/test.txt
[root@localhost ~]# cat /tmp/test.txt
192.168.1.251
[root@localhost ~]# scp  /tmp/test.txt   192.168.1.250:/root/desktop/
root@192.168.1.250's password: 
test.txt       100%   14     0.0KB/s   00:00

2)远程主机文件拷贝到本地  下载
[root@localhost ~]# scp  192.168.1.250:/root/desktop/dianming.sh    /tmp
root@192.168.1.250's password: 
dianming 100%  150     0.2KB/s   00:00    
[root@localhost ~]# ls /tmp/dianming.sh 
/tmp/dianming.sh

常用选项:
    -r  拷贝目录
    -p port:指定端口
    
更改端口设置
[root@server ~]# vim /etc/ssh/sshd_config 
Port 2000
[root@server ~]# systemctl reload sshd
[root@server ~]# netstat -tulnp | grep sshd
tcp        0      0 0.0.0.0:2000              0.0.0.0:*               LISTEN      27227/sshd          
tcp6       0      0 :::2000                 :::*                    LISTEN      27227/sshd

指定端口登陆
[root@localhost ~]# ssh -p 2000  192.168.1.250
基于密钥的连接:实现的是不输入密码登陆远程主机
    是相对于用户来说的,不需要输入密码的

ssh采用的是基于公钥和私钥的加密技术进行自动化认证。

如果客户端连接服务器无密码。客户端上需要生成一对密钥,公钥传给服务器,那么服务器收到公钥,客户端连接服务器就不需要密码。

客户端配置
1.生成密钥

[root@localhost ~]# ssh-keygen

密钥位置
[root@localhost ~]# cd /root/.ssh/
[root@localhost .ssh]# ls
id_rsa  id_rsa.pub  known_hosts

id_rsa  私钥
id_rsa.pub  公钥
known_hosts  已知主机,存指纹的文件
2.发送公钥给服务器
[root@localhost .ssh]# ssh-copy-id -i 192.168.1.250 
3.测试客户端连接服务器不需要密码
[root@localhost .ssh]# ssh 192.168.1.250
Last login: Wed Jul 12 14:29:27 2017 from 192.168.1.251
[root@server ~]# exit
登出
Connection to 192.168.1.250 closed.

服务器会将客户端的公钥保存在authorized_keys 文件中
[root@server ~]# cd /root/.ssh/
[root@server .ssh]# ls
authorized_keys known_hosts

常见错误:
1.ssh连接出错
检测是否有ip地址
检测服务器和客户端是否能ping通
检测网卡连接方法  
2.做双机互信。连接仍然还有密码
服务端
# 清空此authorized_keys文件内容

客户端
# ssh-add  密钥传送失败用此命令
# cd /root/.ssh
# rm -rf id*
# 重新做密钥重新发送        
3.指纹验证不正确
删除known_hosts文件中对应的ip的指纹

凡是支持tcpwrapper的都可以使用以下两个文件做访问控制:
/etc/hosts.allow
/etc/hosts.deny

ldd which sshd|grep wrap
libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f598bc08000)
注:如果可以搜寻到就是支持访问控制

先看allow,如果有,直接放行;
如果没有,再看deny,如果有,则拒绝,如果没有,则放行

例: 只允许192.168.1.250连接
#cat /etc/hosts.allow
sshd:192.168.1.250

cat /etc/hosts.deny
sshd:* # *表示所有. 还可以写网段,如:192.168.1.0/24

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值