Java321技术网

 找回密码
 立即注册
搜索
热搜: centos
查看: 6122|回复: 0
打印 上一主题 下一主题

128MB VPS 上优化 CentOS 5

[复制链接]

126

主题

126

帖子

815

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
815
跳转到指定楼层
楼主
发表于 2017-12-19 02:12:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

CentOS 是一个构建在 Red Hat Enterprise Linux (RHEL) 源代码上的 Linux 发行版,并且从二进制的角度100%兼容 RHEL 软件包,简单的说 RHEL上 可以运行的软件包在 CentOS 上不需要编译就可以直接安装运行。除了少量的版权信息外,CentOS 和 RHEL 基本上一样。CentOS 是免费的,并且有着 RHEL 的稳定,因此深受各大 hosting 服务商支持,几乎所有 Linux VPS 都支持 CentOS。

一般来说如果 VPS 配置较高我会选 CentOS,配置低的话就选 Debian,当然这是个人偏好,大多数 Linux VPS 服务商也会提供 Gentoo,不过每次安装程序,升级都要编译会消耗很多资源,耗时,而且性能没有明显提高,不推荐给配置低的 VPS。

VPS 服务商一般给的操作系统版本都是最小安装版本,或者优化过的版本。每个 VPS 服务商提供的版本都可能不同,安装 CentOS 的系统最低要求至少 64MB 内存(纯文字界面),1GB 硬盘空间。

安装和升级系统

1、登录 VPS 安装 CentOS 5。

2、安装完毕后马上升级整个系统。

  1. yum update
复制代码

有了一个干净的系统以后,剩下来就是加强和优化 Linux。

删除不必要的软件包,服务,用户,文件等

3、删除不需要的软件包。

  1. yum remove Deployment_Guide-en-US finger cups-libs cups
  2. bluez-libs desktop-file-utils ppp rp-pppoe wireless-tools irda-utils
  3. nfs-utils nfs-utils-lib rdate fetchmail eject ksh mkbootdisk mtools
  4. syslinux tcsh startup-notification talk apmd rmt dump setserial portmap yp-tools
  5. ypbind
  6. rpm -qa (列出所有安装了的包)
  7. rpm -e package (删除某个包)
  8. rpm -qi package (查询某个包)
  9. rpm -qf command (根据程序查询包的名字)
  10. rpm -ql package (查询某个包所有的安装文件)</div>
复制代码

4、删除一些不安全的软件包,并且用相应安全的软件替代,如: ssh/sftp/scp 替代 telnet, rsh, ftp, rcp
注意系统需要一个默认的 MTA,删除 Sendmail MTA 之前必须先安装一个,如: Postfix。


  1. yum remove telnet rsh ftp rcp
  2. yum install postfix
  3. yum remove sendmail
  4. /sbin/chkconfig postfix off
复制代码

5、停掉并且删除一些不需要的 xinetd 服务。

  1. /sbin/service xinetd stop; /sbin/chkconfig xinetd off
  2. rm -rf /etc/xinetd.d
复制代码

6、禁止一些 /etc/init.d/ 下面不需要的服务,更多信息请参考 Understanding your (Red Hat Enterprise Linux) daemons, by Len DiMaggioHardening Tips For Default Installation of Red Hat Enterprise Linux 5.

  1. /sbin/chkconfig --list

  2. for a in acpid anacron apmd atd autofs avahi-daemon bluetooth cpuspeed \
  3. cups firstboot gpm haldaemon hidd ip6tables irqbalance isdn kdump \
  4. kudzumcstrans messagebus microcode_ctl netfs nfs nfslock pcscd portmap \
  5. readahead_early readahead_later rpcgssd rpcidmapd sendmail \
  6. setroublesshoot smartd xfs xinetd yum-updatesd; \
  7. do /sbin/chkconfig $a off; done
复制代码

7、重启系统后,检查一下正在运行中的服务,看看是不是都是必须的。

  1. netstat -an | grep LISTEN
  2. netstat -atunp
复制代码

8、为了安全起见,删除一些不需要的用户。


  1. cp /etc/passwd /etc/passwd.sav
  2. cp /etc/group /etc/group.sav
  3. for a in adm lp sync news uucp operator games gopher mailnull nscd rpc;
  4. do /usr/sbin/userdel $a -f; done
  5. for a in lp news uucp games gopher users floopy nscd rpc rpcuser nfsnobody;
  6. do /usr/sbin/groupdel $a -f; done
