Write by lyc at 2018-1-28

ssh 总结

1.ssh

  • ssh 是安全的加密协议,用于远程链接linux服务器
  • ssh 默认端口号是22,安全协议版本是ssh2,除了2之外还有ssh1(有漏洞)
  • ssh 服务端主要包含两个服务功能:ssh 远程连接、SFTP服务
  • Linux ssh 客户端包含 ssh 远程连接命令,已经远程拷贝 scp 命令等。

ssh 安装

openssl 远程连接软件, openssh 远程加密软件

1
$ yum install openssh openssh-devel openssl openssl-devel -y

CentOS7 sshd 优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cp -pv /etc/ssh/sshd_config{,.old}
#/bin/sed -i "s/#Port 22$/Port 22/g" /etc/ssh/sshd_config
#/bin/sed -i "s/Port 22$/Port 22/g" /etc/ssh/sshd_config
/bin/sed -i "s/#UseDNS yes/UseDNS no/g" /etc/ssh/sshd_config
/bin/sed -i "s/#RSAAuthentication yes/RSAAuthentication yes/g" /etc/ssh/sshd_config
/bin/sed -i "s/#PubkeyAuthentication yes/PubkeyAuthentication yes/g" /etc/ssh/sshd_config
/bin/sed -i "s/#AuthorizedKeysFile/AuthorizedKeysFile/g" /etc/ssh/sshd_config
/bin/sed -i "s/#StrictModes yes/StrictModes yes/g" /etc/ssh/sshd_config
/bin/sed -i "s/#PasswordAuthentication yes/PasswordAuthentication yes/g" /etc/ssh/sshd_config
/bin/sed -i "s/PasswordAuthentication no/PasswordAuthentication yes/g" /etc/ssh/sshd_config
/bin/sed -i "s/PermitEmptyPasswords yes/PermitEmptyPasswords no/g" /etc/ssh/sshd_config
/bin/sed -i "s/#PermitEmptyPasswords no/PermitEmptyPasswords no/g" /etc/ssh/sshd_config
/bin/sed -i "s/#PermitRootLogin yes/PermitRootLogin yes/g" /etc/ssh/sshd_config
/bin/sed -i "s/PermitRootLogin no/PermitRootLogin yes/g" /etc/ssh/sshd_config
systemctl enable sshd
systemctl restart sshd

2.ssh 命令

ssh 远程连接

1
2
3
4
5
$ ssh -p22 root@192.168.99.207                                  # 远程连接
$ ssh -p22 root@192.168.99.207 [command] # 以指定用户身份登入目标服务器执行特定命令

$ ssh -p22 root@192.168.99.207 "ifconfig" # 远程执行一个命令
$ ssh -p22 -i /root/.ssh/id_rsa root@192.168.99.207 "ifconfig" # 指定使用某个私钥执行ssh命令

scp 远程拷贝

  • scp 是加密的远程拷贝,而 cp 仅为本地拷贝
  • 可以把数据从一台机器推送到另一台机器,也可以从其他服务器把数据拉回来到本地执行命令的服务器。
  • 每次都是全量完整拷贝,因此效率不高,适合第一次使用,如果需要增量拷贝,用rsync
1
2
3
4
5
6
7
# scp推
$ scp -P22 -r /data root@192.168.99.207:/tmp/ # 全量推送/data目录
$ scp -P22 /data/1.txt root@192.168.99.207:/tmp/ # 推送单个文件

# scp拉
$ scp -P22 root@192.168.99.207:/data/ /tmp/ # 全量拉取/data目录
$ scp -P22 root@192.168.99.207:/data/1.txt /tmp/ # 拉取单个文件
  • -P 指定服务器端口号
  • -p 表示在拷贝前后拷贝文件或目录的属性
  • -r 递归推送/拉取目录
  • 若不加目标目录,则默认为指定用户的家目录

3.ssh-key 密钥对

ssh-key 密钥对目录

  • /root/.ssh ssh密钥对存放目录,权限 0700
  • /root/.ssh/known_hosts 记录与本机建立过ssh连接的其他主机信息
  • /root/.ssh/authorized_keys 存放ssh其他主机的公钥信息
  • /root/.ssh/id_rsa ssh-key私钥,权限 0600
  • /root/.ssh/id_rsa.pub ssh-key公钥 权限 0644

刚安装的操作系统没有目录 /root/.ssh,执行一次命令 ssh localhost 生成

ssh-key 创建

1
2
3
4
$ cd /root/.ssh
$ ssh-keygen -t rsa # 默认长度 RSA2048

$ ssh-keygen -b 4096 -t rsa -C test@xxx.com -f /home/develop/.ssh/jumpserver_develop

配置ssh免密码:下发公钥

下发公钥到目标主机

1
$ ssh-copy-id -i /root/.ssh/id_rsa.pub  "-p22 root@192.168.99.207"

或复制公钥内容 /root/.ssh/id_rsa.pub ,追加到目标主机的 /root/.ssh/authorized_keys 文件

1
2
3
# 目标主机
$ cat /root/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAxxxxxxxxxxxxxxxxxxxxxxxxxxxSqlsUV8phLSftj8ndB6M9FrTzm1upy/F/Hz7EQb9rwC3bWhTq6obDca0lygfmkhXYlF8DfI2Iub8MIbpX3Q== test@xxx.com