Ubuntu系统初始化脚本

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
#!/usr/bin/env bash

# 更新源并安装所需的依赖包
packages="lrzsz wget bash-completion make g++ unzip zip net-tools"
sudo apt -y update
for package in $packages;do [ -z $(dpkg -l | grep net-tools) ] && sudo apt -y install ¥package ;done


# 设置系统文件最大数
ulimit -n 65536

[ -z $(egrep -v "^#|^$" /etc/security/limits.conf) ] &&\
cat << eof | sudo tee /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 65536
* hard nproc 65536
eof

# 普通用户输入免密码
user=$()
cat << eof | sudo tee /etc/sudoers.d/$(whoami)
$(whoami) ALL=(ALL) NOPASSWD:ALL
eof

# 设置主机名
IPADDRESS=$(ip a|grep "eth0" |egrep -i "inet"| awk -F ' ' '{print $2}' |awk -F '/' '{print $1}')
sudo hostnamectl set-hostname XB-NG-PY-$IPADDRESS

sudo sed -i "s#PS1='${debian_chroot:+($debian_chroot)}\\[\\033[01;32m\\]\\u@\\h\\[\\033[00m\\]:\[\\033[01;34m\\]\w\\[\033[00m\\]\\$ '#PS1='${debian_chroot:+($debian_chroot)}\\[\\033[01;32m\]\\u@\H\\[\\033[00m\\]:\\[\\033[01;34m\\]\w\\[\\033[00m\]\\$ '#" /home/xinbo/.bashrc

source /home/xinbo/.bashrc

sudo su - $(whoami)

# 修改默认端口
ssh_port="34235"
sudo sed -i 's@#Port 22@#Port $34235@' /etc/ssh/sshd_config

# 开启防火墙并开放ssh端口
[ $(sudo ufw status|egrep -i "Status"| awk -F ': ' '{print $NF}' ) == 'inactive' ] && echo "y" | sudo ufw enable

sudo ufw allow $ssh_port/tcp && sudo ufw reload

sudo systemctl daemon-reload && sudo systemctl restart ssh