大数据

linux 初始化通用配置

2018-01-09  本文已影响0人  陈sir的知识图谱

linux 初始配置

关闭Selinux

编辑 /etc/selinux/config
SELINUX=disabled
需要重启

查看selinux 状态

sestatus
SELinux status: disabled


关闭Firewalld

做实验的时候,端口无法访问的事情是经常发生的。不折腾了。

systemctl status firewalld
systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld
firewall-cmd --state

安装 Epel

yum install epel-release

查看repo情况

yum repolist

配置 Hostname

cat /etc/hostname
kolla

cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.10.110 kolla

检查

#hostname -F /etc/hostname
#hostname
kolla

同步时间

yum install ntp
systemctl enable ntpd.service
systemctl start ntpd.service

手工同步时间

ntpdate 0.centos.pool.ntp.org

配置 pip 镜像

mkdir ~/.pip
vi ~/.pip/pip.conf

写入如下内容

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

内核调优

调大打开文件数

修改/etc/security/limits.conf

*               soft    nofile          65555
*               hard    nofile          65555
*               hard    core            65555
*               soft    core            65555
*               hard    noproc          65555
*               soft    noproc          65555

修改netfilter 配置

#net.netfilter.nf_conntrack_buckets 的值
# 临时生效
echo 262144 > /sys/module/nf_conntrack/parameters/hashsize
# 重启永久生效,新建文件:/etc/modprobe.d/iptables.conf,设置如下
options nf_conntrack hashsize = 32768 

# 修改net.netfilter.nf_conntrack_max 最大追踪连接数修改 调优
# 临时生效
sudo sysctl -w net.netfilter.nf_conntrack_max=1048576
suod sysctl -w net.nf_conntrack_max=1048576

# 永久生效
# 添加修改内核配置文件(/etc/sysctl.conf) 
net.netfilter.nf_conntrack_max=1048576
net.nf_conntrack_max=1048576
# 当数据包超长时,不丢弃数据包。K8S重要
net.netfilter.nf_conntrack_tcp_be_liberal=1

# 如果要马上应用配置文件里的设置:
sudo sysctl -p /etc/sysctl.conf

# 临时生效

# 主动方的最后1个状态。默认120秒
sudo sysctl -w net.netfilter.nf_conntrack_tcp_timeout_fin_wait=30
sudo sysctl -w net.netfilter.nf_conntrack_tcp_timeout_time_wait=30

# CLOSE_WAIT是被动方收到FIN发ACK,然后会转到LAST_ACK发FIN,除非程序写得有问题,正常来说这状态持续时间很短。#默认 60 秒
sudo sysctl -w net.netfilter.nf_conntrack_tcp_timeout_close_wait=15

# 理论上不用这么长,不小于 net.ipv4.tcp_keepalive_time 就行了。默认 432000 秒(5天)
sudo sysctl -w net.netfilter.nf_conntrack_tcp_timeout_established=300

-----------------------------------------------------

# 永久生效
# 修改内核配置文件(/etc/sysctl.conf) 
net.netfilter.nf_conntrack_tcp_timeout_fin_wait=30
net.netfilter.nf_conntrack_tcp_timeout_time_wait=30
net.netfilter.nf_conntrack_tcp_timeout_close_wait=15
net.netfilter.nf_conntrack_tcp_timeout_established=300

# 如果要马上应用配置文件里的设置:
sudo sysctl -p /etc/sysctl.conf


