1 配置文件路径
 vim /etc/ssh/sshd_config
  • 1.
2 密钥对生成
ssh-keygen -t rsa

#ssh-keygen不加-t默认就是用rss加密方式
#也可以使用ssh-keygen -t dsa 
  • 1.
  • 2.
  • 3.
  • 4.
3 将公钥分发到需要登录的服务器(无密码登录)
#第一种方法
ssh-copy-id  -i .ssh/id_rsa.pub  192.168.1.1

#第二种方法(直接将公钥内容写入到对应用户的authorized_keys中)
scp   /root/.ssh/id_rsa.pub  root@192.168.1.1:/home/
touch   /root/.ssh/authorized_keys   //这个文件若不存在的话,先创建
cat   /home/id_rsa.pub  >> /root/.ssh/authorized_keys
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
4 本地运行远程主机命令方法
#ssh  192.168.116.3 “command”
ssh  192.168.116.3 “ifconfig eth0”
  • 1.
  • 2.
5 服务器安全配置案例
 5.1 ssh登录配置  (酌情配置,一般只需修改端口号禁止root登录)
vim /etc/ssh/sshd_config
  Port 2234  #修改ssh的端口号
 PermitRootLogin no  #禁止root登
PermitEmptyPasswords no   #禁止用密码登录,只能被信任的主机用公私钥登录
配置普通用户sudo权限
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
5.2 ssh连接慢优化配置
vi /etc/ssh/sshd_config      (编辑配置文件)
GSSAPIAuthentication=no
UseDNS= no
  • 1.
  • 2.
  • 3.
6 SSH 隧道
6.1 ssh安全隧道(一):本地端口转发

RHCE 学习笔记之ssh04_端口转发

ssh -L [local_bind_addr:]local_port:remote:remote_port middle_host
ssh -g -L 2222:host2:80 host3  # "-g"选项,指定该选项表示允许外界主机连接本地转发端口(2222),否则host4无法访问host1:2222
#远程执行命令
ssh -g -L 22333:host2:22 host3 "ifconfig"
  • 1.
  • 2.
  • 3.
  • 4.
6.2 ssh安全隧道(二):远程端口转发

 

RHCE 学习笔记之ssh04_vim_02

ssh -R [bind_addr:]remote1_port:host:port remote1
ssh -fgN -R 22333:host2:80 host1 #"-f"选项让ssh在本机host1上以后台方式提供端口转发功能,"-N"不运行远程命令
  • 1.
  • 2.

但是,远程端口转发和本地端口转发最大的一个区别是,远程转发端口是由host1上的sshd服务控制的,默认配置情况下,sshd服务只允许本地开启的远程转发端口(22333)绑定在环回地址(127.0.0.1)上,即使显式指定了bind_addr也无法覆盖。例如:

[root@xuexi ~]# ssh -R *:22333:host2:80 host1 

[root@xuexi ~]# netstat -tnlp
Active Internet connections (only servers)
tcp   0   0 0.0.0.0:22        0.0.0.0:*   LISTEN  8405/sshd  
tcp   0   0 127.0.0.1:25      0.0.0.0:*   LISTEN  1422/master
tcp   0   0 127.0.0.1:22333   0.0.0.0:*   LISTEN  8407/sshd  
tcp   0   0 :::22             :::*        LISTEN  8405/sshd  
tcp   0   0 ::1:25            :::*        LISTEN  1422/master
tcp   0   0 ::1:22333         :::*        LISTEN  8407/sshd
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

要允许本地的远程转发端口绑定在非环回地址上,需要在host1的sshd配置文件中启用"GatewayPorts"项,它的默认值为no。启动该选项后,不给定bind_addrbind_addr设置为"*"都表示绑定在所有地址上。如下:

[root@xuexi ~]# ssh -g -R *:22333:host2:80 host1

[root@xuexi ~]# netstat -tnlp
Active Internet connections (only servers)
tcp   0  0 0.0.0.0:22      0.0.0.0:*   LISTEN  8466/sshd  
tcp   0  0 127.0.0.1:25    0.0.0.0:*   LISTEN  1422/master
tcp   0  0 0.0.0.0:22333   0.0.0.0:*   LISTEN  8468/sshd  
tcp   0  0 :::22           :::*        LISTEN  8466/sshd  
tcp   0  0 ::1:25          :::*        LISTEN  1422/master
tcp   0  0 :::22333        :::*        LISTEN  8468/sshd
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
6.3 ssh安全隧道(三):动态端口转发(SOCKS代理)

RHCE 学习笔记之ssh04_端口转发_03

ssh -D [bind_addr:]port remote 
 ssh -Nfg -D 2222 host3
  • 1.
  • 2.