Linux 远程登录(telnet ssh)

本文涉及的产品
运维安全中心(堡垒机),企业双擎版 50资产 7天
运维安全中心(堡垒机),免费版 6个月
简介:
  •  telnet
 
  1. [root@rhel6 ~]# rpm -qa | grep telnet  
  2. telnet-server-0.17-47.el6.x86_64  
  3. telnet-0.17-47.el6.x86_64  
  4. [root@rhel6 ~]# vi /etc/xinetd.d/telnet                     //telnet是依赖于xinetd的  
  5. defaulton  
  6. # description: The telnet server serves telnet sessions; it uses \  
  7. #       unencrypted username/password pairs for authentication.  
  8. service telnet  
  9. {  
  10.         flags           = REUSE  
  11.         socket_type     = stream  
  12.         wait            = no  
  13.         user            = root  
  14.         server          = /usr/sbin/in.telnetd  
  15.         log_on_failure  += USERID  
  16.         disable         = no  
  17.         instances       = 1                                 //设置服务器最大连接数(即只允许1个用户通过telnet登录)  
  18. #       bind            = 192.168.0.90                      //只允许经由该适配器的数据包进来  
  19. #       only_from       = 192.168.0.0/24                    //只允许该网段通过telnet访问  
  20. #       no_access       = 192.168.0.100                     //不允许该IP通过telnet访问  
  21. #       access_times    = 9:00-18:00                        //telnet服务开放的时间  
  22. }  
  23. [root@rhel6 ~]# /etc/init.d/xinetd restart      
  24. Stopping xinetd:                                           [  OK  ]  
  25. Starting xinetd:                                           [  OK  ]  
  26. [root@rhel5 ~]# telnet rhel6                    
  27. Trying 192.168.0.90...  
  28. Connected to rhel6.  
  29. Escape character is '^]'.  
  30. Red Hat Enterprise Linux Server release 6.2 (Santiago)  
  31. Kernel 2.6.32-220.el6.x86_64 on an x86_64  
  32. login: root  
  33. Password:   
  34. Login incorrect                                             //默认禁止root用户通过telnet登录  
  35. login: xfcy  
  36. Password:   
  37. Last login: Wed Dec 26 17:17:08 from rhel6  
  38. [xfcy@rhel6 ~]$ who  
  39. root     pts/0        2012-12-27 12:01 (192.168.0.90)  
  40. xfcy     pts/1        2012-12-27 12:18 (rhel5)  
  41. [xfcy@rhel6 ~]$ telnet rhel6  
  42. Trying 192.168.0.90...  
  43. Connected to rhel6.  
  44. Escape character is '^]'.  
  45. Connection closed by foreign host.                         //不允许第2个用户通过telnet登录  
  46. [root@rhel6 ~]# netstat -lntp | grep :23                   //默认监听23号端口  
  47. tcp        0      0 :::23                :::*        LISTEN      5169/xinetd        
  48. [xfcy@rhel6 ~]$ vi /etc/services                           //修改telnet服务的监听端口为230  
  49. telnet          230/tcp  
  50. telnet          230/udp  
  51. [root@rhel6 ~]# /etc/init.d/xinetd restart  
  52. Stopping xinetd:                                           [  OK  ]  
  53. Starting xinetd:                                           [  OK  ]  
  54. [root@rhel6 ~]# netstat -lntp | grep :23    
  55. tcp        0      0 :::230                      :::*                        LISTEN      5319/xinetd    
  56. [root@rhel5 ~]# telnet rhel6  
  57. Trying 192.168.0.90...                                     //默认通过23号端口无法访问telnet服务  
  58. telnet: connect to address 192.168.0.90: Connection refused  
  59. telnet: Unable to connect to remote host: Connection refused  
  60. [root@rhel5 ~]# telnet rhel6 230                           //通过230端口可成功访问telnet服务  
  61. Trying 192.168.0.90...  
  62. Connected to rhel6.xfcy.org (192.168.0.90).  
  63. Escape character is '^]'.  
  64. Red Hat Enterprise Linux Server release 6.2 (Santiago)  
  65. Kernel 2.6.32-220.el6.x86_64 on an x86_64  
  66. login: xfcy  
  67. Password:   
  68. Last login: Thu Dec 27 12:50:16 from rhel5  
  69. [xfcy@rhel6 ~]$ netstat -an | grep :23   
  70. tcp        0      0 192.168.0.90:230            192.168.0.89:51147          ESTABLISHED   
  71. tcp        0      0 :::230                      :::*                        LISTEN    

  72. 默认情况下,linux不允许root用户以telnet方式登录linux主机,若要允许root用户登录,可采取以下3种方法之一:
  73. 1.修改login文件
  74. redhat中对于远程登录的限制体现在/etc/pam.d/login 文件中,如果把限制的内容注销掉,那么限制将不起作用。
  75. [root@rhel5 ~]# vi /etc/pam.d/login
  76. #%PAM-1.0 auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so
  77. auth include system-auth
  78. #account required pam_nologin.so
  79. account include system-auth
  80. password include system-auth
  81. # pam_selinux.so close should be the first session rule
  82. session required pam_selinux.so close
  83. session include system-auth
  84. session required pam_loginuid.so
  85. session optional pam_console.so
  86. # pam_selinux.so open should only be followed by sessions to be executed in the user context
  87. session required pam_selinux.so open
  88. session optional pam_keyinit.so force revoke
  89.  
  90. 2.移除securetty文件
  91. 验证规则设置在/etc/securetty 文件中,该文件定义root用户只能在tty1-tty11的终端上记录,移除该文件即可避开验证规则实现root用户远程登录。
  92. [root@rhel5 ~]# mv /etc/securetty /etc/securetty.bak
  93.  
  94. 3.修改securetty文件
  95. [root@rhel5 ~]# vi /etc/securetty
  96. console
  97. vc/1
  98. vc/2
  99. vc/3
  100. vc/4
  101. vc/5
  102. vc/6
  103. vc/7
  104. vc/8
  105. vc/9
  106. vc/10
  107. vc/11
  108. tty1
  109. tty2
  110. tty3
  111. tty4
  112. tty5
  113. tty6
  114. tty7
  115. tty8
  116. tty9
  117. tty10
  118. tty11
  119. pts/1
  120. pts/2
  121. pts/3
  122. pts/4
  123. pts/5
  124. pts/6
  125. pts/7
  126. pts/8
  127. pts/9
  128. pts/10
  129. pts/11
  130.  
  • ssh
 
  1. [root@rhel6 ~]# rpm -qa | grep openssh 
  2. openssh-server-5.3p1-70.el6.x86_64 
  3. openssh-clients-5.3p1-70.el6.x86_64 
  4. openssh-5.3p1-70.el6.x86_64 
  5. openssh-askpass-5.3p1-70.el6.x86_64 
  6.  
  7. [root@rhel6 ~]# cat /etc/ssh/sshd_config  
  8. #Port 22                                        //设置ssh服务的端口 
  9. #MaxStartups 10                                 //设置最大连接数 
  10. #ListenAddress 0.0.0.0 
  11. #PermitRootLogin yes 
  12. Protocol 2                                   //只允许SSH2协议 
  13. SyslogFacility AUTHPRIV 
  14. PasswordAuthentication yes 
  15. ChallengeResponseAuthentication no 
  16. GSSAPIAuthentication yes 
  17. GSSAPICleanupCredentials yes 
  18. UsePAM yes 
  19. AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES 
  20. AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT 
  21. AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE 
  22. AcceptEnv XMODIFIERS 
  23. X11Forwarding yes    
  24. Subsystem       sftp    /usr/libexec/openssh/sftp-server 
  25.  
  26.  
  27. 当客户端登入远程服务器时,客户端会主动的接收到的服务器的公钥(public key) 去比对 ~/.ssh/known_hosts 有无相关的公钥, 然后进行底下的动作: 
  28. 若接收的公钥尚未记录,则询问用户是否记录。若接受则写入 ~/.ssh/known_hosts 且继续登入的后续工作;若不接收则不写入该文件,并且离开登入工作; 
  29. 若接收到的公钥已有记录,则比对记录是否相同,若相同则继续登入动作;若不相同,则出现警告信息,且离开登入的动作。 
  30. [root@rhel6 ~]# rm -f .ssh/known_hosts  
  31. [root@rhel6 ~]# ssh rhel6 
  32. The authenticity of host 'rhel6 (192.168.1.119)' can't be established. 
  33. RSA key fingerprint is 1a:cf:92:de:28:7d:f2:e0:e8:e6:ad:f1:7c:40:6a:67. 
  34. Are you sure you want to continue connecting (yes/no)? yes                 //接受并在known_hosts中创建公钥 
  35. Warning: Permanently added 'rhel6,192.168.1.119' (RSA) to the list of known hosts. 
  36. root@rhel6's password:  
  37. Last login: Mon Dec 31 11:27:22 2012 from 192.168.1.19 
  38. [root@rhel6 ~]# cat .ssh/known_hosts  
  39. rhel6,192.168.1.119 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA08gfRmTgp6wM1GPgbVBsAiL6dOaKoViS9w/aL3P/NVGjYANfKQQxx2yagOxqOIFV5wefnrutdgoEmYm9sWl+9AtIf4XgMHupGWlq3jK4LWkKrN2Lg7HdijpbKzH2XuHcI1k9sRzB6F2Xhx3YdTnQKyT8wb9spKp9hzTL4ztGXrrcRW9lXBrz7jp9m4HOwim44j6SSVPTAVrCZWho2X+I27f/6DbCHNfFXV1mi+g7ERo2c8e4KwoKComXaa+E/PsBPKWOuvJgujl1VPQ2hTAWPSVXA67eR9o+39c/cOliDPq/SGsGXtWxZei9FM7G+OZAI5RdZ/Fqmbvivzfweg7IZQ== 
  40.  
  41. 每一次启动sshd服务时,sshd服务端都会主动去找/etc/ssh/ssh_host*的公私钥文件,如果不存在则会重新创建公私钥 
  42. [root@rhel6 ~]# ls /etc/ssh/ssh_host_*    
  43. /etc/ssh/ssh_host_dsa_key      /etc/ssh/ssh_host_key      /etc/ssh/ssh_host_rsa_key     //私钥 
  44. /etc/ssh/ssh_host_dsa_key.pub  /etc/ssh/ssh_host_key.pub  /etc/ssh/ssh_host_rsa_key.pub //公钥 
  45. [root@rhel6 ~]# rm -f /etc/ssh/ssh_host_* 
  46. [root@rhel6 ~]# ls /etc/ssh/ssh_host_*    
  47. ls: cannot access /etc/ssh/ssh_host_*: No such file or directory 
  48. [root@rhel6 ~]# /etc/init.d/sshd restart  
  49. Stopping sshd:                                             [  OK  ] 
  50. Generating SSH1 RSA host key:                              [  OK  ]    //创建SSH1的RSA公私钥 
  51. Generating SSH2 RSA host key:                              [  OK  ]    //创建SSH2的RSA公私钥 
  52. Generating SSH2 DSA host key:                              [  OK  ]    //创建SSH2的DSA公私钥 
  53. Starting sshd:                                             [  OK  ] 
  54. [root@rhel6 ~]# ls /etc/ssh/ssh_host_*   
  55. /etc/ssh/ssh_host_dsa_key      /etc/ssh/ssh_host_key      /etc/ssh/ssh_host_rsa_key 
  56. /etc/ssh/ssh_host_dsa_key.pub  /etc/ssh/ssh_host_key.pub  /etc/ssh/ssh_host_rsa_key.pub 
  57.  
  58.  
  59. [root@rhel6 ~]# ssh rhel6 
  60. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 
  61. @    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @ 
  62. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 
  63. IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! 
  64. Someone could be eavesdropping on you right now (man-in-the-middle attack)! 
  65. It is also possible that the RSA host key has just been changed. 
  66. The fingerprint for the RSA key sent by the remote host is 
  67. 16:1b:b4:09:20:fe:8f:48:12:e1:3c:16:5e:86:67:8b. 
  68. Please contact your system administrator. 
  69. Add correct host key in /root/.ssh/known_hosts to get rid of this message. 
  70. Offending key in /root/.ssh/known_hosts:1                                   //由于更新了公私钥,故提示known_hosts文件中第1行的信息不匹配 
  71. RSA host key for rhel6 has changed and you have requested strict checking. 
  72. Host key verification failed. 
  73.  
  74. [root@rhel6 ~]# sed -i '1d' .ssh/known_hosts                                //删除known_hosts的第一行内容 
  75. [root@rhel6 ~]# ssh rhel6                                                        
  76. The authenticity of host 'rhel6 (192.168.1.119)' can't be established. 
  77. RSA key fingerprint is 16:1b:b4:09:20:fe:8f:48:12:e1:3c:16:5e:86:67:8b. 
  78. Are you sure you want to continue connecting (yes/no)? yes                  //重新更新known_hosts中的公钥 
  79. Warning: Permanently added 'rhel6,192.168.1.119' (RSA) to the list of known hosts. 
  80. root@rhel6's password:  
  81. Last login: Mon Dec 31 13:28:30 2012 from rhel6 
  82.  
  83. ssh [-f] [-p port_num] [user@]IP  [CMD] 
  84. -f  :需要配合后面的[CMD],不登入远程主机直接发送一个指令,若不加-f参数则需等待后面的CMD指令执行完毕才会离开远程主机 
  85. -p  :指定sshd监听的端口 
  86. -X  :开启X11 Forwarding(X11 forwarding是基于SSH使用远程X-Windows应用,需配合xhost +) 
  87. -Y  :开启X11 Forwarding 
  88. [root@rhel6 ~]# vi ssh_test.sh                           //创建一个用于测试的脚本 
  89. #!/bin/sh 
  90. echo '####### ssh without "-f" ############' 
  91. date 
  92. ssh rhel6 sleep 10 
  93. date 
  94. echo '####### ssh with "-f" ############' 
  95. date 
  96. ssh -f rhel6 sleep 10 
  97. date 
  98. [root@rhel6 ~]#chmod +x ssh_test.sh  
  99. [root@rhel6 ~]# ./ssh_test.sh  
  100. ####### ssh without "-f" ############ 
  101. Mon Dec 31 14:24:26 CST 2012 
  102. Mon Dec 31 14:24:36 CST 2012                             //需等待远程主机的指令执行完毕才会离开 
  103. ####### ssh with "-f" ############ 
  104. Mon Dec 31 14:24:36 CST 2012 
  105. Mon Dec 31 14:24:36 CST 2012                             //远程主机执行指令后立即离开 
  106.  
  107. [root@rhel6 ~]# ssh rhel6 
  108. Last login: Mon Dec 31 15:13:16 2012 from rhel6 
  109. [root@rhel6 ~]# echo $DISPLAY 
  110.  
  111. [root@rhel6 ~]# exit 
  112.  
  113. [root@rhel6 ~]# ssh -X rhel6 
  114. Last login: Mon Dec 31 15:17:19 2012 from rhel6 
  115. [root@rhel6 ~]# echo $DISPLAY 
  116. localhost:10.0 

