Using SSH
2014-08-21 本文已影响68人
zddav
SSH -- 绝对是个神器,功能巨多,不一而足,不定时更新,主要看我什么时候用到_
省掉每次都要输密码
每次都要输密码还是比较烦人的,那可以使用工具ssh-keygen生成key来做自动授权来达到自动登录的目的。
~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/david/.ssh/id_rsa): /tmp/id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /tmp/id_rsa.
Your public key has been saved in /tmp/id_rsa.pub.
The key fingerprint is:
75:da:19:87:c5:3e:7e:de:bb:28:8e:58:de:44:c7:23 david@kr1100-ProLiant-BL460c-G7
The key's randomart image is:
+--[ RSA 2048]----+
| .. |
| o. |
| . +.. |
| . +.+o |
| S .Eo= . |
| . o o .|
| . . o.|
| + +. . o|
| . o.o.. oo|
+-----------------+
~$ ls /tmp/id_rsa*
/tmp/id_rsa /tmp/id_rsa.pub
~$ scp /tmp/id_rsa.pub xxx@xxx.xxxx.xxx.xxx:/tmp/id_rsa.pub
~$ ssh xxx@x.x.x.x
~$ cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys
给主机设置别名
同样,可以更近一步,设置主机别名来缩减输入。
~$ cat ~/.ssh/config
Host test1
hostname 255.255.255.255
user zddav
以后直接输入别名就可以直接登录到远程主机了,配合前面的设置同时可以省掉密码输入。
~$ ssh test1
挂载到本地
- 手动挂载
# sshfs zddav@xxx.xxx.xxx.xxx:/home/zddav/ /mnt
- 自动挂载
user $ tail -1 /etc/fstab
${user}@${IP}:${dir} /home/$USER/ss fuse.sshfs defaults,_netdev,Identityfile=/home/$USER/.ssh/id_rsa,allow_other,default_permissions,uid=1000,gid=1000,port=${PORT} 0 0
注意:如果本地用户名和远程用户名不一样,需要去掉“default_permissions”, 不然没有写权限
参考:
ArchWiki
google keyword [sshfs /etc/fstab port]
PuttyGen生成的key没法使用“Server refused our key”
这个问题是因为key的格式不对,需要做一些手动编辑
原来是这样的(注意换行及前后的分段信息):
david@ubuntu:~$ cat .ssh/id_rsa.pub
---- BEGIN SSH2 PUBLIC KEY ----
Comment: "rsa-key-20140821"
AAAAB3NzaC1yc2EAAAABJQAAAQEAgnslAoiM66N9zpyEVOQ6nFQBWOokWnT3GM9j
HR8CB23/1Pv2XMif/VN8OamTS8uRsc9098fKNUhyL5nRElqe2vFptpyJIwo2/xzq
jwVHq2pr2C3L/rKvFl/tiGTWkmjF4WC/2Phq2Po7O4JRy7f2gmqE/t3F7gDAiTlm
k+AnHRP0DlIkaAZ56QwxKlUNPuSR3eSz1o6FmxRxd95Iod4oPpePNkluCkG8WcBy
Tt7JGM+gK37ERAWFUN9DNgXdg8prPT/AxpEmNZYM//ZX+enL/doSDpQCndTNpn6M
FvT++gjyHwOOPos2t0rNtAlUIq29jZ30ShIrO6Vg+/5ZgpUCWQ==
---- END SSH2 PUBLIC KEY ----
将它改成这样(去掉换行和前后的分段信息):
david@ubuntu:~$ cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAgnslAoiM66N9zpyEVOQ6nFQBWOokWnT3GM9jHR8CB23/1Pv2XMif/VN8OamTS8uRsc9098fKNUhyL5nRElqe2vFptpyJIwo2/xzqjwVHq2pr2C3L/rKvFl/tiGTWkmjF4WC/2Phq2Po7O4JRy7f2gmqE/t3F7gDAiTlmk+AnHRP0DlIkaAZ56QwxKlUNPuSR3eSz1o6FmxRxd95Iod4oPpePNkluCkG8WcByTt7JGM+gK37ERAWFUN9DNgXdg8prPT/AxpEmNZYM//ZX+enL/doSDpQCndTNpn6MFvT++gjyHwOOPos2t0rNtAlUIq29jZ30ShIrO6Vg+/5ZgpUCWQ==