1. echo "修改IP地址"
    2. # 修改IP地址
    3. sudo sed -i -e"s/BOOTPROTO=dhcp/BOOTPROTO=static/" /etc/sysconfig/network-scripts/ifcfg-ens192
    4. sudo sed -i -e"s/ONBOOT=no/ONBOOT=yes/" /etc/sysconfig/network-scripts/ifcfg-ens192
    5. sudo sed -i -e"16iIPADDR=192.168.72.122\nGATEWAY=192.168.72.254\nDNS1=114.114.114.114\nDNS2=8.8.8.8" /etc/sysconfig/network-scripts/ifcfg-ens192
    6. sudo service network restart
    7. echo "设置免密操作"
    8. #设置免密操作
    9. # 授权可编辑/etc/sudoers 文件
    10. sudo chmod -v u+w /etc/sudoers
    11. # 在101行添加xcsoft ALL=(ALL) NOPASSWD:ALL
    12. sudo sed -i -e"101ixcsoft ALL=(ALL) NOPASSWD:ALL" /etc/sudoers
    13. # 替换
    14. sudo sed -i -e"s/%wheel\s*ALL=(ALL)\s*ALL/%wheel ALL=(ALL) NOPASSWD:ALL/" /etc/sudoers
    15. # 取消sudoers文件写权限
    16. sudo chmod -v u-w /etc/sudoers
    17. echo "禁root登录"
    18. # 禁root登录
    19. sudo sed -i -e"39iPermitRootLogin no" /etc/ssh/sshd_config
    20. echo "修改ssh端口号"
    21. # 修改ssh端口号
    22. sudo sed -i -e"17iPort 922" /etc/ssh/sshd_config
    23. sudo service sshd restart
    24. echo "配置系统yum源"
    25. # 备份系统旧配置文件
    26. sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    27. # 编辑源
    28. cat >> /etc/yum.repos.d/CentOS-Base.repo << "EOF"
    29. # CentOS-Base.repo
    30. #
    31. # The mirror system uses the connecting IP address of the client and the
    32. # update status of each mirror to pick mirrors that are updated to and
    33. # geographically close to the client. You should use this for CentOS updates
    34. # unless you are manually picking other mirrors.
    35. #
    36. # If the mirrorlist= does not work for you, as a fall back you can try the
    37. # remarked out baseurl= line instead.
    38. #
    39. #
    40. [os]
    41. name=Qcloud centos os - $basearch
    42. baseurl=http://mirrors.cloud.tencent.com/centos/$releasever/os/$basearch/
    43. enabled=1
    44. gpgcheck=1
    45. gpgkey=http://mirrors.cloud.tencent.com/centos/RPM-GPG-KEY-CentOS-7
    46. [updates]
    47. name=Qcloud centos updates - $basearch
    48. baseurl=http://mirrors.cloud.tencent.com/centos/$releasever/updates/$basearch/
    49. enabled=1
    50. gpgcheck=1
    51. gpgkey=http://mirrors.cloud.tencent.com/centos/RPM-GPG-KEY-CentOS-7
    52. [centosplus]
    53. name=Qcloud centosplus - $basearch
    54. baseurl=http://mirrors.cloud.tencent.com/centos/$releasever/centosplus/$basearch/
    55. enabled=0
    56. gpgcheck=1
    57. gpgkey=http://mirrors.cloud.tencent.com/centos/RPM-GPG-KEY-CentOS-7
    58. [cloud]
    59. name=Qcloud centos contrib - $basearch
    60. baseurl=http://mirrors.cloud.tencent.com/centos/$releasever/cloud/$basearch/openstack-kilo/
    61. enabled=0
    62. gpgcheck=1
    63. gpgkey=http://mirrors.cloud.tencent.com/centos/RPM-GPG-KEY-CentOS-7
    64. [cr]
    65. name=Qcloud centos cr - $basearch
    66. baseurl=http://mirrors.cloud.tencent.com/centos/$releasever/cr/$basearch/
    67. enabled=0
    68. gpgcheck=1
    69. gpgkey=http://mirrors.cloud.tencent.com/centos/RPM-GPG-KEY-CentOS-7
    70. [extras]
    71. name=Qcloud centos extras - $basearch
    72. baseurl=http://mirrors.cloud.tencent.com/centos/$releasever/extras/$basearch/
    73. enabled=1
    74. gpgcheck=1
    75. gpgkey=http://mirrors.cloud.tencent.com/centos/RPM-GPG-KEY-CentOS-7
    76. [fasttrack]
    77. name=Qcloud centos fasttrack - $basearch
    78. baseurl=http://mirrors.cloud.tencent.com/centos/$releasever/fasttrack/$basearch/
    79. enabled=0
    80. gpgcheck=1
    81. gpgkey=http://mirrors.cloud.tencent.com/centos/RPM-GPG-KEY-CentOS-7
    82. EOF
    83. sudo yum clean all
    84. sudo yum makecache
    85. sudo yum install -y vim wget
    86. # 安装 epel源
    87. sudo mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
    88. sudo wget -O /etc/yum.repos.d/epel.repo http://mirrors.cloud.tencent.com/repo/epel-7.repo
    89. sudo yum clean all
    90. sudo yum makecache
    91. sudo yum update
    92. echo "安装Fail2ban防爆破"
    93. # 安装 Fail2ban
    94. sudo yum install -y fail2ban
    95. # 进入fail2ban目录
    96. cd /etc/fail2ban
    97. # 复制一份配置文件
    98. sudo cp fail2ban.conf fail2ban.local
    99. sudo cp jail.conf jail.local
    100. vim
    101. sudo cat >> jail.d/sshd.local << "EOF"
    102. [sshd]
    103. enabled=true
    104. bandtime=1y
    105. findtime=1m
    106. maxretry=5
    107. EOF
    108. sudo service fail2ban restart
    109. sudo systemctl enable fail2ban
    110. echo "修改hostname"
    111. # 修改hostname
    112. sudo sed -i -e"s/\w*/demo/" /etc/hostname