Linux 远程登录(telnet ssh)

简介:
  •  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,如需转载请自行联系原作者



相关文章
|
10天前
|
网络协议 安全 网络安全
【技术分享】Telnet / SSH
4月更文挑战第7天
13 0
|
27天前
|
安全 网络协议 Shell
【Shell 命令集合 系统管理 】Linux 远程登录 rlogin命令 使用指南
【Shell 命令集合 系统管理 】Linux 远程登录 rlogin命令 使用指南
30 0
|
27天前
|
网络协议 Shell Linux
【Shell 命令集合 网络通讯 】⭐Linux 远程登录工具 telnet 命令 使用指南
【Shell 命令集合 网络通讯 】⭐Linux 远程登录工具 telnet 命令 使用指南
28 0
|
1月前
|
Linux Shell 网络安全
Linux远程登录及相关工具介绍
Linux远程登录及相关工具介绍。
16 2
|
1月前
|
网络协议 Ubuntu Linux
「远程开发」VSCode使用SSH远程linux服务器 - 公网远程连接
「远程开发」VSCode使用SSH远程linux服务器 - 公网远程连接
111 0
|
1月前
|
安全 Linux Shell
|
2月前
|
安全 网络协议 Linux
|
2月前
|
关系型数据库 网络安全 数据库
通过SSH登录OceanBase数据库需要修改用户密码,然后使用SSH客户端进行远程登录
通过SSH登录OceanBase数据库需要修改用户密码,然后使用SSH客户端进行远程登录
69 6
|
2月前
|
存储 Linux 网络安全
如何在 Linux 中删除 SSL 证书和 SSH 密码?
如何在 Linux 中删除 SSL 证书和 SSH 密码?
79 1
如何在 Linux 中删除 SSL 证书和 SSH 密码?
|
2月前
|
网络安全 数据安全/隐私保护
如何使用ssh key免密码登录服务器?
如何使用ssh key免密码登录服务器?