Ubuntu 18.4 通用配置脚本

2019/12/26 Linux Ubuntu

Ubuntu 18.4 通用配置脚本

配置 ip

先决条件:网口名为 eno1 且为联通状态,若不是,请手动修改。

#! /bin/bash
# config_ip.sh
# sh config_ip.sh eno1 192.168.1.xxx 192.168.1.1
mv /etc/netplan /etc/netplan.bk
mkdir -p /etc/netplan/
touch /etc/netplan/01-netcfg.yaml
cat > /etc/netplan/01-netcfg.yaml<<EOF
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    $1:
      addresses: [$2/24]
      gateway4: $3
      nameservers:
        addresses:
          - "$3"
EOF

cat /etc/netplan/01-netcfg.yaml
echo 'Finish!'
netplan apply
if [ $? -eq 0]
then
    echo 'Apply Success!'
else
    echo 'Apply Failure!'
fi

配置 DNS

#! /bin/bash
# config_dns.sh
# sh config_dns.sh 192.168.1.x
mv /etc/systemd/resolved.conf /etc/systemd/resolved.conf.bk

cat > /etc/systemd/resolved.conf <<EOF
[Resolve]
DNS=$1
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes
EOF

配置源

#! /bin/bash
# config_source.sh
# sh config_source.sh
mv /etc/apt/sources.list /etc/apt/sources.list.bk
cat > /etc/apt/sources.list <<EOF
# tsinghua
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://packages.grafana.com/oss/deb stable main
deb-src https://packages.grafana.com/oss/deb stable main
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

# aliyun
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

# ubuntu
deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
EOF
apt-get update

配置集群免密登陆

生成默认密钥对

ssh-keygen -t rsa -C "default_key@default.namespace"
ls ~/.ssh/

获取认证文件

ssh-copy-id -i ~/.ssh/id_rsa xxx@xxx

通过生成的密钥通过 ssh-copy-id 指定一台机器,那么那台机器的 ~/.ssh/authorized_keys 就会更新,录入一条新的数据。

写 config

获得上述两个文件后,统一放在 ~/.ssh/ 下,同时创建 ~/.ssh/config,写入以下内容:

# 匹配 1.* 网段
Host 1.*
Hostname 192.168.%h
User root
IdentityFile ~/.ssh/id_rsa

将这三个文件同步到每台节点即可。

修改 hostname

#! /bin/bash
# config_hostname.sh
# sh config_hostname.sh $HOSTNAME xxxx
cp /etc/hostname /etc/hostname.bk
cp /etc/hosts /etc/hosts.bk
HOSTNAME=$1
NEW=$2
sed -i "s/${HOSTNAME}/${NEW}/" /etc/hostname
sed -i "s/${HOSTNAME}/${NEW}/" /etc/hosts
sed -i "s/preserve_hostname: false/preserve_hostname: true/" # 针对 18.04.2 版本,不修改则重启后重置hostname
echo "================"
cat /etc/hostname
echo "================"
cat /etc/hosts
hostname $NEW

启动 root 用户并授权远程登录

sudo apt install openssh-server
sudo passwd root  
sudo sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/" /etc/ssh/sshd_config
sudo systemctl restart sshd

用户增加sudo权限

usermode修改:

sudo usermod -aG 超级用户组名  用户名
sudo  usermod -aG sudo username
# 其中a:表示添加,G:指定组名

修改sudoers文件:

# 给单个用户添加sudo权限
username ALL=(ALL:ALL)  ALL

# 给用户组添加sudo权限
%groupname ALL=(ALL:ALL)  ALL

参考

Search

    Table of Contents