ssh 操作

安装

  1. sudo apt-get install openssh-client
  2. sudo apt-get install openssh-server

高级特性

  1. 1. 基础命令
  2. ssh-keygen -t rsa -P "密码" //创建非对称秘钥
  3. ssh-keygen -t rsa //不需要密码的
  4. ssh www@192.168.1.24 //链接远程主机
  5. ssh-add -l //查看添加的key缓存
  6. ssh-add -D //清除缓存
  7. ssh -t ac@ad command 执行远程命令
  8. ssh -vvv ac@ad command 显示调试模式
  9. ssh-add ~/.ssh/id_rsa 添加本地 key 到缓存
  10. ssh-copy-id -i ~/.ssh/id_rsa.pub username@hostname 拷贝本机公钥到远程服务器
  11. 执行远程脚本,需要免密码登陆
  12. ssh -t -p 22 hadoop@192.168.160.45 touch /tmp/aaaaa.txt
  13. *. 注意事项
  14. 目标 ~/.ssh/authorized_keys 权限必须是 -rw-------
  15. chmod 600 ~/.ssh/authorized_keys
  16. 2.远程执行命令配置
  17. usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
  18. [-D [bind_address:]port] [-e escape_char] [-F configfile]
  19. [-I pkcs11] [-i identity_file]
  20. [-L [bind_address:]port:host:hostport]
  21. [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
  22. [-R [bind_address:]port:host:hostport] [-S ctl_path]
  23. [-W host:port] [-w local_tun[:remote_tun]]
  24. [user@]hostname [command]
  25. -l 指定登入用户
  26. -p 设置端口号
  27. -f 后台运行,并推荐加上 -n 参数
  28. -n 将标准输入重定向到 /dev/null,防止读取标准输入
  29. -N 不执行远程命令,只做端口转发
  30. -q 安静模式,忽略一切对话和错误提示
  31. -T 禁用伪终端配置
  32. // 远程执行命令格式
  33. ssh [options][remote host][command]
  34. ~/.bashrc 里面,如果不是交互模式会推出,如果特殊需求,这里注释掉
  35. # If not running interactively, don't do anything
  36. case $- in
  37. *i*) ;;
  38. *) return;;
  39. esac
  40. 案例:必须在目标的服务器,使用脚本作为容器来执行指定 shell
  41. ssh -q -t dwadmin@bi2 "bash -i /usr/local/hive/bin/hive -e 'show tables;'"
  42. ssh -q -t dwadmin@10.10.2.91 "bash -i /usr/local/hive/bin/hive -e \"LOAD DATA LOCAL INPATH '/data/log/uba/uba_web_visit_20151201.log' OVERWRITE INTO TABLE real_time.uba_web_visit_log\""
  43. 3. 根据私钥生成公钥
  44. openssl
  45. 输入
  46. rsa -in /home/hadoop/.ssh/id_rsa -pubout -out /home/hadoop/.ssh/id_rsa.pub