操作Linux是现在任何一家IT公司都要求的技能,而且Linux博大精深,都是以各种命令l来操作,要学会灵活使用,提高工作效率

#ssh免密登录和alias命令结合使用
在测试工作中,对于查看一些问题,一些进程的时候,我们需要登录到服务端的后台机器,去查看进程,查看日志,部署环境搭建服务等,这时候需要登录机器,输入命令和密码,而且我们公司后端机器密码简直变态,每次都要去复制再粘贴过来,而且我们后台服务很多,机器很多,各种后端机器,前端机器,数据源机器,还有好多hadoop机器,每次登录都需要先 ssh 用户名@密码 ,再去复制密码粘贴,一次二次还好,时间久了就会很烦,这个时候就上网查了一些好用东西,免密码登录。

  hulbdeMacBook-Air:1222 dtwave$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/dtwave/.ssh/id_rsa): 
/Users/dtwave/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Passphrases do not match.  Try again.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Passphrases do not match.  Try again.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/dtwave/.ssh/id_rsa.
Your public key has been saved in /Users/dtwave/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:h28fv+BgSv3WtHt+5LgeTMwZZuPhUT7BpaepuhFVf3M dtwave@hulbdeMacBook-Air.local
The key's randomart image is:
+---[RSA 2048]----+
|              o.+|
|             . *.|
|            . OoE|
|         . . B X=|
|        S o   X  |
|         + . +. .|
|        . B +oo= |
|       . + B.++.+|
|        . oo+.**o|
+----[SHA256]-----+
hulbdeMacBook-Air:1222 dtwave$ 
hulbdeMacBook-Air:1222 dtwave$ cd  ~/.ssh
hulbdeMacBook-Air:.ssh dtwave$ ll
total 48
-rw-------  1 dtwave  staff   672  7  4 10:11 id_dsa
-rw-r--r--  1 dtwave  staff   620  7  4 10:11 id_dsa.pub
-rw-------  1 dtwave  staff  1675  8 13 11:41 id_rsa         #私钥
-rw-r--r--  1 dtwave  staff   412  8 13 11:41 id_rsa.pub     #公钥
-rw-r--r--  1 dtwave  staff  4102  8  8 20:50 known_hosts

使用ssh-keygen,如果提示command not found,需要安装该模块

再使用

ssh-copy-id -i ~/.ssh/id_rsa.pub 用户名@ip

命令会将自己的公钥反正远程机器的 ~/.ssh/authorized_keys 文件中。
现在就可以远程免密码登录了

hulbdeMacBook-Air:~ dtwave$ ssh deploy@xx.xx.xx.xx
Last login: Mon Aug 13 15:11:11 2018 from xx.xx.xx.xx

Welcome to Alibaba Cloud Elastic Compute Service !

[deploy@test-shuqi3-base2 ~]$ 

这个时候就还有一个问题,就是输入ssh命令也挺长,每次输入很不方便,还要记着ip地址,这个时候可以使用alias命令来再次简化操作

hulbdeMacBook-Air:~ dtwave$ alias node1="ssh deploy@xx.xx.xx.xx"
hulbdeMacBook-Air:~ dtwave$ node1
Last login: Mon Aug 13 15:11:28 2018 from xx.xx.xx.xx

Welcome to Alibaba Cloud Elastic Compute Service !

[deploy@test-shuqi3-base2 ~]$ 

现在就可以直接输入node1就直接登录远程机器,非常方便,可以自己知道这台机器是干什么的,输入也十分方便,很提升工作效率的。
还有就是alias这个命令只在当前页面生效,重新开一个窗口就没用了,我们需要将这个命令放在 /.bash_profile文件中,我这是mac系统,普通linux会放在/.bashrc。这样每次开启新端口都会读取这项配置。

hulbdeMacBook-Air:~ dtwave$ vim ~/.bash_profile
hulbdeMacBook-Air:~ dtwave$ cat ~/.bash_profile
alias python='/usr/local/bin/python3.6'

alias node1="ssh deploy@xx.xx.xx.xx"
alias server='ssh deploy@xx.xx.xx.xx'
alias  ll="ls -l"
alias node='ssh deploy@xx.xx.xx.xx'
alias python='/usr/local/bin/python3.6'
Logo

更多推荐