centos7-docker鏡像
docker pull registry.cn-hangzhou.aliyuncs.com/moensun/centos7
docker pull registry.cn-hangzhou.aliyuncs.com/benpaoz/centos7.4:1.0.3 (使用這個版本)
單用戶模式
(1)在對應的內核條目上,按下e,會進入edit模式,搜尋ro那一行,以linux16開關的,按end鍵到最后,輸入rd.break,再按ctrl+x進入。 注意:需要修改LANG=en_US_UTF-8 , 不然會是亂碼 (2)進入后重新掛載/sysroot/,增加寫權限 mount #查看到只有讀權限 mount -o remount,rw /sysroot/ (3)切換到原始系統下 chroot /sysroot/ touch /.autorelabel #這句是為了selinux生效 passwd root (4)退出單用戶的方法是 先ctrl+d退出,然后reboot
救援模式
(1)修改bios設置dvd_drive首選啟動 (2)選擇 troubleshoot ing 回車啟動 (3) rescue a centos system 救援模式。然后兩次回車 (4)選擇 1) continue 兩次回車 (5) chroot /mnt/sysimage touch /.autorelabel #這句是為了selinux生效,使用這個 重啟不會有檢測 (6) passwd root (7) exit (8) reboot
/etc/rc.local不啟動
chmod a+x /etc/rc.d/rc.local #添加執行權限
設置ip
# systemctl stop NetworkManager --停止服務 # systemctl status NetworkManager --查看狀態,確認為關閉了 # systemctl disable NetworkManager --設置為開機不自動啟動
dhclient ip addr #ip命令是通過 iproute這個包提供的。 #如果要用ifconfig yum install -y net-tools vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 #(包括克隆出來的,只需要更改網卡名和驅動名、ip) TYPE=Ethernet BOOTPROTO=static DEFROUTE=yes PEERDNS=yes PEERROUTES=yes NAME=ens33 #網卡名 DEVICE=ens33 #網卡名 ONBOOT=yes IPADDR=192.168.159.30 NETMASK=255.255.255.0 GATEWAY=192.168.159.2 DNS1=114.114.114.114
# /etc/init.d/network restart --network服務這里默認還是可以使用原來的管理方法 # chkconfig network on
時間同步
方法1: # yum install ntp ntpdate --安裝ntp時間同步相關軟件包 # vim /etc/ntp.conf --確認配置文件里有下列的時間同步源 server 0.rhel.pool.ntp.org iburst server 1.rhel.pool.ntp.org iburst server 2.rhel.pool.ntp.org iburst server 3.rhel.pool.ntp.org iburst # systemctl enable ntpd --設置開機自動啟動ntpd # systemctl start ntpd --立即啟動ntpd服務 # date --確認時間與現在時間一致 # ntpdate 0.rhel.pool.ntp.org --如果還沒有同步成功,你可以用此命令手動同步一下 ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #更改時區為上海 方法2: # yum install xinetd -y # vim /etc/xinet.d/time-dgram disable = no (--yes改為no) # vim /etc/xinetd.d/time-stream disable = no (--yes改為no) # systemctl restart xinetd # systemctl status xinetd # systemctl enable xinetd 方法3: yum install ntpd -y echo "30 * * * * root /usr/sbin/ntpdate ntp.api.bz" >> /etc/crontab
設置主機名和命令參數補全
設置主機名 (1)centos7 hostnamectl set-hostname test1 cat /etc/hostname #用上面的命令會直接修改這個配置文件的內容 hostnamectl status #查看hostname狀態 (2)centos6 hostname test1 cat /etc/sysconfig/network #上面命令不會修改配置文件的內容 命令參數補全 yum install -y bash-completion source /etc/profile
centos7防火牆
若不想使用firewalld,繼續使用iptables,可以停掉firewalld,並且安裝iptables-services包 systemctl stop firewalld systemctl disable firewalld yum install -y iptables-services systemctl enable iptables systemctl start iptables
安裝好centos7后憂化
https://www.cnblogs.com/benjamin77/p/8473129.html 設置hostname 命令補全 sshd加速 關閉selinux firewalld 安裝yum install -y rsync openssl openssl-devel gcc gcc-c++ wget curl unzip lrzsz sysstat ntp man vim bash-completion ntpd screen expect autoconf automake bzip2 bzip2-devel openssh-clients
centos7優化腳本
#!/bin/bash ####centos 7 裝好 優化初始腳本 ##運行此腳本 需要通網絡 用root用戶 #Auther :hanxiaohui #Date :2015-06-18 #User :Set basic environment function FAILED(){ if [ `echo $?` -ne 0 ];then exit fi } #檢查用戶是否為root USER=`whoami` if [ "$USER" != 'root' ];then echo "Please user root" exit fi #優化內核參數: if [ ! -f '/etc/sysctl.conf.bak' ];then \cp /etc/sysctl.conf{,.bak} cat << eof > /etc/sysctl.conf net.ipv4.tcp_syncookies = 1 #表示開啟SYN Cookies。當出現SYN等待隊列溢出時,啟用cookies來處理,可防范少量SYN攻擊,默認為0,表示關閉; net.ipv4.tcp_tw_recycle = 1 #表示開啟TCP連接中TIME-WAIT sockets的快速回收,默認為0,表示關閉; net.ipv4.tcp_tw_reuse = 1 #表示開啟重用。允許將TIME-WAIT sockets重新用於新的TCP連接,默認為0,表示關閉; net.ipv4.tcp_fin_timeout = 3 #修改系統默認的 TIMEOUT 時間。 net.ipv4.tcp_max_tw_buckets = 20000 net.ipv4.ip_forward = 0 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 kernel.sysrq = 0 kernel.core_uses_pid = 1 kernel.msgmnb = 65536 kernel.msgmax = 65536 kernel.shmmax = 68719476736 kernel.shmall = 4294967296 net.ipv4.tcp_max_tw_buckets = 10000 net.ipv4.tcp_sack = 1 net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_rmem = 4096 87380 4194304 net.ipv4.tcp_wmem = 4096 16384 4194304 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.core.netdev_max_backlog = 262144 net.core.somaxconn = 262144 net.ipv4.tcp_max_orphans = 3276800 net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_keepalive_time = 30 net.ipv4.ip_local_port_range = 1024 65000 vm.overcommit_memory = 1 eof fi #刷新系統內核參數: /sbin/sysctl -p #配置PS 環境: if [ -z `egrep -i ps1 /etc/profile |awk '{print $1}'` ];then echo "PS1='\[\e[35;1m\][\u@\h \w \t&&\#]\\$\[\e[m\]'" >> /etc/profile && source /etc/profile fi #做本機的hosts 解析: ETH=`awk '{if($2>0 && NR > 2) print substr($1, 0, index($1, ":") - 1)}' /proc/net/dev |grep -v lo |head -n 1` HOSTS=`egrep -i $(echo $HOSTNAME) /etc/hosts |awk '{print $2}'` if [ -z "$HOSTS" ];then echo $(ip ad |grep "global $ETH" |awk -F "/" '{print $1}' |awk '{print $2}' && echo $HOSTNAME) >> /etc/hosts fi #關閉selinux sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config #禁止使用DNS反向解析: sed -i 's/^#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config #加快登陸速度: sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/sshd_config #重新加載openssh service sshd restart #更改語言為en lang=`grep -i lang /etc/locale.conf |awk -F '"' '{print $2}'` if [ "$LANG" != 'en_US.UTF-8' ];then sed -i "s/${LANG}/en_US.UTF-8/" /etc/locale.conf fi #設置時區 if [ ! -f /etc/localtime.bak ];then cp /etc/localtime{,.bak} yes|cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime fi #安裝必要的維護工具
yum install -y rsync openssl openssl-devel gcc gcc-c++ wget curl unzip lrzsz sysstat ntp man vim bash-completion ntpd screen expect autoconf automake bzip2 bzip2-devel openssh-clients
FAILED #修改國內源和安裝epel源: [ -d /etc/yum.repos.d.bak ] && rm -rf /etc/yum.repos.d.bak cp -a /etc/yum.repos.d{,.bak} rm -rf /etc/yum.repos.d/* wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/Centos-7.repo wget -P /etc/yum.repos.d/ http://mirrors.163.com/.help/CentOS7-Base-163.repo yum clean all yum makecache
sleep 2 yum install -y epel-release wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo yum clean all yum makecache #命令補全 yum install -y bash-completion source /etc/profile #每半個小時從網絡上更新一次系統時間 NTPDATE=`egrep -i ntpdate /etc/crontab |awk -F '/' '{print $4}' |awk '{print $1}'` if [ "$NTPDATE" == "" ];then echo "#每半個小時從網絡上更新一次系統時間" >> /etc/crontab echo "30 * * * * root /usr/sbin/ntpdate ntp.api.bz" >> /etc/crontab fi /usr/sbin/ntpdate ntp.api.bz #或者寫成: #if [ ! -z `egrep -i ntpdate /etc/crontab |awk -F '/' '{print $4}' |awk '{print $1}'` ];then # echo "#每半個小時從網絡上更新一次系統時間" >> /etc/crontab # echo "30 * * * * root /usr/sbin/ntpdate time-a.nist.gov" >> /etc/crontab #fi #配置limit ulimit -SHn ,, ulimit -SHu ,, LIMIT=`egrep -i -v '^#|^$' /etc/security/limits.conf` if [ -z "$LIMIT" ] then cp /etc/security/limits.conf{,.bak} sed -i "49i *\t\thard\tnproc\t\t65535\n*\t\tsoft\tnproc\t\t65535\n*\t\thard\tnofile\t\t\65535\n*\t\tsoft\tnofile\t\t65535\n" /etc/security/limits.conf fi iptables -F ;iptables-save > /opt/sh/iptables.bak systemctl disable firewalld
systemctl disable NetworkManager #systemctl stop firewalld #重啟系統 sync && reboot
centos7配置國內源和epel源
cd /etc/yum.repos.d/
mkdir repo_bak mv *.repo repo_bak/ wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/Centos-7.repo wget -P /etc/yum.repos.d/ http://mirrors.163.com/.help/CentOS7-Base-163.repo yum clean all # 清除系統所有的yum緩存 yum makecache # 生成yum緩存 yum install -y epel-release # epel源安裝成功,比原來多了一個epel.repo和epel-testing.repo文件 wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo # 下載阿里開源鏡像的epel源文件 多了epel-7.repo #再次重新生成yum緩存 yum clean all yum makecache yum repolist enabled #查看系統可用的yum源 yum repolist all #所有的yum源
centos7修改系統語言和設置時區
查看系當前語言包 locale 查看系統擁有語言包 locale -a (zh_CN.UTF-8是簡體中文,如果沒有zh_CN.UTF-8,就安裝語言包,如果存在可以直接設置) 安裝簡體中文語言包 yum install kde-l10n-Chinese 設置為中文 臨時 LANG="zh_CN.UTF-8" #修改為中文 LANG="en_US.UTF-8" #修改為英文 永久 方法(一) vi /etc/locale.conf ##加下面內容到第一行,設置中文 LANG=zh_CN.UTF8 方法(二) localectl set-locale LANG=zh_CN.UTF8 修改時區為亞洲上海 ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
centos7不再有chkconfig工具了,而是使用systemd
使某服務自啟動 systemctl enable httpd.service 不自啟動 systemctl disable httpd.service 查看服務狀態 systemctl status httpd.service 查看所有服務 systemctl list-units --type=service 啟動服務 systemctl start httpd.service 停止服務 systemctl stop httpd.service 重啟服務 systemctl restart httpd.service systemctl daemon-reload #改配置后需要執行這個 啟動腳本路徑
/usr/lib/systemd/system/ 檢查一個服務是否為開機啟動
systemctl is-enabled httpd
列出所有正在運行的、類型為service的Unit
systemctl list-units --type=service
systemctl list-unit-files | grep enabled | grep service #這個好用
查看啟動時的默認Target:
systemctl get-default
設置啟動時的默認 Target: systemctl set-default multi-user.target
切換 Target 時,默認不關閉前一個 Target 啟動的進程,systemctl isolate 命令改變這種行為,關閉前一個 Target 里面所有不屬於后一個 Target 的進程
systemctl isolate multi-user.target
查看運行級別:
runlevel
查看一個service屬於哪個target,需要cat 具體的service文件:
cat /usr/lib/systemd/system/sshd.service #看里面[install]部分
systemd - unit
systemd管理服務的機制的特點: 支持服務並列啟動,不用順序啟動,從而開機時間縮短 支持自動檢測服務依賴的服務 Systemd 可以管理所有系統資源。不同的資源統稱為 Unit(單位)。 Unit 一共分成12種類型 Service unit:系統服務 Target unit:多個 Unit 構成的一個組 Device Unit:硬件設備 Mount Unit:文件系統的掛載點 Automount Unit:自動掛載點 Path Unit:文件或路徑 Scope Unit:不是由 Systemd 啟動的外部進程 Slice Unit:進程組 Snapshot Unit:Systemd 快照,可以切回某個快照 Socket Unit:進程間通信的 socket Swap Unit:swap 文件 Timer Unit:定時器 systemd - unit相關命令: 列出正在運行的 Unit systemctl list-units 列出所有Unit,包括沒有找到配置文件的或者啟動失敗的 systemctl list-units --all 列出所有沒有運行的 Unit systemctl list-units --all --state=inactive 列出所有加載失敗的 Unit systemctl list-units --failed 列出所有正在運行的、類型為 service 的 Unit systemctl list-units --type=service 顯示某個 Unit 是否正在運行 systemctl is-active application.service 顯示某個 Unit 是否處於啟動失敗狀態 systemctl is-failed application.service 顯示某個 Unit 服務是否建立了啟動鏈接 systemctl is-enabled application.service
systemd target啟動級別
centos7里不再有0-6啟動級別,而是4個target graphical.targe 多人模式,支持圖形和命令行兩種登陸,對應之前的3,5級別 multi-user.target 多人模式,只支持命令行登錄,對應之前的3級別 rescue.target 單人模式,對應之前的1級別 emergency.target 單人模式,不過系統進入后根目錄是只讀的。 target類似於centos6里面的啟動級別,但target支持多個target同時啟動。target其實是多個unit的組合,系統啟動說白了就是啟動多個unit,為了管理方便,就使用target來管理這些unit。 查看當前系統的所有 Target systemctl list-unit-files --type=target 查看一個 Target 包含的所有 Unit systemctl list-dependencies multi-user.target 查看啟動時的默認 Target systemctl get-default 設置啟動時的默認 Target systemctl set-default multi-user.target 切換 Target 時,默認不關閉前一個 Target 啟動的進程, systemctl isolate 命令改變這種行為,關閉前一個 Target 里面所有不屬於后一個 Target 的進程 systemctl isolate multi-user.target 查看運行級別 runlevel
systemd unit 關聯target
主配置文件 /etc/systemd/system.conf 開機會先加載 /etc/systemd/system/default.target 所有的service和target在 /usr/lib/systemd/system/ ls -l /etc/systemd/system/defaut.target 是一個軟連接,軟連接到了/usr/lib/systemd/system/multi-user.target 它會加載/usr/lib/systemd/system/multi-user.target.wants下面的service 查看一個service屬於哪個target,需要cat 具體的service文件 cat /usr/lib/systemd/system/sshd.service 看里面[install]部分