Centos7 初始化配置

633人浏览 / 0人评论

Centos7 初始化配置

[toc] 标签: Centos

安装EPEL源

yum install epel-release
修改epel源
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch/debug
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/SRPMS
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

升级系统

yum update -y
update和upgrade的区别: 两者的作用都是将软件包升级到最新版。可是upgrade会考虑取代关系,如果有个包foo改名为foo2,那么upgrade会将foo升级为foo2并将foo删除!如果你的系统已经存在运行中的服务,且服务正常运行着,没重大的安全隐患不要使用upgrade升级系统。

升级系统而不升级内核

yum update --exclude=kernel* -y

安装常用包

Centos7 最小化安装很多命令都没有,比如ifconfig , netstat, 这时需要安装一些工具包,包括源码编译安装软件时的依赖
yum install -y pcre-devel openssl-devel libxslt* perl-ExtUtils-Embed at gcc-c++ python subversion gperf make rpm-build git curl bzip2-devel libcurl-devel gd gd-devel t1lib t1lib-devel libmcrypt libmcrypt-devel libtidy libtidy-devel GeoIP-devel libatomic_ops-devel zlib-devel unzip libstdc++* net-snmp net-snmp* gmp gmp-devel openldap openldap-devel libpcap-devel glib2-devel GeoIP-devel libxml2-devel libxml2-devel redis vim wget git htop iftop libtool make automake mlocate openssl-devel pam-devel unzip gcc screen openssl openssl-devel iptables-services bash-completion* pcre-devel openssl-devel libxslt* perl-ExtUtils-Embed at gcc-c++ python subversion gperf make rpm-build git curl bzip2-devel libcurl-devel gd gd-devel t1lib t1lib-devel libmcrypt libmcrypt-devel libtidy libtidy-devel GeoIP-devel libatomic_ops-devel zlib-devel unzip libstdc++* net-snmp net-snmp* gmp gmp-devel openldap openldap-devel net-tools

关闭一些程序

selinux

具体也不知道干嘛的,据说是美国的什么安全的什么鬼东西,具体去百度。启用了这个有些服务莫名其妙不能启动,索性关掉。
#打开selinux的配置文件
vim /etc/selinux/config

#将第7行的enforcing改为disabled
SELINUX=disabled

#保存退出
#这时需要重启才会生效,可以使用命令临时关闭
setenforce 0

firewalld

默认防火墙,CentOS7CentOS6的区别之一使用firewalld替代了之前一直使用iptables,个人觉得iptables用起来更加顺手。
#禁止开机启动
systemctl disable firewalld

#立即停止服务
systemctl stop firewalld

使用 iptables

启用iptables

#设置iptables为开机启动
systemctl enable iptables

#立即启动iptables
systemctl start iptables

#禁止IPv6的ip6tables开机启动
systemctl disable ip6tables

#立即停止IPv6的ip6tables
systemctl stop ip6tables

设置iptables

  • 查看防火墙规则
#通过以下命令查看iptables现有的规则,包含INPUT,FORWARD,OUTPUT
iptables -L -vn --line-number

#通过以下命令查看iptables现有nat的规则,包含PREROUTING,INPUT,OUTPUT,POSTROUTING
iptables -L -vn -t nat --line-number
  • 修改iptables
iptables配置文件路径/etc/sysconfig/iptables
iptables 文件修改完成后需要重启服务器

配置SSH

无论你的服务器是用在外网还是内网,默认的SSH端口都必须更改。安全是最重要的。同时使用证书登陆,那就更好了。
#编辑sshd_config文件
vim /etc/ssh/sshd_config

#修改端口,在#Port 22这一行下添加,端口号请自行选择。
Port 9099

#修改PasswordAuthentication字段为no
PasswordAuthentication no

#保存退出
生成ssh key
#通过以下命令生成key(密钥对)
ssh-keygen -t rsa -b 4096

#这里回车即可
Enter file in which to save the key (/root/.ssh/id_rsa):

#这里请为你的key设置一个密码,用key登录的时候需要密码才能解锁key。这是保护key的一种有效的方法。
#密码留空并回车即为空密码,不安全!
Enter passphrase (empty for no passphrase):

#重命名密钥,有些主机以内置密钥,覆盖前请确认。
mv .ssh/id_rsa.pub .ssh/authorized_keys

#查看key并保存到本地,然后删除。
cat .ssh/id_rsa

#留密钥在服务器内就足够了,key删掉以保安全。前提是你已经保存到本地!要不然你将不能通过SSH登陆服务器!
rm -f .ssh/id_rsa
重启SSH服务
systemctl restart sshd

修改hostname

如果你有多台服务器,那hostname将是区分这些服务器最好的方法。你可以通过以下命令进行修改
hostnamectl set-hostname [your-hostname]

#例如
hostnamectl set-hostname test1

#重启生效