复制代码

加固和优化系统

9、打开防火墙。

  1. system-config-securitylevel-tui
复制代码

10、检查和禁止全局可写的 SUID 文件。

  1. find / -perm +4000 -user root -type f -print
  2. find / -perm +2000 -group root -type f -print
  3. chmod u-s /full/path/to/filename
  4. chmod g-s /full/path/to/filename
复制代码

11、只允许 root 在一个 terminal 上登录,如: tty1。

  1. vi /etc/securetty
复制代码

12、避免其他用户按 Ctrl+Alt+Del 重启。

  1. vi /etc/inittab
复制代码

注释掉
#ca::ctrlaltdel:/sbin/shutdown -t3 -r now

13、/etc/security/console.apps/ 下面有 root 用户登录 console 后可以运行的程序,全部删除。
rm -f /etc/security/console.apps/*

14、删除一些登录信息。


vi /etc/issue (warning at login prompt)
vi /etc/motd (warning after successful login)

15、只运行一个 virtual terminal,如果是 VPS 的话,自己不可能物理登录终端,可以全部禁止掉。

  1. vi /etc/inittab
  2. # Run gettys in standard runlevels
  3. #1:2345:respawn:/sbin/mingetty tty1
  4. #2:2345:respawn:/sbin/mingetty tty2
  5. ...
复制代码

16、加固 SSH 安全。


  1. vi /etc/ssh/sshd_config
  2. Port 2222
  3. Protocol 2
  4. PermitRootLogin no
  5. PermitEmptyPasswords no
  6. X11Forwarding no
  7. UsePAM no
  8. UseDNS no
  9. AllowUsers vpsee
  10. Banner /etc/issue
复制代码

17、安装 Bastille 软件包帮助加固。

  1. rpm -Uvh perl-Curses-1.15-1.el5.rf.i386.rpm
  2. rpm -ivh Bastille-3.0.9-1.0.noarch.rpm
  3. /usr/sbin/bastille -c
复制代码

18、优化 Linux 内核。

vi /etc/sysctl.confnet.ipv4.conf.all.send_redirects = 0net.ipv4.conf.all.accept_redirects = 0
定制 Linux 内核
  1. vi /etc/sysctl.conf
  2. net.ipv4.conf.all.send_redirects = 0
  3. net.ipv4.conf.all.accept_redirects = 0
复制代码

19、定制,编译,安装 Linux 内核。

  1. yum install rpm-build ncurses ncurses-devel
  2. rpm -ivh kernel-2.6.18-8.1.1.el5.src.rpm
  3. cd /usr/src/redhat/SPECS
  4. rpmbuild -bp --target i686 kernel-2.6.spec
  5. cd /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i686
  6. sed -i 's/EXTRAVERSION = -prep/EXTRAVERSION = -8.1.1.custom.el5/' Makefile
  7. make menuconfig
  8. make rpm
  9. cd /usr/src/redhat/RPMS/i686
  10. rpm -ivh kernel-2.6.18prep-1.rpm
  11. /sbin/mkinitrd /boot/initrd-2.6.18-prep.img 2.6.18-prep (2.6.18-prep -> /lib/modules)
  12. vi /boot/grub/menu.1st
复制代码

20、修改 iptables,只允许 ssh,http 和 https 端口打开。

  1. /sbin/iptables -F
  2. /sbin/iptables -A INPUT -i lo -j ACCEPT
  3. /sbin/iptables -A INPUT -i ! lo -d 127.0.0.0/8 -j REJECT
  4. /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  5. /sbin/iptables -A OUTPUT -j ACCEPT
  6. /sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
  7. /sbin/iptables -A INPUT -p tcp --dport 443 -j ACCEPT
  8. /sbin/iptables -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
  9. /sbin/iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
  10. /sbin/iptables -A INPUT -j REJECT
  11. /sbin/iptables -A FORWARD -j REJECT
复制代码

然后查看一下 iptables:


  1. iptables -L
复制代码

回复

使用道具 举报

QQ|Archiver|手机版|小黑屋|Java321技术网   蜀ICP备15030946号-1

GMT+8, 2024-4-29 16:24 , Processed in 0.055604 second(s), 21 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表