w查看兩臺機器的鏈接數,從哪裏鏈接的。linux
用2號機器ssh1號機器。shell
[root@centos02 ~]# ssh 192.168.237.128 The authenticity of host '192.168.237.128 (192.168.237.128)' can't be established. ECDSA key fingerprint is SHA256:85mFXFmTEONGhiXkOy1wXiSc/ArENhDNw+IEqmnJ5vY. ECDSA key fingerprint is MD5:a4:d9:13:b1:30:42:f4:5a:3d:9a:3c:e2:aa:85:ae:e8. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.237.128' (ECDSA) to the list of known hosts. root@192.168.237.128's password: Last login: Sat Feb 13 22:34:13 2021 from 192.168.237.1
此時再用w查看鏈接狀況centos
[root@centos01 ~]# w 22:52:41 up 18 min, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.237.1 22:34 9:13 0.01s 0.01s -bash root pts/1 192.168.237.127 22:49 1.00s 0.02s 0.01s w
[root@centos01 ~]# w 22:43:31 up 9 min, 1 user, load average: 0.00, 0.03, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.237.1 22:34 3.00s 0.01s 0.00s w [root@centos01 ~]# w 22:52:52 up 19 min, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.237.1 22:34 4.00s 0.01s 0.00s w root pts/1 192.168.237.127 22:49 12.00s 0.01s 0.01s -bash
兩臺機器已經互聯。bash
ssh也可用另外兩種方式鏈接。網絡
1.用戶名 ssh username@ipdom
2.指定端口 ssh -p 端口 ipssh
[root@centos02 ~]# ssh root@192.168.237.128 root@192.168.237.128's password: Last login: Sat Feb 13 22:49:02 2021 from 192.168.237.127 [root@centos01 ~]# exit 登出 Connection to 192.168.237.128 closed. [root@centos02 ~]# ssh -p 22 192.168.237.128 root@192.168.237.128's password: Last login: Sat Feb 13 22:59:40 2021 from 192.168.237.127
在2號機設置私鑰,1號機公鑰ide
輸入ssh-keygen後提示密鑰對的存放路徑,這裏我選擇默認回車。以後提示輸入密碼,默認爲空,回車。3d
[root@centos02 ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:MOLQdi6VT8VQ3e4+v4QJ2Jjh/cGzE7sFBf+229EK9c8 root@centos02 The key's randomart image is: +---[RSA 2048]----+ | .+o. o | | . . .. . + | | . + * .. . o | | + = =. B . o .| | o . S= + B. o| | . o.@oo| | .O =o| | .Bo=| | ..=E| +----[SHA256]-----+
複製公鑰到1號機,註釋是哪裏來的公鑰。code
[root@centos02 ~]# ls /root/.ssh/ authorized_keys id_rsa id_rsa.pub known_hosts [root@centos02 ~]# cat /root/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJRiZvX1F/yeqry6Jnp16pMPI0C1Tc+5WGTsEdcX6M66EPDeLZMq0/4rSTQBK5R5C2VXNPQJD5Jw6XM+lTTHRF1pHHAhSZiZ/GYKYRS8jNwN3mBJa60uI8VZv6UJNFeeelm44djb1q+iCepcYDO0WW0ug2bYJC5BnA9X5uYe0ENAhNBnZqSWgUAcduXkEBoedAgMRp86ttKB7UyGJ3Xlr9/dAoSWX4ZZLvBwDu+VGNQcunR50L+o85bNKc54ZTuhQnDOhdjIKZDe88WZTQshdNPWli2gFIgFVf1JkB7OvqjTOtgWLy9I+ab/UTnmXlTrd8qjd2lVk9C5uxv0IiYyLB root@centos02
[root@centos01 ~]# vi /root/.ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAt1vgyaIFQhOxm6AS70JW7HCd+eU9CjNJT3uW6U6+/qksyZvLwQcOY90VzO8thvSlIWaQFA62VlV7Ovq2ZV7zhdjm/JFeAJhWrbsF0td9KBQmf++Q/niePVnkkibRzzqRrHlJe0t9LYmCn7kr7aGKUA26hUIvhR0/30l7Bmcv8cgMEYaapWgAjCMszARIO2AIOkBPDGLpN4clhHNbbteFLBEpGDPHVzw3PUgymhQuGni1jkprYpO4oYDL5huk1t30TZVX7mEdB5Z5KvO543/8wU9xmT2D104QhvorYKTGTGUCMlP7LAkPeuFjNE5EwjoGXNgnWVKQ6RF8HgAaGFPvDw== ##centos 7-Xshell ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJRiZvX1F/yeqry6Jnp16pMPI0C1Tc+5WGTsEdcX6M66EPDeLZMq0/4rSTQBK5R5C2VXNPQJD5Jw6XM+lTTHRF1pHHAhSZiZ/GYKYRS8jNwN3mBJa60uI8VZv6UJNFeeelm44djb1q+iCepcYDO0WW0ug2bYJC5BnA9X5uYe0ENAhNBnZqSWgUAcduXkEBoedAgMRp86ttKB7UyGJ3Xlr9/dAoSWX4ZZLvBwDu+VGNQcunR50L+o85bNKc54ZTuhQnDOhdjIKZDe88WZTQshdNPWli2gFIgFVf1JkB7OvqjTOtgWLy9I+ab/UTnmXlTrd8qjd2lVk9C5uxv0IiYyLB root@centos02 ##centos02
檢查2號機可否連上1號機
[root@centos02 ~]# ssh -p 22 192.168.237.128 Last login: Sat Feb 13 23:01:22 2021 from 192.168.237.127 [root@centos01 ~]#
此時鏈接成功,且不須要密碼。
若是鏈接不成功,檢查1號機selinux是否有關閉。
[root@centos01 ~]# getenforce Enforcing [root@centos01 ~]# setenforce 0 [root@centos01 ~]# getenforce Permissive [root@centos01 ~]#