# Linux 配置

# 一、Centos 常用设置

# 1.安装必要的软件

yum install -y perl lrzsz wget zsh git ebtables socat ipset conntrack yum-utils nfs-utils net-tools.x86_64 telnet gcc epel-release
1

# 2.处理乱码问题

[root@hdp1 ~]# vi /etc/environment
LANG="zh_CN.UTF-8"
LC_ALL="zh_CN.UTF-8"
1
2
3

# 3.免密登录

ssh-copy-id root@master01
1

# 4.zsh

传送门

# 5.设置hostname

hostnamectl set-hostname centos7base
1

# 6.关闭selinux

# 查看Selinux状态
getenforce

# 将 SELinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
1
2
3
4
5
6

# 7.关闭swap

swapoff -a  
sed -ri 's/.*swap.*/#&/' /etc/fstab
#sed -i 's$/dev/mapper/centos-swap$#/dev/mapper/centos-swap$g' /etc/fstab 同上
#echo "vm.swappiness=0" >> /etc/sysctl.conf 
#sysctl -p /etc/sysctl.conf 
1
2
3
4
5

# 8.关闭防火墙

# 查看防火墙状态
firewall-cmd --state

# 停止firewall
systemctl stop firewalld.service

# 禁止firewall开机启动
systemctl disable firewalld.service 

# 新增开放端口
firewall-cmd --zone=public --add-port=端口号/tcp --permanent

# 移除开放端口
firewall-cmd --zone=public --remove-port=端口号/tcp --permanent

# 查看开放的端口
firewall-cmd --zone=public --list-ports

# 刷新防火墙
firewall-cmd --reload

#允许 iptables 检查桥接流量
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

# 9.时间同步

# 安装chrony
yum -y install chrony

# 修改同步服务器地址为阿里云
sed -i.bak '3,6d' /etc/chrony.conf && sed -i '3cserver ntp1.aliyun.com iburst' \
/etc/chrony.conf

# 启动chronyd及加入开机自启
systemctl start chronyd && systemctl enable chronyd

# 查看同步结果
chronyc sources 
1
2
3
4
5
6
7
8
9
10
11
12

# 10 集群时间同步(不连外网状态下)

注意先调整好所有机器的时区

# 方法1
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 方法2
timedatectl list-timezones
sudo timedatectl set-timezone Asia/Shanghai
1
2
3
4
5
6

# master01 操作步骤

  1. 查看所有节点ntpd 服务状态和开机自启动状态
systemctl status ntpd
sudo systemctl start ntpd
sudo systemctl is-enabled ntpd
1
2
3
  1. 修改master01的/etc/ntp.conf配置文件

修改前

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
1
2
3
4
5
6
7
8
9

修改后

# 授权192.168.235.0 - 192.168.235.255 网段上的所有机器可以从这台机器上查询和同步时间
192.168.235.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).

# 不用外网时间
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

# 添加属性 当该节点丢失网络连接,依然可以雌蛾用本地时间作为时间服务器为群集中的其他节点提供时间同步
server 127.127.1.0
fudge 127.127.1.0 stratum 10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  1. 修改mater01的/etc/sysconfig/ntpd文件

增加如下内容(让硬件时间与系统时间一起同步)

SYNC HWCLOCK=yes
1
  1. 重启启动ntpd服务
sudo systemctl start ntpd
sudo systemctl enable ntpd
1
2

# 其他节点操作步骤

  1. 关闭所有节点上的ntp服务和自启动
sudo systemctl stop ntpd
sudo systemctl disable ntpd
1
2
  1. 配置间隔1分钟与时间服务器同步一次
[tpxcer@slave01 hadoop-3.3.1]$ sudo crontab -e
*/1 * * * * /usr/sbin/ntpdate master01 >/dev/null 2>&1
1
2
  1. 测试一下
# 修改master01的时间
sudo date -s "2021-09-01 11:11:11"

# 1分钟后查看其他机器是否与时间服务器同步
date
1
2
3
4
5

# 11.同步执行命令脚本xcall

可以放在 /usr/local/bin

#!/bin/bash

# 获取控制台指令

cmd=$*

