Linux常用配置

 

近期虚拟机由Vmware换到了Hyper-V,虚拟机系统(CentOS7)重新装了,把一些配置记录一下。

网络配置

相关的主要的几个配置文件为:

配置主机名(域名)和IP地址的对应
/ect/hosts

配置主机名
/etc/hostname

配置主机名和网关
/etc/sysconfig/network

网卡配置文件,网卡名为eth0则配置文件名为ifcfg-eth0,以此类推 /etc/sysconfig/network-scripts/ifcfg-eth0

配置DNS客户端(关于使用哪个DNS服务器的配置)
/etc/resolv.conf

# 查看网络
ip a

# 启动指定网卡
ifup eth0

# 编辑网卡配置文件,文件名是和网卡名对上的
vi /etc/sysconfig/network-scripts/ifcfg-eth0

网卡配置(/etc/sysconfig/network-scripts/ifcfg-xxxx)

# 网络类型,以太网
TYPE=Ethernet

# 网卡对应的设备别名
DEVICE=eth0

#自启动
ONBOOT=yes

# 获得ip地址的方式,static或dhcp
BOOTPROTO=static

# IP地址
IPADDR=12.168.1.80

# 子网掩码,两种方式
NETMASK=255.255.255.0
PREFIX=24

# 网关
GATEWAY=192.168.1.0

# DNS地址,多个用编号区别(优先于/etc/resolv.conf)
DNS1=192.168.1.1

# 普通用户是否能控制网卡
USERCTL=no

DNS配置(/etc/resolv.conf)

nameserver 8.8.8.8
nameserver 119.29.29.29

网络配置(/etc/sysconfig/network)

# 是否使用网络,设为no,则不能使用网络。
NETWORKING=yes

# 本机的主机名,和/etc/hosts中设置的主机名对应
# 推荐在/etc/hostname设置
HOSTNAME=CentOS7

# 网关地址
GATEWAY=192.168.1.1

防火墙配置

# 停止firewall
systemctl stop firewalld
# 检查防火墙状态
systemctl status firewalld
# 禁止firewall开机启动
systemctl disable firewalld.service

# 安装iptables,CentOS7中默认防火墙是firewall,使用iptables需要安装
yum install iptables-services -y
# 启动iptables
systemctl start iptables
# 设置iptables开机启动
systemctl enable iptables
# 配置文件
vi /etc/sysconfig/iptables
# 保存配置
service iptables save

清空iptables规则

# 清除所有制订的规则
iptables -F

# 清除所有用户“自定义”的chain
iptables -X

# 将所有chain的计数与流量统计都归零
iptables -Z

# 查看规则
iptables -nvL

iptables的使用(工作于OSI三层和四层以及二层源MAC地址过滤)

yum源配置

阿里 https://opsx.alibaba.com/

网易 http://mirrors.163.com/

这里用阿里的

# 进入仓库文件夹
cd /etc/yum.repos.d/

# 下载仓库配置文件
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 清除yum缓存
yum clean all

# 生成yum缓存
yum makecache

有时候也需要安装epel源

yum install -y epel-release
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

关闭SELinux

# 临时关闭
setenforce 0

# 永久关闭(重启生效)
# SELINUX=enforcing改为SELINUX=disabled
vi /etc/selinux/config

# 查看状态
getenforce

Centos7桌面环境开机自动登陆root

# 修改配置文件
vi /etc/gdm/custom.conf

增加如下配置

[daemon]
AutomaticLoginEnable=True
AutomaticLogin=root

开机模式切换

# 获取当前模式
systemctl get-default

# 修改为图形化
systemctl set-default graphical.target

# 修改为命令行
systemctl set-default multi-user.target

修改主机名

# 查看
uname -a

# 命令方式
hostnamectl set-hostname  NMServer-7

# 配置文件方式
vi /etc/hostname

修改命令行模式屏幕分辨率

grubby --update-kernel=ALL --args="video=hyperv_fb:800x600"

NTP服务

# 安装
yum -y install ntp

# 开机启动
systemctl enable ntpd

# 开启服务
systemctl start ntpd

# 同步
ntpdate master

# 修改配置文件
vi /etc/ntp.conf

禁用Transparent Huge Pages

# 开机启动方式
vi /etc/rc.local

在最后添加

if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
 echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
 echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
# 修改配置方式
vi /etc/grub.conf
# transparent_hugepage=never

# 查看
cat /sys/kernel/mm/transparent_hugepage/enabled

# [always] 启用
# [never] 禁用
# [madvise] 只在MADV_HUGEPAGE标志的VMA中使用THP

其他命令

# 查看启动项
systemctl list-unit-files

# Centos7安装Gcc
yum install gcc
yum install gcc-c++