## 安装 lrzsz
``` shell
yum  install -y lrzsz

安装zsh

yum install -y zsh git
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# chsh -s /usr/bin/zsh 更换使用的shell 程序为zsh

推荐主题

直接设置使用即可,无需修改原主题

ZSH_THEME="agnoster"

定制化主题

安装之后会发现看不到当前用户的用户名。解决方案如下

# 查看当前zsh用的哪一个主题
echo $ZSH_THEME  # 输出robbyrussell
cp ~/.oh-my-zsh/themes/robbyrussell.zsh-theme .oh-my-zsh/themes/myrobbyrussell.zsh-theme

修改 myrobbyrussell.zsh-theme 的内容为如下内容

#PROMPT="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
#PROMPT+=' %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'
PROMPT='%{$fg_bold[red]%}-> %{$fg_bold[magenta]%}%n%{$fg_bold[cyan]%}@%{$fg[green]%}%m %{$fg_bold[green]%}%p%{$fg[cyan]%}%~ %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%}% %{$fg[magenta]%}%(?..%?%1v)%{$fg_bold[blue]%}? %{$fg[yellow]%}# '


ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

修改 ~/.zshrc

ZSH_THEME="myrobbyrussell"

参考设置

#1 
PROMPT='%{$fg[green]%}%m@%{$fg[magenta]%}%(?..%?%1v)%n:%{$reset_color%}%{$fg[cyan]%}%~#'

#2 
PROMPT='%{$fg_bold[red]%}-> %{$fg_bold[green]%}%p%{$fg[cyan]%}%d %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%}% %{$reset_color%}~#:'

#3 
PROMPT='%{$fg_bold[red]%}-> %{$fg_bold[green]%}%p%{$fg[cyan]%}%d %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%}% %{$fg[magenta]%}%(?..%?%1v)%{$reset_color%}~#: '    

#4 
PROMPT='%{$fg_bold[red]%}-> %{$fg_bold[magenta]%}%n%{$fg_bold[cyan]%}@%{$fg[green]%}%m %{$fg_bold[green]%}%p%{$fg[cyan]%}%~ %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%}% %{$fg[magenta]%}%(?..%?%1v)%{$fg_bold[blue]%}? %{$fg[yellow]%}# ' 

# 5 推荐
PROMPT='%{$fg_bold[red]%}-> %{$fg_bold[magenta]%}%n%{$fg_bold[cyan]%}@%{$fg[green]%}%m %{$fg_bold[green]%}%p%{$fg[cyan]%}%~ %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%}% %{$fg[magenta]%}%(?..%?%1v)%{$fg_bold[blue]%}? %{$reset_color%}~#: ' 

zsh 与bash 的共用问题

方案
为了便于在bash和zsh切换后可以使用同样的配置的alias等配置,采用如下方案:

自定义配置放在~/.profile中
.bashrc配置文件中使用source ~/.profile加载自定义配置
.zshrc配置文件中使用[[ -e ~/.profile ]] && emulate sh -c ‘source ~/.profile’加载自定义配置
配置文件示例如下:

.bashrc
zzzzzzzzzzzzz原有配置
下面一行为新加配置
source ~/.profile
.zshrc
zzzzzzzzzzzzz原有配置
下面一行为新加配置
 [[ -e ~/.profile ]] && emulate sh -c 'source ~/.profile'

zsh 与 bash 加载环境配置文件的顺序

bash加载配置文件顺序

login 和 non-login bash shell 的情况下,读取的配置文件数据并不一样所致。

一般来说 login shell 会读取两个配置文件:

  1. /etc/profile:这是系统整体的设定,修改这个文件要慎重

  2. ~/.bash_profile 或 ~/.bash_login 或~/.profile:属于使用者个人设定,要改自己的数据,可以写在这里。

bash 在读完整体环境设定的 /etc/profile 并藉此呼叫其他配置文件后,接下来则会读取使用者的个人配置文件。在login shell 的 bash 环境中,所读取的个人偏好配置文件主要有三个,依顺序分别为:

png

实线的方向是主线程流程,虚线的方向则是被调用的配置文件。

source与.bashrc

non-login shell 这种非登入情况取得 bash 操作接口的环境配置文件又是什么? 当你取得 non-login shell 时,该 bash 配置文件仅会读取 ~/.bashrc 而已。

由于 /etc/profile 与 ~/.bash_profile 都是在取得 login shell 的时候才会读取的配置文件,所以,如果将自己的偏好设定写入上述文件后,通常都是得注销在登入后,改设定才会生效。那么,能不能直接读取配置文件而不注销登入呢?可以的,那就是利用 source 这个指令。

利用 source 或小数的(.)都可以将配置文件的内容读进来目前的 shell 环境中。

zsh加载配置文件顺序

当 Zsh 启动时,它会按照顺序依次读取下面的配置文件:

  1. /etc/zsh/zshenv

该文件应该包含用来设置PATH 环境变量[broken link: invalid section]以及其他一些环境变量的命令;不应该包含那些可以产生输出结果或者假设终端已经附着到 tty 上的命令。

  1. ~/.zshenv

该文件和 /etc/zsh/zshenv 相似,但是它是针对每个用户而言的。一般来说是用来设置一些有用的环境变量。

  1. /etc/zsh/zprofile

这是一个全局的配置文件,在用户登录的时候加载。一般是用来在登录的时候执行一些命令。请注意,在 Arch Linux 里该文件默认包含一行配置,用来加载 /etc/profile 文件,详见 #全局配置文件。

  1. /etc/profile

在登录时,该文件应该被所有和伯克利(Bourne)终端相兼容的终端加载:它在登录的的时候会加载应用相关的配置(/etc/profile.d/*.sh)。注意在 Arch Linux 里,Zsh 会默认加载该文件。

  1. ~/.zprofile

该文件一般用来在登录的时候自动执行一些用户脚本。

  1. /etc/zsh/zshrc

当 Zsh 被作为交互式终端的时候,会加载这样一个全局配置文件。

  1. ~/.zshrc

当 Zsh 被作为交互式终端的时候,会加载这样一个用户配置文件。

  1. /etc/zsh/zlogin

在登录完毕后加载的一个全局配置文件。

  1. ~/.zlogin

和 /etc/zsh/zlogin 相似,但是它是针对每个用户而言的。

  1. /etc/zsh/zlogout

在注销的时候被加载的一个全局配置文件。

  1. ~/.zlogout

和 /etc/zsh/zlogout 相似,但是它是针对每个用户而言的.

参数说明

code info
%T 系统时间(时:分)
%* 系统时间(时:分:秒)
%D 系统日期(年-月-日)
%n 你的用户名
%B - %b 开始到结束使用粗体打印
%U - %u 开始到结束使用下划线打印
%d 你目前的工作目录
%~ 你目前的工作目录相对于~的相对路径
%M 计算机的主机名
%m 计算机的主机名(在第一个句号之前截断)
%l 你当前的tty
%n 登录名

CENTOS 8 替换yum源

cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
cp /etc/yum.repos.d/CentOS-AppStream.repo /etc/yum.repos.d/CentOS-AppStream.repo.bak
cp /etc/yum.repos.d/CentOS-Extras.repo /etc/yum.repos.d/CentOS-Extras.repo.bak

sed -i 's/mirrorlist=/#mirrorlist=/g' /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-AppStream.repo /etc/yum.repos.d/CentOS-Extras.repo
sed -i 's/#baseurl=/baseurl=/g' /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-AppStream.repo /etc/yum.repos.d/CentOS-Extras.repo
sed -i 's/http:\/\/mirror.centos.org/https:\/\/mirrors.aliyun.com/g' /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-AppStream.repo /etc/yum.repos.d/CentOS-Extras.repo

cp /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.bak
cp /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.bak
cp /etc/yum.repos.d/epel-playground.repo /etc/yum.repos.d/epel-playground.repo.bak

sed -i 's/metalink=/#metalink=/g' /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-playground.repo
sed -i 's/#baseurl=/baseurl=/g' /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-playground.repo
sed -i 's/https:\/\/download.fedoraproject.org\/pub/https:\/\/mirrors.aliyun.com/g' /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-playground.repo

最后, 懒人脚本

if [ `whoami` != 'root' ]
then
    echo 'you must run this script as root'
    exit 0
fi
yum install -y epel-release

version =`cat /etc/centos-release | awk '{print $4}' | cut -d . -f1`
if [[ version ==  8 ]]; then

    cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
    cp /etc/yum.repos.d/CentOS-AppStream.repo /etc/yum.repos.d/CentOS-AppStream.repo.bak
    cp /etc/yum.repos.d/CentOS-Extras.repo /etc/yum.repos.d/CentOS-Extras.repo.bak


    sed -i 's/mirrorlist=/#mirrorlist=/g' /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-AppStream.repo /etc/yum.repos.d/CentOS-Extras.repo
    sed -i 's/#baseurl=/baseurl=/g' /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-AppStream.repo /etc/yum.repos.d/CentOS-Extras.repo
    sed -i 's/http:\/\/mirror.centos.org/https:\/\/mirrors.aliyun.com/g' /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-AppStream.repo /etc/yum.repos.d/CentOS-Extras.repo


    cp /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.bak
    cp /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.bak
    cp /etc/yum.repos.d/epel-playground.repo /etc/yum.repos.d/epel-playground.repo.bak

    sed -i 's/metalink=/#metalink=/g' /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-playground.repo
    sed -i 's/#baseurl=/baseurl=/g' /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-playground.repo
    sed -i 's/https:\/\/download.fedoraproject.org\/pub/https:\/\/mirrors.aliyun.com/g' /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-playground.repo

fi
if [[ version == 7 ]]; then
    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
    mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
fi


yum install -y  vim zsh git lrzsz wget

systemctl stop firewalld
systemctl disable firewalld

# 关闭selinux
setenforce 0
# 永久关闭
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
sed -i "s/^SELINUX=permissive/SELINUX=disabled/g" /etc/sysconfig/selinux
sed -i "s/^SELINUX=permissive/SELINUX=disabled/g" /etc/selinux/config

mkdir ~/.pip
touch ~/.pip/pip.conf
cat > ~/.pip/pip.conf <<-EOF
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com
EOF
read -p "请输入hostname 名称 :" host
hostnamectl --static set-hostname $host

# install oh my zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
cat > ~/.oh-my-zsh/themes/myrobbyrussell.zsh-theme <<-EOF

PROMPT='%{$fg_bold[red]%}-> %{$fg_bold[magenta]%}%n%{$fg_bold[cyan]%}@%{$fg[green]%}%m %{$fg_bold[green]%}%p%{$fg[cyan]%}%~ %{$fg_bold[blue]%}\$(git_prompt_info)%{$fg_bold[blue]%}% %{$fg[magenta]%}%(?..%?%1v)%{$fg_bold[blue]%}? %{$reset_color%}~#: '


ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"
EOF
sed -i 's/ZSH_THEME="robbyrussell"/ZSH_THEME="agnoster"/g' ~/.zshrc
# chsh -s /usr/bin/zsh 更换使用的shell 程序为zsh
上一篇下一篇

猜你喜欢

热点阅读