# 判断指令是否为空
if [ $# -eq 0 ];
then 
	echo "command can not be null !"
	exit
fi

# 获取当前登录用户
user=`whoami`

# 在从机执行指令,这里需要根据你具体的集群情况配置,host与具体主机名一致
for host in ks.m1 ks.m2 ks.m3 ks.s1
do
    echo "================current host is $host================="
	echo "--> excute command \"$cmd\""
    ssh $user@$host $cmd
done

echo "excute successfully !"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# 12.同步复制文件脚本xsync

可以放在 /usr/local/bin

#!/bin/bash

#1. 判断参数个数
if [ $# -lt 1 ]; then
    echo "Not Enough Arguement!"
    exit;
fi

#2. 遍历集群所有机器
for host in slave01 slave02 slave03
#for host in slave01
do
echo ==================== $host ====================
    #3. 遍历所有目录,挨个发送
    for file in $@
    do
        #4. 判断文件是否存在
        if [ -e $file ]
            then
                #5. 获取父目录
                pdir=$(cd -P $(dirname $file); pwd)
                #6. 获取当前文件的名称
                fname=$(basename $file)
                ssh $host "mkdir -p $pdir"
                rsync -av $pdir/$fname $host:$pdir
            else
                echo $file does not exists!
        fi
    done
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# 13.安装JDK

# 卸载已安装的JDK

rpm -qa | grep -i java | xargs -n1 rpm -e --nodeps
1

# 安装JDK

Oracle官方 (opens new window) 下载JDK然后解压到目录

tar -zxvf ~/jdk-8u301-linux-x64.tar.gz
1

# 配置JDK变量

创建新的变量文件,放到/etc/profile.d

cd /etc/profile.d
vim my_env.sh
source /etc/profile
1
2
3

文件内容

#JAVA_HOME
export JAVA_HOME=/opt/jdk1.8.0_311
export PATH=$PATH:$JAVA_HOME/bin
1
2
3

# 14 设置静态ip

修改/etc/sysconfig/network-scripts/ifcfg-ens33,将网络改为静态ip

这里的ens33根据设备不同编号也不同

原内容:

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="47eb256e-1189-4dcb-b28b-cefb7dd76c18"
DEVICE="ens33"
ONBOOT="yes"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

修改后:

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=47eb256e-1189-4dcb-b28b-cefb7dd76c18
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.235.5
PREFIX=24
GATEWAY=192.168.235.1
DNS1=192.168.235.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 15. nfs服务

1.安装nfs-server

# 在每个机器。
yum install -y nfs-utils

# 在master 执行以下命令 
echo "/nfs/data/ *(insecure,rw,sync,no_root_squash)" > /etc/exports

# 执行以下命令,启动 nfs 服务;创建共享目录
mkdir -p /nfs/data

# 在master执行
systemctl enable rpcbind
systemctl enable nfs-server
systemctl start rpcbind
systemctl start nfs-server

# 使配置生效
exportfs -r

#检查配置是否生效
exportfs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

2.配置nfs-client(选做,所有节点)

showmount -e 192.168.50.3
mkdir -p /nfs/data
mount -t nfs 192.168.50.3:/mnt/Apps/k8s-data /nfs/data

# 开机自动挂载,编辑/etc/fstab
192.168.50.3:/mnt/Apps/zk-hdp1  /opt/apache-zookeeper/zkData nfs rw
1
2
3
4
5
6

# 16. 配置用户sudo权限

编辑/etc/sudoers把用户tpxcer加入sudo

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL

## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL

## Same thing without a password
tpxcer  ALL=(ALL)       NOPASSWD: ALL
1
2
3
4
5
6
7
8
9
10
11
12

# 17. 安装dnf

yum install epel-release
yum install dnf
dnf install 'dnf-command(config-manager)'
1
2
3

# 18. systemctl 设置后台服务的自启配置

  1. 基本语法
systemctl list-unit-files (功能描述:查看服务开机启动状态)
systemctl disable service_name (功能描述:关掉指定服务的自动启动)
systemctl enable service_name (功能描述:开启指定服务的自动启动)
1
2
3
  1. 例子 开启/关闭 iptables(防火墙)服务的自动启动
systemctl enable firewalld.service
systemctl disable firewalld.service
1
2

# 19. esxi增加磁盘容量后的扩容操作

# 19.1 未添加硬盘的扩容

查看当前硬盘列表

# 查看当前硬盘列表
[root@node01 opt]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   15G  0 part
  ├─centos-root 253:0    0 13.4G  0 lvm  /
  └─centos-swap 253:1    0  1.6G  0 lvm
sr0              11:0    1  973M  0 rom
1
2
3
4
5
6
7
8
9

进行重新分区(并非删除数据)

[root@node01 opt]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。


命令(输入 m 获取帮助):p

磁盘 /dev/sda:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000aede3

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    33554431    15727616   8e  Linux LVM

命令(输入 m 获取帮助):d
分区号 (1,2,默认 2):
分区 2 已删除

命令(输入 m 获取帮助):p

磁盘 /dev/sda:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000aede3

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux

命令(输入 m 获取帮助):n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
分区号 (2-4,默认 2):
起始 扇区 (2099200-209715199,默认为 2099200):
将使用默认值 2099200
Last 扇区, +扇区 or +size{K,M,G} (2099200-209715199,默认为 209715199):
将使用默认值 209715199
分区 2 已设置为 Linux 类型,大小设为 99 GiB

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
正在同步磁盘。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

刷新分区信息

partprobe
1

再次查看硬盘情况

[root@node01 opt]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   99G  0 part
  ├─centos-root 253:0    0 13.4G  0 lvm  /
  └─centos-swap 253:1    0  1.6G  0 lvm
sr0              11:0    1  973M  0 rom
1
2
3
4
5
6
7
8

使用 pvresize 命令将物理卷扩展到新的可用空间上

pvresize /dev/sda2
1

用lvm命令扩容硬盘

[root@node01 opt]# lvextend -r -l +100%FREE /dev/centos/root
  Size of logical volume centos/root changed from 13.39 GiB (3429 extents) to 97.39 GiB (24933 extents).
  Logical volume centos/root successfully resized.
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=877824 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=3511296, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 3511296 to 25531392
1
2
3
4
5
6
7
8
9
10
11
12
13

查看扩容结果

[root@node01 opt]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   99G  0 part
  ├─centos-root 253:0    0 97.4G  0 lvm  /
  └─centos-swap 253:1    0  1.6G  0 lvm
sr0              11:0    1  973M  0 rom
1
2
3
4
5
6
7
8

# 二、Ubuntu 常用设置

# 1.中文支持

# 编码配置
dpkg-reconfigure locales

# 设置LC_ALL变量
export LC_ALL="en_US.UTF-8"
1
2
3
4
5

# 2.Mirror

中国科技技术大学镜像 (opens new window)

# 3.SSH

sudo apt install openssh-server
sudo systemctl status ssh
1
2

# 4.可选程序设置

ls /usr/bin/python*
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2
sudo update-alternatives --config python
1
2
3
4

# 三、Debian 常用设置

# 3.1 增加sudo用户

编辑/etc/sudoers

user    ALL=(ALL:ALL) ALL
1

# 3.2 把DVD源去掉

sudo vi /etc/apt/sources.list
1

# 3.3 终端显示中文

export LC_ALL=zh_CN.utf-8
dpkg-reconfigure locales
1
2

# 3.4 NFS(客户端)

传送门

apt-cache search showmount
apt-get install nfs-common
1
2

# 3.5 去掉安装系统时候使用的代理

编辑文件/etc/apt/apt.conf,删除代理

Acquire::http::Proxy "http://192.168.50.216:1087";
1

# 3.6 配置静态路由

路由追踪

traceroute 192.168.50.1
1

添加路由

ip route add 10.8.0.0/24 via 192.168.50.1
1

查看路由列表

OMV-➜  ~ ip route list
default via 192.168.50.4 dev ens192 proto static
10.8.0.0/24 via 192.168.50.1 dev ens192
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
172.20.0.0/16 dev br-b2702b8521f6 proto kernel scope link src 172.20.0.1
192.168.50.0/24 dev ens192 proto kernel scope link src 192.168.50.3

OMV-➜  ~ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         _gateway        0.0.0.0         UG    0      0        0 ens192
10.8.0.0        RT-AX86U-6010   255.255.255.0   UG    0      0        0 ens192
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.20.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-b2702b8521f6
192.168.50.0    0.0.0.0         255.255.255.0   U     0      0        0 ens192
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

加到网络配置文件中/etc/network/interfaces

up ip route add 10.8.0.0/24 via 192.168.50.1 dev ens192
1

# 四、通用配置

# 重新配置默认Java版本

update-alternatives --config java
1

# 禁Ping

修改/etc/sysctl.conf加一行

net.ipv4.icmp_echo_ignore_all = 1
1

执行sysctl -p加载配置后生效。

临时启用Ping

echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all
1

# 禁止IP

iptables -I INPUT -s 124.115.0.199 -j DROP
/etc/rc.d/init.d/iptables save
service iptables restart
1
2
3

# 修改DNS

vim /etc/resolv.conf
1

# 在PATH中添加一条新路径

export PATH="$PATH:/home/user/bin"
1

# 删除用户

# r 用于彻底删除,用户HOME目录下的档案会被移除,在其他位置上的档案也将一一找出并删除,比如路径/var/mail/用户名下的邮件。
userdel -r ubuntu
1
2

# 格式化硬盘

fdisk -l
fdisk /dev/sdc
mkfs.ext4 /dev/sdc
mkdir /mnt/M06
mount /dev/sdc /mnt/M06
df -h
1
2
3
4
5
6
更新时间: 5/24/2023, 1:14:31 PM