Skip to content

Instantly share code, notes, and snippets.

@zhengkai
Last active October 31, 2023 10:10
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save zhengkai/15d5203b6bbfd9bc1594 to your computer and use it in GitHub Desktop.
Save zhengkai/15d5203b6bbfd9bc1594 to your computer and use it in GitHub Desktop.
把 SSH 用起来

把 SSH 用起来

by 郑凯 zhengkai@gmail.com
2014.10.23

0. Chrome 插件

Secure Shell
by Google
https://chrome.google.com/webstore/detail/secure-shell/pnhechapfaindjhompbnflcldabbghjo
密钥放到邮箱里,能打开 Chrome 浏览器的地方就能 SSH

推荐 Windows 下再装点字体:
Google Fonts https://fonts.google.com/
左上 Categories 里,除了 Monospace 都取消

1. 拒绝明文密码

优点:

  1. 安全
  2. 方便管理

ssh-keygen 命令生成密钥对
默认叫是 id_rsa(私钥) 和 id_rsa.pub(公钥)
多组密钥靠参数来区分
ssh -i my_key_rsa host
或者在 ~/.ssh/config 里设定 IdentityFile

公钥的意思真的是公开,你把公钥印到报纸上都可以
我的公钥 https://zhengkai.github.io/

禁用明文登录:/etc/ssh/sshd_config
PasswordAuthentication no

PS: 另严厉谴责 Putty 这种不用 OpenSSH 格式公钥的异端

2. 配置文件 ~/.ssh/config

配置 demo https://github.com/zhengkai/config/blob/master/ssh/config
怕被社工所以只放了自己机器的

保持连接

ControlMaster auto
ControlPath ~/.tmp/ssh_mux_%h_%p_%r
ControlPersist 4h

配置多个

Host deploy
    User kai.zheng
    IdentityFile ~/.ssh/key/deploy_rsa
    HostName 214.130.251.3

给机器起名字,而不是记 IP:Julia、Sonia、Tesla、Molly、Monk、Freya

3. 端口映射瑞士军刀

翻墙常用 ssh -D 55777 host

检测使用 curl --socks5-hostname 127.0.0.1:55777 https://www.facebook.com/
想长期使用还得 autossh -qTfnNC
Proxy SwitchySharp https://chrome.google.com/webstore/detail/dpplabbmogkhghncfbfdeeokoefdjegm
单纯的翻墙 SS http://shadowsocks.org 更好,但是需要装服务器端

远程开端口 ssh -R 55003:127.0.0.1:22 host
本地开端口 ssh -L 55003:127.0.0.1:22 host

4. ssh 是基础设施

cp -r /mnt/foo /mnt/bar
scp -r molly:/mnt/foo /mnt/bar

alias sshsync='rsync --partial -vzrtopg -e ssh'
sshsync molly:/mnt/bar /mnt/foo

git clone git@bitbucket.org:yitao/royalstory-server-code.git /www/royal
git clone qa:/www/qa/.git /www/royal

5. 题外话,权限问题,跟 ssh 关系不大

一群有 sudo 权限的账号,比共用一个 root 账号要好

多用 sudo,少用 root
sudo visudo
%sudo ALL=(ALL:ALL) NOPASSWD: ALL

多人读写一个目录:
错误方法:umask 002
正道:Access Control Lists (sudo apt-get install acl

/etc/fatab 设置(线上的xfs貌似自带该功能,不需要额外设置)

UUID=7f08728f-6e3f-4fbd-8531-9ba6258e43a8 /      ext4   noatime,acl 0   1

脚本 https://github.com/zhengkai/conf/blob/master/bin/acl

setfacl    -R -m g:$1:rwX $2

目录有 +号表示成功

drwxrwxrwx+ 13 zhengkai zhengkai 4.0K Oct 11 16:22 royal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment