Linux

Ubuntu初始化配置

以下命令大多需要root权限,如非root请增加sudo

配置允许免密码切换(慎重)

vi /etc/sudoers
    %sudo ALL=(ALL:ALL) ALL         # 注释该行,表示sudo组用户可以从任何“主机”上登陆以任何用户的“身份”执行任何“命令”;
    %sudo ALL=NOPASSWD: ALL         # 追加一行(27行)

配置主机名

vi /etc/hostname
    Cloud
vi /etc/hosts
    127.0.0.1 Cloud

配置网络

vi /etc/network/interfaces
    auto eth0
    iface eth0 inet static
    address 10.2.20.32
    gateway 10.2.20.1
    netmask 255.255.255.0

修改后重启网络

快速配置从模板创建的虚拟机主机名及IP()

sed -i 's#Ubuntu#Ubuntu-Cluster5#g' /etc/hostname
sed -i 's#Ubuntu#Ubuntu-Cluster5#g' /etc/hosts
hostname Ubuntu-Cluster5
sed -i 's#240#35#g' /etc/network/interfaces
ifdown eth0;ifup eth0

修改密码

passwd ubuntu

配置DNS

grep 202.106.0.20    /etc/resolv.conf||echo 'nameserver 202.106.0.20' >> /etc/resolv.conf
grep 219.141.136.10  /etc/resolv.conf||echo 'nameserver 219.141.136.10' >> /etc/resolv.conf
grep 114.114.114.114 /etc/resolv.conf||echo 'nameserver 114.114.114.114' >> /etc/resolv.conf
grep 8.8.8.8         /etc/resolv.conf||echo 'nameserver 8.8.8.8' >> /etc/resolv.conf
grep 4.4.4.4         /etc/resolv.conf||echo 'nameserver 4.4.4.4' >> /etc/resolv.conf

避免重启后失效,做以下配置:

grep 202.106.0.20 /etc/resolvconf/resolv.conf.d/base||echo 'nameserver 202.106.0.20' >> /etc/resolvconf/resolv.conf.d/base
grep 219.141.136.10  /etc/resolvconf/resolv.conf.d/base||echo 'nameserver 219.141.136.10' >> /etc/resolvconf/resolv.conf.d/base
grep 114.114.114.114 /etc/resolvconf/resolv.conf.d/base||echo 'nameserver 114.114.114.114' >> /etc/resolvconf/resolv.conf.d/base
grep 8.8.8.8         /etc/resolvconf/resolv.conf.d/base||echo 'nameserver 8.8.8.8' >> /etc/resolvconf/resolv.conf.d/base
grep 4.4.4.4         /etc/resolvconf/resolv.conf.d/base||echo 'nameserver 4.4.4.4' >> /etc/resolvconf/resolv.conf.d/base

重启后/etc/resolv.conf/etc/resolvconf/resolv.conf.d/base被覆盖

设置时区,校对时间

mv /etc/localtime /etc/localtime.$(date +%F_%T)
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
/usr/sbin/ntpdate s1a.time.edu.cn
grep ntpdate /var/spool/cron/root>/dev/null 2>&1||echo '55 23 * * * /usr/sbin/ntpdate s1a.time.edu.cn' >> /var/spool/cron/root
hwclock --systohc

配置UserAgent

grep MrUse ~/.wgetrc>/dev/null 2>&1||cat >> ~/.wgetrc <<WGETRC
# @start ~/.wgetrc Create by MrUse $(date +%F_%T)
Header=User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:37.0) Gecko/20100101 Firefox/37.0 Marked by MrUse
Header=Referer: https://www.baidu.com/
#http_proxy=""
#ftp_proxy=""
# @end ~/.wgetrc
WGETRC

grep MrUse ~/.curlrc>/dev/null 2>&1||cat >> ~/.curlrc <<CURLRC
# @start ~/.curlrc Create by MrUse $(date +%F_%T) User-Agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:37.0) Gecko/20100101 Firefox/37.0 Marked by MrUse"
Header="X-Forwarded-For: https://www.baidu.com/"
# @end ~/.curlrc
CURLRC

优化

ulimit -HSn 65535
grep MrUse /etc/security/limits.conf>/dev/null 2>&1||cat >> /etc/security/limits.conf <<LIMITS
# MrUse Limits $(date +%F_%T)
*   soft    nproc   65535
*   hard    nproc   65535
*   soft    nofile  65535
*   hard    nofile  65535
LIMITS
echo 2048 > /proc/sys/net/core/somaxconn

自定义命令别名

grep ll= ~/.bashrc|| echo 'alias ll="ls -lhp --color"' >> ~/.bashrc && source ~/.bashrc

配置源,更新

dig us.archive.ubuntu.com  # 查看源中域名,下面把域名换成IP

sudo cp /etc/apt/sources.list /etc/apt/sources.list.default
sudo sed -i 's#us.archive.ubuntu.com#91.189.91.13#g' /etc/apt/sources.list
sudo sed -i 's#http://#ftp://#g' /etc/apt/sources.list
sudo sed -i 's#cn.arhive.ubuntu.com#112.124.140.210#g'  /etc/apt/sources.list

sudo apt-get clean
sudo rm -rf /var/lib/apt/lists/partial/*
sudo rm -rf /var/lib/apt/lists/*
sudo apt-get clean
sudo apt-get -y update --fix-missing
sudo apt-get -y upgrade
sudo sync

颜色值写入环境变量

grep '^# MrUse Color' /etc/profile>/dev/null 2>&1||cat >> /etc/profile <<COLOR

# MrUse Color $(date +%F_%T)
export red='\e[1;31m'
export redbg='\e[1;41m'
export blue='\e[1;34m'
export bluebg='\e[1;44m'
export green='\e[1;32m'
export greenbg='\e[1;42m'
export eol='\e[0m'
COLOR
source /etc/profile

修改登陆后显示信息

在CentOS上可以编辑/etc/motd,设置登陆后的提示信息。
在Ubuntu下需要修改/var/run/motd

更新

yum -y update

安装常用工具

# 硬件工具
apt-get -y install dmidecode dmesg hdparm parted e2fsprogs
# 系统工具
apt-get -y install tree lshw man at chkconfig vixie-cron crontabs screen dialog ntpdate ntsysv
# 文件处理
yum-y install curl wget lrzsz rsync mlocate tar bzip2 gzip zip unzip dos2unix vim sed patch git
# 编译工具
apt-get -y install gcc gcc-c++ autoconf automake make cmake 
# 网络工具
apt-get -y install net-tools telnet nmap iptraf ifstat tcpdump wireshark
# 性能监控/追踪
apt-get -y install lsof strace iotop sar nmon htop