ubuntu 18.04 初始化配置

ubuntu18.04.init.sh
#!/bin/bash
#配置ssh
sed -i -e "/GSSAPIAuthentication/s/yes/no/g" -e "/GSSAPICleanupCredentials/s/yes/no/g" -e"s/^#UseDNS\ no/UseDNS\ no/" -e"s/^#UseDNS\ yes/UseDNS\ no/" /etc/ssh/sshd_config
echo -ne "ClientAliveInterval 60\nClientAliveCountMax 10" >> /etc/ssh/sshd_config
systemctl restart sshd
#调整log级别
sed -i 's/\#LogLevel=info/LogLevel=notice/g' /etc/systemd/system.conf
systemctl daemon-reexec
# 禁用systemd-timesyncd
systemctl stop systemd-timesyncd
systemctl disable systemd-timesyncd
# 优化ssh
cat >> /etc/ssh/ssh_config <<'EOF'
Host *
   StrictHostKeyChecking no
   UserKnownHostsFile=/dev/null
EOF
 
# 禁止登陆welcome信息
sudo chmod -x /etc/update-motd.d/*
# 使用ustc源
cat > /etc/apt/sources.list <<'EOF'
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src 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-backports main restricted universe multiverse
EOF
apt clean all
apt update -y
# 安装常用软件包
apt install vim wget net-tools iperf3 telnet lvm2 tree screen tmux rsync lrzsz zip unzip ntpdate sysstat hdparm htop iotop iftop dstat nmap mtr mlocate dnsutils ipmitool pciutils parted acpid man-db bash-completion lsof bc ncdu
# 自定义设置
cat >> /etc/bash.bashrc <<'EOF'
export PATH=/usr/local/bin:$PATH
export TERM=xterm-256color
export EDITOR=/usr/bin/vim
# man page
export LESS_TERMCAP_mb=$(printf '\e[01;31m') # enter blinking mode - red
export LESS_TERMCAP_md=$(printf '\e[01;35m') # enter double-bright mode - bold, magenta
export LESS_TERMCAP_me=$(printf '\e[0m') # turn off all appearance modes (mb, md, so, us)
export LESS_TERMCAP_se=$(printf '\e[0m') # leave standout mode
export LESS_TERMCAP_so=$(printf '\e[01;33m') # enter standout mode - yellow
export LESS_TERMCAP_ue=$(printf '\e[0m') # leave underline mode
export LESS_TERMCAP_us=$(printf '\e[04;36m') # enter underline mode - cyan
# history tune
shopt -s histappend
export HISTTIMEFORMAT="%F %T "
export HISTCONTROL=ignoredups:erasedups
export HISTSIZE=100000
export HISTFILESIZE=100000
export PROMPT_COMMAND="history -a"
EOF
# vim 
cat >> /etc/vimrc <<'EOF'
set ts=4
set paste
EOF
# disable ipv6
cat >> /etc/sysctl.d/99-sysctl.conf <<'EOF'
# disable ipv6
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
EOF
# 尽量使用物理内存
echo 'vm.swappiness = 0' >> /etc/sysctl.conf
# 开启BBR
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p
# 启用root用户的promt颜色
sed -i '/force_color_prompt/s/^#//g' /root/.bashrc
# 删除其他不需要的组件
sudo apt autoremove --purge snapd lxd cloud-init apparmor

ubuntu 18.04 改用netplan来配置网络参数了,原来的“/etc/network/interfaces”就被弃用,建议参照此处来配置netplan,如果搞不定或者其他原因还是需要使用原来的配置方式,那么下面就是了:

安装ifupdown:

apt-get update
apt-get install ifupdown

按照之前的方式配置interface:

cat > /etc/network/interfaces <<'EOF'
source /etc/network/interfaces.d/*
 
# The loopback network interface
auto lo
iface lo inet loopback
 
auto eno1
iface eno1 inet static
  address 192.168.1.133
  netmask 255.255.255.0
  gateway 192.168.1.1
  dns-nameservers 119.29.29.29 119.28.28.28
EOF

立即生效:

ifdown --force eno1 lo && ifup -a

禁用并卸载netplan:

systemctl stop networkd-dispatcher
systemctl disable networkd-dispatcher
systemctl mask networkd-dispatcher
apt-get purge nplan netplan.io

ubuntu 16.04开始就弃用rc.local了,如果还需要使用,按照下面的步骤操作:

cp /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service
cat >> /etc/systemd/system/rc-local.service <<'EOF'
[Install]
 WantedBy=multi-user.target
EOF
# /etc/rc.local内容
cat > /etc/rc.local <<'EOF'
#!/bin/bash
echo qqq > /var/1.txt
EOF
# 添加执行权限
chmod +x /etc/rc.local
# 设置开机启动
systemctl enable rc-local
systemctl start rc-local.service
systemctl status rc-local.service

  • linux/others/ubuntu18.04_initial.txt
  • 最后更改: 2019/08/14 09:37
  • 由 mrco