1、查找主机的ip方式
操作: ifconfig eth0
注:如图所示,本机的ip为172.25.254.121
2、openssh
当主机中开启openssh服务,远程连接的接口也开启了。
sshd : openssh服务的客户端
ssh : openssh服务的客户端
3、客户端连接sshd
操作: ssh [email protected]
意义: 用ssh命令连接ip为172.25.254.121的主机的root用户
以上显示,已连接成功。若要退出登录,只需输入命令exit即可。
4、给ssh服务添加认证方式
(1)生成公钥和私钥: [root@localhost ~ ]#ssh-keygen
总结:两种方式免密远程连接(其中desktop为客户端。server为服务端)
<1>在客户端(desktop)建立新的公钥和私钥,将公钥发送给服务器(server),客户端(desktop)可通过ip远程在服务器(server)进行操作;
操作:desktop:ssh-keygenssh-copy-id -i id_rsa.pub [email protected]
操作:server:ssh-keygen
scp id_rsa [email protected]:/root/.ssh
ssh-copy-id -i id_rsa.pub [email protected]
dektop: ssh [email protected]
图 1
图 2
图 3
图解:图 1 、图 2 在 server下操作 ,生成锁和钥匙,将钥匙发给客户端,将锁发给自己; 图 3 在desktop下操作,验证是否可以实现免密连接。这里,我们需要注意的是:server 生成的锁 id_rsa.pub 和 发送给自己的锁是其实是一样的。
5、sshd的安全配置
sshd安全配置文件位置: /etc/ssh/sshd_config
禁止原始认证方式:
78 PasswordAuthentication no/yes //开启或关闭ssh的默认认证方式
48 PermitRootLogin no/yes //开启或关闭root用户的登录权限
79 AllowUsers student //用户白名单,当前设定是只允许student登录
80 DenyUsers linux //用户黑名单,除了linux之外的所有用户可以登录
注:当修改完文件后,注意需要让服务器重新读取文件,systemctl restart sshd .这样文件修改才会生效。
6、linux服务管理
systemctl start sshd //开启服务
systemctl stop sshd //停止服务
systemctl status sshd //查看服务状态
systemctl restart sshd //重启服务
systemctl reload sshd //让服务重新加载配置
systemctl enable sshd //设定服务开启启动
systemctl disable sshd //设定服务开机不启动