1. #!/bin/bash
    2. function installopenssh() {
    3. #### 请将此目录放到/opt目录下执行####
    4. #### 配置YUM源####
    5. clear
    6. echo -e "\t\t########################"
    7. echo -e "\t\t# 配置YUM仓库 #"
    8. echo -e "\t\t########################"
    9. sleep 1
    10. echo -e "启动脚本....................................\033[32m [成功] \033[0m"
    11. sleep 1
    12. echo -e "备份YUM源中.................................\033[33m [稍等] \033[0m"
    13. sleep 3
    14. mkdir /etc/yum.repos.d/yum.bak &>/dev/null
    15. mv /etc/yum.repos.d/* /etc/yum.repos.d/yum.bak/ &>/dev/null
    16. id &>/dev/null
    17. if [ $? -eq 0 ];then
    18. echo -e "备份YUM源...................................\033[32m [成功] \033[0m"
    19. else
    20. echo -e "备份YUM源...................................\033[31m [失败] \033[0m"
    21. fi
    22. sleep 1
    23. echo -e "配置YUM源中.................................\033[33m [稍等] \033[0m"
    24. sleep 1
    25. cat > /etc/yum.repos.d/local.repo <<EOF
    26. [local]
    27. name=localyum
    28. baseurl=ftp://x.x.x.x/pub/centos7.5
    29. enabled=1
    30. gpgcheck=0
    31. EOF
    32. yum clean all &>/dev/null
    33. if [ $? -eq 0 ];then
    34. echo -e "配置YUM源...................................\033[32m [成功] \033[0m"
    35. else
    36. echo -e "配置YUM源...................................\033[31m [失败] \033[0m"
    37. fi
    38. #### 配置install openssh8.0 ####
    39. cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak &>/dev/null
    40. echo -e "\t\t########################"
    41. echo -e "\t\t# ssh文件备份完成 #"
    42. echo -e "\t\t########################"
    43. clear
    44. echo -e "\t\t########################"
    45. echo -e "\t\t# 配置安装openssh #"
    46. echo -e "\t\t########################"
    47. echo -e "安装依赖包....................................\033[33m [稍等] \033[0m"
    48. RPMS='zlib-devel openssl-devel gcc perl-devel pam-devel unzip'
    49. yum install -y $RPMS &>/dev/null
    50. echo "openssh8.0默认路径为:/opt/openssh.install/RPM/openssh8.0"
    51. echo "openssh8.6默认路径为:/opt/openssh.install/RPM/openssh8.6"
    52. read -p "请输入存放RPM包绝对路径目录位置:" path
    53. if [ -d $path ];then
    54. echo -e "判断文件是否存在.......................... \033[032m [存在] \033[0m"
    55. cd $path
    56. yum -y install * &>/dev/null
    57. if [ $? -eq 0 ];then
    58. echo -e "安装成功...................................\033[032m [成功] \033[0m"
    59. else
    60. echo -e "安装失败...................................\033[031m [失败] \033[0m"
    61. fi
    62. else
    63. echo -e "判断文件是否存在......................... \033[031m [不存在] \033[0m"
    64. fi
    65. sleep 2
    66. #### 其他 配置####
    67. clear
    68. echo -e "\t\t########################"
    69. echo -e "\t\t# 其他 配置 #"
    70. echo -e "\t\t########################"
    71. cd /etc/ssh/
    72. chmod 400 ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key
    73. if [ $? -eq 0 ];then
    74. echo -e "调整权限....................................\033[032m [成功] \033[0m"
    75. else
    76. echo -e "调整权限....................................\033[031m [失败] \033[0m"
    77. fi
    78. sleep 2
    79. clear
    80. echo -e "\t\t########################"
    81. echo -e "\t\t# 最后的配置 #"
    82. echo -e "\t\t########################"
    83. cat >> /etc/ssh/sshd_config <<EOF
    84. PubkeyAuthentication yes
    85. PasswordAuthentication yes
    86. PermitRootLogin yes
    87. EOF
    88. sshd -t &>/dev/null
    89. if [ $? -eq 0 ];then
    90. read -p "完成所有配置是否重启sshd服务[Y/N]:" yn
    91. if [ $yn == y ];then
    92. systemctl restart sshd
    93. sleep 2
    94. if [ $? -eq 0 ];then
    95. echo "[重启成功]"
    96. else
    97. echo "[重启失败]"
    98. fi
    99. elif [ $yn == n ];then
    100. echo "[完成]没有重启sshd服务"
    101. else
    102. echo "[输入无效]"
    103. fi
    104. fi
    105. sleep 2
    106. clear
    107. echo "SElinux开启状态会影响到root用户禁止远程登入状态"
    108. read -p "是否关闭SElinux?[Y/N]:" yn2
    109. if [ $yn2 == y ];then
    110. setenforce 0
    111. sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config
    112. echo "[已关闭]"
    113. elif [ $yn2 == n ];then
    114. echo "[不进行操作]"
    115. else
    116. echo "[输入无效]"
    117. fi
    118. #### raw格式优化 ####
    119. cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak &>/dev/null
    120. rm -rf /etc/ssh/sshd_config &>/dev/null
    121. cp /opt/openssh.install/sshd_config /etc/ssh/ &>/dev/null
    122. systemctl restart sshd &>/dev/null
    123. }
    124. #### 安装telnet ####
    125. function telnet_install() {
    126. yum -y localinstall /opt/openssh.install/telent/* &>/dev/null
    127. cp telent/telnet /etc/xinetd.d/
    128. mv /etc/securetty /etc/securetty.bak &>/dev/null
    129. systemctl restart xinetd
    130. #### firewalld ####
    131. firewall-cmd --add-service=telnet --permanent &>/dev/null
    132. firewall-cmd --reload &>/dev/null
    133. }
    134. #### 卸载telnet ####
    135. function telnet_remove() {
    136. rpm -e telnet-0.17-66.el7.x86_64
    137. rpm -e telnet-server-0.17-66.el7.x86_64
    138. firewall-cmd --remove-service=telnet --permanent
    139. firewall-cmd --reload
    140. mv /etc/securetty.bak /etc/securetty
    141. mv /etc/xinetd.d/telnet /etc/xinetd.d/telnet.bak
    142. }
    143. ## =====函数引用===== ##
    144. installopenssh
    145. #telnet_install
    146. #telnet_remove
    147. 1qaz!QAZ