==================================================================================

ssh等价性

 
  1. [root@rhel5-1 .ssh]# ssh-keygen -t rsa 
  2. Generating public/private rsa key pair.                      "以下全部回车即可" 
  3. Enter file in which to save the key (/root/.ssh/id_rsa):  
  4. Enter passphrase (empty for no passphrase):  
  5. Enter same passphrase again:  
  6. Your identification has been saved in /root/.ssh/id_rsa. 
  7. Your public key has been saved in /root/.ssh/id_rsa.pub. 
  8. The key fingerprint is: 
  9. a1:ef:d7:94:03:da:bb:64:f2:7d:4f:73:ad:92:29:a1 root@rhel5-1.xfcy.org 
  10. [root@rhel5-1 .ssh]# ls 
  11. id_rsa  id_rsa.pub                              "id_rsa文件必须存在" 
  12. [root@rhel5-1 .ssh]# cat id_rsa.pub >> key 
  13. [root@rhel5-1 .ssh]# scp key rhel5-2:/root/.ssh/ 
  14. The authenticity of host 'rhel5-2 (192.168.1.22)' can't be established. 
  15. RSA key fingerprint is 26:5a:c3:e5:58:f0:0d:57:94:02:b0:7f:01:27:34:2a. 
  16. Are you sure you want to continue connecting (yes/no)? yes 
  17. Warning: Permanently added 'rhel5-2,192.168.1.22' (RSA) to the list of known hosts. 
  18. root@rhel5-2's password:  
  19. key                                        100%  403     0.4KB/s   00:00 
  20.  
  21.  
  22.  
  23. [root@rhel5-2 .ssh]# ls 
  24. key 
  25. [root@rhel5-2 .ssh]# ssh-keygen -t rsa 
  26. Generating public/private rsa key pair.                        
  27. Enter file in which to save the key (/root/.ssh/id_rsa):  
  28. Enter passphrase (empty for no passphrase):  
  29. Enter same passphrase again:  
  30. Your identification has been saved in /root/.ssh/id_rsa. 
  31. Your public key has been saved in /root/.ssh/id_rsa.pub. 
  32. The key fingerprint is: 
  33. 19:51:ec:c9:87:b3:7e:de:b0:e2:7d:b4:89:09:60:8f root@rhel5-2.xfcy.org 
  34. [root@rhel5-2 .ssh]# ls 
  35. id_rsa  id_rsa.pub  key 
  36. [root@rhel5-2 .ssh]# cat id_rsa.pub >> authorized_keys 
  37. [root@rhel5-2 .ssh]# cat key >> authorized_keys 
  38. [root@rhel5-2 .ssh]# scp authorized_keys rhel5-1:/root/.ssh/ 
  39. The authenticity of host 'rhel5-1 (192.168.1.11)' can't be established. 
  40. RSA key fingerprint is 26:5a:c3:e5:58:f0:0d:57:94:02:b0:7f:01:27:34:2a. 
  41. Are you sure you want to continue connecting (yes/no)? yes 
  42. Warning: Permanently added 'rhel5-1,192.168.1.11' (RSA) to the list of known hosts. 
  43. root@rhel5-1's password:  
  44. authorized_keys                               100%  806     0.8KB/s   00:00 
  45.  
  46. [root@rhel5-2 .ssh]# ls 
  47. authorized_keys  id_rsa  id_rsa.pub  key  known_hosts 
  48.  
  49. [root@rhel5-2 .ssh]# ssh rhel5-1 
  50. Last login: Thu Aug 30 15:41:33 2012 from rhel5-2.xfcy.org 
  51. 此时从rhel5-2通过ssh登录到rhel5-1已不需要密码,rhel5-1通过ssh登录到rhel5-2也不需要密码 
  52. 注:两端的id_rsa文件必须存在 

 

      本文转自Vnimos51CTO博客,原文链接:http://blog.51cto.com/vnimos/1105117,如需转载请自行联系原作者



