centos7实现远程免密码登录,并用alias命令简化登录名
SOST1主机IP:192.168.60.231
HOST2主机IP:192.168.60.234
我们用host1去免密连接host2
1、在host1上使用ssh-keygen产生公钥私钥对(全程配置都在host1上,过程中只要输入host2的密码)
[root@localhost ~]# ssh-keygen #一路回车即可 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa already exists. Overwrite (y/n)?
用ssh-copy-id将公钥复制到远程机器中
[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.60.234 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.60.234's password: #输入HOST2的root密码 Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.60.234'" and check to make sure that only the key(s) you wanted were added.
到这里,我们用秘钥登录设置已经成功了,可以用输入ssh root@192.168.60.234此时已经不用输入密码了,(这边如果有连接host2,请输入exit退出后在继续下面的步骤)接下来我们用alias简化机制将ssh root@192.168.60.234简化成host2
[root@localhost .ssh]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias host2='ssh root@192.168.60.234' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' [root@localhost .ssh]# alias host2='ssh root@192.168.60.234'#照葫芦画瓢
[root@localhost .ssh]# host2 #此时已经登录host2了,如果两个服务器名称一样可以ifconfig查看ip,输入exit可以退出远程。 Last login: Tue Dec 1 11:54:17 2020 from 192.168.60.231 [root@localhost ~]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.60.234 netmask 255.255.255.0 broadcast 192.168.60.255 inet6 fe80::c0ca:33ff:fea9:6a14 prefixlen 64 scopeid 0x20<link> ether c2:ca:33:a9:6a:14 txqueuelen 1000 (Ethernet) RX packets 26362197 bytes 3009246913 (2.8 GiB) RX errors 0 dropped 17414 overruns 0 frame 0 TX packets 49342 bytes 3961048 (3.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 6059 bytes 562852 (549.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6059 bytes 562852 (549.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0