ssh服务:

管理服务器的方式:

本地管理 (安装系统、故障修复)

SSH远程连接的方式

Linux: ssh命令

Windows:

Xshell; Xmanager

SecureCRT

Putty 


提供ssh服务/ssh客户端工具的软件:


[root@localhost ~]# rpm -qa | grep ssh

openssh-server-6.6.1p1-33.el7_3.x86_64

openssh-clients-6.6.1p1-33.el7_3.x86_64


[root@localhost ~]# systemctl status sshd    ------>查状态


[root@localhost ~]# ss -antp | grep sshd

LISTEN     0      128          *:22                       *:*                   users:(("sshd",pid=1202,fd=3))


1、远程连接主机 


# ssh [user@]host 

# ssh 192.168.87.10

# ssh marin@192.168.87.10 

2、远程连接主机执行命令 


# ssh 192.168.87.10 'hostname' 


[root@localhost ~]# ssh martin@192.168.122.105 'hostname'




3、远程复制文件的工具 


scp, rsync (增量复制)


[root@node01 ~]# scp /etc/fstab 192.168.122.121:/tmp/


[root@node01 ~]# scp 192.168.122.121:/etc/passwd /tmp/


-r:复制目录


rsync


[root@node01 ~]# rsync -av /bj/ 192.168.122.121:/sh


[root@node01 ~]# rsync -av /bj 192.168.122.121:/sh



配置文件:/etc/ssh/sshd_config 


1) 关闭SSH对主机名的解析 


GSSAPIAuthentication no

UseDNS no


[root@node1 ~]# systemctl restart sshd 



2) 禁用root用户远程连接 


PermitRootLogin no


3) 修改默认的SSH端口 


Port 22345

ListenAddress 192.168.122.121


[root@server ~]# ssh martin@192.168.87.10 -p 22345



关闭SELinux和防火墙


# setenforce 0

# vim /etc/sysconfig/selinux 


关闭防火墙 


# systemctl stop firewalld

# systemctl disable firewalld


SSH认证方式:


基于用户名、密码;默认 

基于密钥

基于密钥的配置方法:

1、在客户端生成密钥对 

2、把公钥传送给服务器 



1) 在客户端生成密钥对

 

[root@server ~]# ssh-keygen -t rsa


[root@server ~]# ls .ssh/

id_rsa  id_rsa.pub  known_hosts


id_rsa 私钥

id_rsa.pub 公钥



2) 把公钥传送给服务器


[root@server ~]# ssh-copy-id -i -p 22345 192.168.87.10