相关文章
|
1月前
|
Linux 网络安全 Docker
盘古栈云,创建带ssh服务的linux容器
创建带ssh服务的linux容器
267 146
|
5月前
|
应用服务中间件 网络安全 数据安全/隐私保护
网关服务器配置指南:实现自动DHCP地址分配、HTTP服务和SSH无密码登录。
哇哈哈,道具都准备好了,咱们的魔术秀就要开始了。现在,你的网关服务器已经魔法满满,自动分配IP,提供网页服务,SSH登录如入无人之境。而整个世界,只会知道效果,不会知道是你在幕后操控一切。这就是真正的数字世界魔法师,随手拈来,手到擒来。
278 14
|
4月前
|
监控 Linux 网络安全
FinalShell SSH工具下载,服务器管理,远程桌面加速软件,支持Windows,macOS,Linux
FinalShell是一款国人开发的多平台SSH客户端工具,支持Windows、Mac OS X和Linux系统。它提供一体化服务器管理功能,支持shell和sftp同屏显示,命令自动提示,操作便捷。软件还具备加速功能,提升访问服务器速度,适合普通用户和专业人士使用。
825 0
|
6月前
|
Ubuntu Linux 网络安全
在Linux云服务器上限制特定IP进行SSH远程连接的设置
温馨提示,修改iptables规则时要格外小心,否则可能导致无法远程访问你的服务器。最好在掌握足够技术知识和理解清楚操作含义之后再进行。另外,在已经配置了防火墙的情况下,例如ufw(Ubuntu Firewall)或firewalld,需要按照相应的防火墙的规则来设置。
333 24
|
6月前
|
安全 网络协议 算法
FTP/TFTP/SSH/Telnet
本文详细介绍了FTP、TFTP、SSH和Telnet四种网络通信协议。FTP用于文件传输,功能强大但安全性低;TFTP是简化版文件传输协议,适合小文件快速传输,无用户认证;SSH是一种高安全性的远程登录协议,支持加密传输,广泛应用于服务器管理;Telnet虽简单易用,但因明文传输数据而安全性较低。文章还对比了各协议的功能、端口、加密情况及应用场景,帮助读者根据需求选择合适的协议。
468 21
|
5月前
|
安全 网络协议 Linux
Linux查 ssh端口号和服务状态
本指南详细介绍如何检查SSH服务的运行状态,包括通过进程命令验证服务启动、查看监听端口、检测系统服务状态以及防火墙配置。同时提供安全建议,如修改默认端口、禁用密码登录和定期更新系统,确保SSH服务稳定与安全。适用于不同Linux发行版(Systemd/SysVinit),帮助用户全面排查和优化SSH配置。
|
7月前
|
安全 Linux 网络安全
在Linux(CentOS和AWS)上安装更新的git2的方法并配置github-ssh
经过以上这些步骤,你现在就能在GitHub上顺利往返,如同海洋中的航海者自由驰骋。欢迎你加入码农的世界,享受这编程的乐趣吧!
281 10
|
Linux
linux xshell telnet 进去后如何退出
【8月更文挑战第27天】Telnet协议支持用户远程登录并操控另一台计算机。在Linux系统中结束Telnet会话可采用多种方式:直接输入"exit";利用快捷键Ctrl + ]后跟"quit";同样可通过Telnet命令结合快捷键"Ctrl + ]q"实现;此外,图形界面下直接关闭窗口也是一个简便的选择。用户可根据个人习惯及客户端类型选取合适的方法退出会话。
1377 4
|
8月前
|
安全 网络安全 数据安全/隐私保护
Debian 12系统中允许Root远程SSH登录解决方法!
在 Debian 12 系统中开启 SSH 远程 Root 登录需修改 SSH 配置文件 (`sshd_config`),将 `PermitRootLogin` 设置为 `yes` 并确保密码认证启用。完成后重启 SSH 服务并验证连接。若防火墙启用,需放行端口 22。注意,直接开放 Root 登录可能带来安全风险,建议使用普通用户登录后切换至 Root。
808 1
|
JavaScript 应用服务中间件 Linux
【应用服务 App Service】解决无法从Azure门户SSH登录问题
【应用服务 App Service】解决无法从Azure门户SSH登录问题
216 0