相关代码

curl命令-网站如果3次不是200或301则报警

  1. curl -o /dev/null -s -w "%{http_code}" baidu.com
  2. -k/--insecure 允许不使用证书到SSL站点
  3. -H/--header 自定义头信息传递给服务器
  4. -I/--head 只显示请求头信息
  5. -w/--write-out [format] 什么输出完成后
  6. -s/--silent 静默模式。不输出任何东西
  7. -o/--output 把输出写到该文件中

linux正则

参考: http://blog.csdn.net/Hello_Hwc/article/details/40017833

  • 基本
  1. . 匹配任何单个字符
  2. * 前面出现0个或者多个
  3. ^ 以..开始
  4. $ 以..结束
  • 举个例子
  1. china : 匹配此行中任意位置有china字符的行
  2. ^china : 匹配此以china开关的行
  3. china$ : 匹配以china结尾的行
  4. ^china$ : 匹配仅有china五个字符的行
  5. [Cc]hina : 匹配含有Chinachina的行
  6. Ch.na : 匹配包含Ch两字母并且其后紧跟一个任意字符之后又有na两个字符的行
  7. Ch.*na : 匹配一行中含Ch字符,并且其后跟0个或者多个字符,再继续跟na两字符
  • 扩展正则
  1. ? : 匹配前面正则表达式的零个或一个扩展
  2. + : 匹配前面正则表达式的一个或多个扩展
  3. {n,m}: 前面出现1个或2个或3
  4. | : 匹配|符号前或后的正则表达式
  5. ( ) : 匹配方括号括起来的正则表达式群

grep

  • 参数
  1. -n, --line-number
  2. -i, --ignore-case 不区分大小写
  3. -r, --recursive 按照目录
  4. -o, --only-matching 只显示匹配行中匹配正则表达式的那部分
  5. -v, --invert-match 排除
  6. -c, --count 统计url出现次数
  7. grep -nr
  8. grep -oP
  • 过滤ip
  1. 192.168.100.100
  2. ifconfig|grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}"
  • 过滤邮箱
  1. cat >>tmp.txt<<EOF
  2. iher-_@qq.com
  3. hello
  4. EOF
  5. cat tmp.txt|grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z]+)+"
  • 统计baidu关键字的url在这个大文件中出现的次数
  1. $ cat >file.txt<<EOF
  2. wtmp begins Mon Feb 24 14:26:08 2014
  3. 192.168.0.1
  4. 162.12.0.123
  5. "123"
  6. 123""123
  7. njuhwc@163.com
  8. njuhwc@gmil.com 123
  9. www.baidu.com
  10. tieba.baidu.com
  11. www.google.com
  12. www.baidu.com/search/index
  13. EOF
  14. grep -cn ".*baidu.com.*" file.txt
  15. 3

bash自动补全

  1. yum install bash-com* -y
  2. 我在dokcer命令tab可以补全了

nginx json日志格式标准版

参考: https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/configmap.md

  1. log-format-upstream: '{ "time": "$time_iso8601", "remote_addr": "$proxy_protocol_addr",
  2. "x-forward-for": "$proxy_add_x_forwarded_for", "request_id": "$request_id", "remote_user":
  3. "$remote_user", "bytes_sent": $bytes_sent, "request_time": $request_time, "status":
  4. $status, "vhost": "$host", "request_proto": "$server_protocol", "path": "$uri",
  5. "request_query": "$args", "request_length": $request_length, "duration": $request_time,
  6. "method": "$request_method", "http_referrer": "$http_referer", "http_user_agent":
  7. "$http_user_agent" }'

elk启动

  1. nohup /bin/su - elk -c "/usr/local/elasticsearch/bin/elasticsearch" > /data/es/es-start.log 2>&1 &
  2. nohup /bin/su - elk -c "/usr/local/kibana/bin/kibana" > /data/es/kibana-start.log 2>&1 &
  3. nohup "/usr/local/logstash/bin/logstash -f /data/es/conf/logstash/logstash.conf" > /data/es/logstash-start.log 2>&1 &
  4. - logstash
  5. /data/home/user00/logstash/bin/logstash -f /data/home/user00/logstash/conf/index.conf -l /data/home/user00/logstash/logs/logstash.log &
  6. /usr/local/logstash/bin/logstash -f logstash.yaml --config.reload.automatic
  7. curl -XDELETE http://192.168.100.204:9200/.monitoring-kibana-6-2017.10.23
  8. 健康:
  9. http://192.168.100.204:9200/_cat/health?v
  10. 节点:
  11. http://192.168.100.204:9200/_cat/nodes?v
  12. 查看index:
  13. http://192.168.100.204:9200/_cat/indices?v

修改网卡名字

  1. vim /etc/udev/rules.d/70-persistent-net.rules
  2. vim /etc/sysconfig/network-scripts/ifcfg-eth0

sshfs挂载(实现nfs效果)

  • 仅需客户端配置(已做客户端sshkey无密访问服务端)
  1. yum install -y sshfs
  2. 挂载
  3. sshfs -o allow_other,transform_symlinks root@192.168.14.133:/data /data
  4. 卸载
  5. fusermount -u /data

参考: https://www.91yun.co/archives/8731

  1. 我在logging模块里看到的这个注释
  2. #---------------------------------------------------------------------------
  3. # Configuration classes and functions
  4. #---------------------------------------------------------------------------

python搜路径

  1. 起因是有人问怎么把函数全局化,不用import即可随处调用
  2. os.getcwd() #当前py所在目录
  3. b.__file__ #这个模块的路径
  4. os.__module__ #这个函数在哪个模块

参考:http://blog.csdn.net/l_b_yuan/article/details/52260646

  1. os.path.abspath(path) #返回绝对路径
  2. os.path.split(path #将path分割成目录和文件名二元组返回
  3. os.path.dirname(path) #返回path的目录。其实就是os.path.split(path)的第一个元素
  4. os.path.basename(path) #返回path最后的文件名
  5. os.path.exists(path) #如果path存在,返回True;如果path不存在,返回False
  6. os.path.isabs(path) #如果path是绝对路径,返回True
  7. os.path.isfile(path) #如果path是一个存在的文件,返回True。否则返回False
  8. os.path.isdir(path) #如果path是一个存在的目录,则返回True。否则返回False
  9. os.path.getatime(path) #返回path所指向的文件或者目录的最后存取时间
  10. os.path.getmtime(path) #返回path所指向的文件或者目录的最后修改时间
  11. s.path.join(path1[, path2[, ...]]) #将多个路径组合后返回,第一个绝对路径之前的参数将被忽略。
  12. >>> os.path.join('c:\\', 'csv', 'test.csv')
  13. 'c:\\csv\\test.csv'
  14. >>> os.path.join('windows\temp', 'c:\\', 'csv', 'test.csv')
  15. 'c:\\csv\\test.csv'
  16. >>> os.path.join('/home/aa','/home/aa/bb','/home/aa/bb/c')
  17. '/home/aa/bb/c'

python env 和 vscode配置

  1. pip install virtualenv
  2. pip install virtualenvwrapper
  3. pip install virtualenvwrapper-win
  4. mkvirtualenv --python==C:\Python27\python.exe py27env
  5. exit
  6. mkvirtualenv --python==C:\Python34\python.exe py34env
  7. workon
  8. {
  9. "workbench.colorTheme": "Solarized Light",
  10. "window.zoomLevel": 1,
  11. "window.menuBarVisibility": "default",
  12. "editor.wordWrap": "on",
  13. "editor.fontSize": 16,
  14. "files.autoSave": "afterDelay",
  15. "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
  16. "editor.rulers": [80,120]
  17. }

env配置文件

  1. ~/.bash_profile:用户每次登录时执行
  2. ~/.bashrc:每次进入新的Bash环境时执行
  3. ~/.bash_logout:用户每次退出登录时执行

sedmail发邮件配置

  1. yum install sendmail -y
  2. cat >>/etc/mail.rc<<EOF
  3. set from=xxx@tt.com
  4. set smtp=smtp.exmail.qq.com
  5. set smtp-auth-user=xxx@tt.com
  6. set smtp-auth-password=123456
  7. set smtp-auth=login
  8. EOF
  9. source /etc/mail.rc
  • 发消息
  1. echo "test"| mail -s "邮件标题" iher@foxmail.com
  • 发文件
  1. mail -s "邮件标题" iher@foxmail.com < /etc/passwd
  • 发附件
  1. mail -s "邮件标题" -a /var/log/messages iher@Foxmail.com < /etc/passwd
  • 邮件相关目录
  1. C6 postfix /var/spool/postfix/maildrop
  2. C5 sedmail /var/spool/clientmqueue

注: centos6.5已经不自动安装sendmail了所以没必要走这一步优化

  • 写脚本自动清理邮箱
  1. mkdir -p /server/scripts
  2. cat /root/shell/spool_clean.sh
  3. #!/bin/sh
  4. find/var/spool/clientmqueue/-type f -mtime +30|xargs rm-f
  1. echo '*/30 * * * * /bin/sh /server/scripts/spool_clean.sh >/dev/null 2>&1'>>/var/spool/cron/root

locale字符集-面试

  • 查本地支持的所有字符集
  1. # locale -a
  • 查当前使用的字符集
  1. locale #调取了/etc/sysconfig/i18n
  • 系统默认字符集:
  1. export LANG='zh_CN.UTF-8'

监控网卡实时流量

  • 监控网卡流量历史流量
  1. yum install sysstat
  2. sar -n DEV 1 5 #1s监控1次,共监控5次.
  3. sar -n DEV (-n network)
  1. watch more /proc/net/dev

find干掉超过10天的

  • mtime 10天内 10天外
  1. find . -mtime +10 -exec rm -rf {} \;
  2. find . -mtime +10|xargs rm -f

测试udp端口是否通-面试

  1. $ nc -vuz 192.168.6.6 53
  2. Connection to 192.168.6.6 53 port [udp/domain] succeeded!

实际使用时可以只用-u参数,-u代表udp协议 ,-v代表详细模式,-z代表只监测端口不发送数据。

使用nc+tar传文件

  • client发交互式到服务器的console
  1. nc -l -u 8021 --server #可以配置tcpdump -i eth0 port 8021 -nnv抓包
  2. nc -u 192.168.6.52 8021 --client #交互式发送消息
  • client发文件到服务端console
  1. server: nc -l -u 8021
  2. client: nc -u 192.168.6.52 8021 < /etc/hosts
  • tar+nc传文件
  1. server tar -cf - /home/database | nc -l 5677 #将/home/database文件
  2. client nc 192.168.6.52 5677 | tar -xf - #传到client的当前目录

生成密码:

  1. openssl rand -hex 8
  1. $mkpasswd -l 16 -s 2
  2. 3Hte^bd-pkylSbf7
  1. echo "ansible"|passwd --stdin ansible #centos7改用户密码

fstab挂载

  • fstab挂载硬盘
  1. cat /etc/fstab
  2. 需挂载的设备 挂载点 fs类型 参数 备份 检查
  3. /dev/mapper/centos-data /data xfs defaults 0 0
  • nfs挂载(centos7放fstab)
  1. 192.168.8.68:/data/backup/no75/confluence/data /data/confluence/ nfs defaults 0 0
  • nfs挂载(centos6放/etc/rc.local里即可)
  1. /usr/bin/mount -t nfs 192.168.8.68:/data/owncloud /data/owncloud-192.168.8.68
  • nfs服务端设置:
  1. /data/backup/no75/confluence/data 192.168.8.0/24(rw,sync,no_root_squash)
  • (磁盘扩容)关于tmpfs空间满,会影响其中的服务使用吗
  1. Filesystem Size Used Avail Use% Mounted on
  2. /dev/sda1 32G 1.3G 29G 5% /
  3. tmpfs 16G 16G 0 100% /dev/shm
  4. mount -o remount,size=18G /dev/shm
  • 只读mount
  1. Mount the file system and make it writeable
  2. mount -uw /
  3. Make the filesystem read only again.
  4. mount -ur /

date命令小结

  • 前一天日期
  1. date +%Y-%m-%d~%H-%M-%S -d "-1 day"
  1. date "+%Y-%m-%d %H-%M-%S" -d "-1 day"
  • 压缩带日期
  1. tar zcvf etc_$(date +%F -d "-1 day").tar.gz /etc/

系统时间优化

  • 时区校准
  1. rm -rf /etc/localtime && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && ntpdate ntp1.aliyun.com
  • 设置同步时间
  1. /user/sbin/ntpdate ntp1.aliyun.com
  2. echo '*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2 >&1' >>/var/spool/cron/root
  • 手动修改时间
  1. date -s "2016/06/11 22:50"

过滤网卡ip

  1. ifconfig eth0|grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}"|sed -n '1p'
  2. ifconfig|sed -n '2p'|sed -r 's#^.*addr:(.*) Bcast.*$#\1#g'
  3. ifconfig|sed -n '2p'|awk -F':' '{print $2}'|awk '{print $1}'

回车擦除^H

  1. echo "stty erase ^H" >>/root/.bash_profile
  2. source /root/.bash_profile

centos7安装nslookup ifconfig

How to install dig, host, and nslookup – bind-utils on CentOS:

  1. yum install bind-utils -y [c6使用nslookup]
  2. yum install net-tools -y [c7使用ifconfig]

selinux优化

  1. setenforce 0
  2. sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
  3. getenforce
  4. /etc/init.d/iptables stop

文件描述符优化

  1. ulimit -SHn 65535
  2. echo '* - nofile 65536' >>/etc/security/limits.conf
  3. echo "* soft nproc 65535" >>/etc/security/limits.conf
  4. echo "* hard nproc 65535" >>/etc/security/limits.conf
  5. echo "* soft nofile 65535" >>/etc/security/limits.conf
  6. echo "* hard nofile 65535" >>/etc/security/limits.conf

清除系统版本banner

  1. > /etc/issuse
  2. >/etc/redhat-release

添加普通用户并进行sudo授权管理

  1. $ useradd sunsky
  2. $ echo "123456"|passwd --stdin sunsky&&history c
  3. $ visudo # 99gg
  4. root ALL=(ALL) ALL #此行下,添加如下内容
  5. sunsky ALL=(ALL) ALL
  6. lanny ALL=(ALL) ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom #仅允许他执行这些命令

ssh慢优化

  1. \cp /etc/ssh/sshd_config /etc/ssh/sshd_config.ori
  2. sed -i 's#\#UseDNS yes#UseDNS no#g' /etc/ssh/sshd_config
  3. sed -i 's#GSSAPIAuthentication yes#GSSAPIAuthentication no#g' /etc/ssh/sshd_config
  4. /etc/init.d/sshd restart
  5. Port 22345
  6. PermitRootLogin no
  7. PermitEmptyPasswords no
  8. UseDNS no
  9. ListenAddress 192.168.138.24
  10. GSSAPIAuthentication no

crt设置超时

  1. export TMOUT=10
  2. echo "export TMOUT=10" >>/etc/profile
  3. source /etc/profile

vim安装优化

  1. yum -y install vim-enhanced
  2. cat >>/etc/vimrc<<a
  3. set nu
  4. set cursorline
  5. set nobackup
  6. set ruler
  7. set autoindent
  8. set vb t_vb=
  9. set ts=4
  10. set expandtab
  11. a
  12. . /etc/vimrc

rsync安装配置

  • rsync server配置(rpm -qa|grep rsync):
  1. cat /usr/local/rsync/rsync.conf
  2. uid = root
  3. gid = root
  4. use chroot = no
  5. max connections = 10
  6. strict modes = yes
  7. pid file = /var/run/rsyncd.pid
  8. lock file = /var/run/rsync.lock
  9. log file = /var/log/rsyncd.log
  10. [web]
  11. path = /code/pp100web/target/ROOT
  12. comment = web file
  13. ignore errors
  14. read only = no
  15. write only = no
  16. hosts allow = 192.168.14.132
  17. list = false
  18. uid = root
  19. gid = root
  20. auth users = webuser
  21. secrets file = /usr/local/rsync/rsync.passwd
  • 重启rsync
  1. kill -HUP `cat /var/run/rsyncd.pid`
  2. /usr/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf
  3. ps -ef|grep rsync
  • 配置允许同步的的客户端
  1. vim /usr/local/rsync/rsync.conf
  2. hosts allow = 192.168.14.132,192.168.14.133

注意:密码文件统一600,且普通用户为谁,属主即为谁.

java环境变量(附带tomcat)

  1. export JAVA_HOME=/usr/local/jdk
  2. export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
  3. export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
  4. export TOMCAT_HOME=/usr/local/tomcat
  5. export CATALINA_BASE="/data/tomcat"
  6. export PATH=/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/jdk1.7.0_45/bin:/root/bin:/usr/local/jdk1.7.0_45/bin:/root/bin

换源&安装常用软件

  1. wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
  2. wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
  3. yum clean all
  4. yum makecache
  5. yum install lrzsz ntpdate sysstat dos2unix wget telnet tree -y

添加定时任务

  1. crontab -l
  2. */5 * * * * /usr/sbin/ntpdate times.windows.com >/dev/null 2>&1

优化退格键

  1. stty erase "^H" #追加到/etc/profile

优化history:

  1. export HISTTIMEFORMAT="%F %T `whoami` "
  2. echo "export HISTTIMEFORMAT="%F %T `whoami` "" >> /etc/profile

优化message:格式

  1. export PROMPT_COMMAND='{ msg=$(history 1 | { read x y; echo $y; });logger "[euid=$(whoami)]":$(who am i):[`pwd`]"$msg";}'

过滤日志

  1. cat /etc/salt/master |grep -v "#" | sed '/^$/d'
  2. grep -nir
  3. -i 不区分大小写
  4. -n 显示行号
  5. -r 查找目录, grep -r 'xx' .

kill服务

  1. /usr/bin/killall -HUP syslogd
  2. /bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || :

禁止ping

  1. echo "net.ipv4.icmp_echo_ignore_all=1">>/etc/sysctl.conf
  2. tail -1 /etc/sysctl.conf
  3. sysctl -p
  4. echo 1 > /proc/sys/net/ipv4/ip_forward #这样好处可以tab
  1. sysctl -w net.ipv4.ip_forward=1 #好像没写到/etc/sysctl.conf里

sed 在某行(指具体行号)前或后加一行内容

  1. sed -i 'N;4addpdf' a.txt
  2. sed -i 'N;4ieepdf' a.txt
  3. sed -i 'N;4a44444444444444444444444444testt' 1.log在第四行后加一行
  4. http://www.361way.com/sed-process-lines/2263.html

关闭bell:[需reboot]

  1. sed -i 's#^\#set bell-style none#set bell-style none#g' /etc/inputrc
  2. echo "modprobe -r pcspkr" > /etc/modprobe.d/blacklist

关掉ctrl+alt+delete关机

  1. \cp /etc/init/control-alt-delete.conf /etc/init/control-alt-delete.conf.bak
  2. sed -i 's#exec /sbin/shutdown -r now "Control-Alt-Deletepressed"#\#exec /sbin/shutdown -r now "Control-Alt-Deletepressed"#g'
  1. yum groupinstall base -y
  2. yum groupinstall core -y
  3. yum groupinstall development libs -y
  4. yum groupinstall development tools -y

echo高亮显示

  1. echo -e "\033[32m crontab has been added successfully \033[0m"

nfs安装配置

  • 服务端&客户端
  1. yum install nfs-utils rpcbind -y
  • 服务端:
  1. /etc/init.d/rpcbind start
  2. ps -ef |grep rpc
  3. /etc/init.d/rpcbind status
  4. rpcinfo -p localhost
  • 服务端配置共享目录
  1. echo "/data 10.0.0.0/24(rw,sync,no_root_squash)" >> /etc/exports
  2. chkconfig rpcbind on
  3. chkconfig nfs on
  • 客户端挂载
  1. /etc/init.d/rpcbind start
  2. chkconfig rpcbind on
  3. showmount -e 10.1.1.10
  4. mount -t nfs 10.1.1.10:data /mnt
  5. 写到/etc/rc.local

nginx编译安装

  • 1.安装依赖
  1. yum install pcre pcre-devel openssl openssl-devel y
  • 2.添加nginx用户
  1. useradd -s /sbin/nologin -M nginx
  • 3.编译安装
  1. ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.6.2 --with-http_stub_status_module --with-http_ssl_module
  2. make && make install
  3. echo $?
  4. ln -s /usr/local/nginx-1.6.2 /usr/local/nginx
  • 4.检查nginx.conf语法
  1. /usr/local/sbin/nginx # -t检查配置文件语法
  2. /usr/local/nginx/sbin/nginx # 启动
  • 5.添加nginx服务到PATH
  1. echo PATH=/application/nginx/sbin/:$PATH >> /etc/profile
  2. source /etc/profile
  3. netstat -ntulp |grep nginx
  4. lsof -i:80
  5. curl 192.168.14.151
  6. nginx -s stop
  7. nginx -s reload
  • 7.nginx反代配置nignx.conf
  1. worker_processes auto;
  2. events {
  3. multi_accept on;
  4. use epoll;
  5. worker_connections 51200;
  6. }
  7. error_log stderr notice;
  8. worker_rlimit_nofile 65535;
  9. http {
  10. include mime.types;
  11. default_type application/octet-stream;
  12. server_info off;
  13. server_tag off;
  14. server_tokens off;
  15. server_name_in_redirect off;
  16. client_max_body_size 20m;
  17. client_header_buffer_size 16k;
  18. large_client_header_buffers 4 16k;
  19. sendfile on;
  20. tcp_nopush on;
  21. keepalive_timeout 65;
  22. server_tokens on;
  23. gzip on;
  24. gzip_min_length 1k;
  25. gzip_buffers 4 16k;
  26. gzip_proxied any;
  27. gzip_http_version 1.1;
  28. gzip_comp_level 3;
  29. gzip_types text/plain application/x-javascript text/css application/xml;
  30. gzip_vary on;
  31. upstream owncloud {
  32. server 127.0.0.1:8000;
  33. }
  34. upstream confluence {
  35. server 127.0.0.1:8090;
  36. }
  37. server {
  38. listen 80;
  39. server_name owncloud.maotai.org;
  40. location / {
  41. proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;
  42. proxy_pass http://owncloud;
  43. proxy_set_header Host $host;
  44. proxy_set_header X-Real-IP $remote_addr;
  45. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  46. }
  47. }
  48. server {
  49. listen 80;
  50. server_name confluence.maotai.org;
  51. location / {
  52. proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;
  53. proxy_pass http://confluence;
  54. proxy_set_header Host $host;
  55. proxy_set_header X-Real-IP $remote_addr;
  56. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  57. }
  58. }
  59. server {
  60. listen 80;
  61. server_name status-no189.maotai.org;
  62. location /nginx_status {
  63. stub_status on;
  64. access_log off;
  65. }
  66. }
  67. }

徒手生成10M的文件

参考: https://linux.cn/article-4126-1.html

  1. head -c 10M < /dev/urandom > /var/log/log-file
  2. # 生成随机字符串
  3. cat /dev/urandom |tr -dc [:alnum:] |head -c 8

logrotate nginx日志切割

每天3点才切割问题: 参考: http://www.voidcn.com/article/p-tpivuevp-gn.html

  1. cat > /etc/logrotate.d/nginx
  2. /usr/local/nginx/logs/*.log {
  3. daily
  4. missingok
  5. rotate 7
  6. dateext
  7. compress
  8. delaycompress
  9. notifempty
  10. sharedscripts
  11. postrotate
  12. if [ -f /usr/local/nginx/logs/nginx.pid ]; then
  13. kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
  14. fi
  15. endscript
  16. }

网卡配置

  1. DEVICE=eth0
  2. TYPE=Ethernet
  3. ONBOOT=yes
  4. NM_CONTROLLED=yes
  5. BOOTPROTO=static
  6. IPADDR=192.168.6.28
  7. NETMASK=255.255.255.0
  8. GATEWAY=192.168.6.1

修改console提示符

  • Ubuntu的promote
  1. export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$"
  • centos的promote
  1. export PS1="[\u@\h \W]\$"

yum安装lamp

  • yum安装lamp:
  1. yum install -y httpd php php-cli php-common php-pdo php-gd
  2. yum install -y httpd php php-cli php-common php-pdo php-gd mysql mysql-server php-mysql
  3. yum install -y httpd php php-ldap php-gd
  • php配置:
  1. vim /etc/php.ini
  2. 729 post_max_size = 16M
  3. 946 date.timezone = PRC #(中华人民共和国)

批量创建用户脚本

  1. cat adduser.sh
  2. #!/bin/bash
  3. # Add system user
  4. for ldap in {1..5};do
  5. if id user${ldap} &> /dev/null;then
  6. echo "System account already exists"
  7. else
  8. adduser user${ldap}
  9. echo user${ldap} | passwd --stdin user${ldap} &> /dev/null
  10. echo "user${ldap} system add finish"
  11. fi
  12. done
  13. # chmod +x adduser.sh
  14. # ./adduser.sh
  15. # id user1
  16. uid=502(user1) gid=502(user1) groups=502(user1)
  1. useradd test -u 6000 -g 6000 -s /sbin/nologin -M -d /dev/null

[shell] $*和$@的区别

  • 单独的 $*和$@ 没区别
  • "$*"和"$@"区别如下
  1. [root@node1 ~]# cat test.sh
  2. #!/bin/sh
  3. for i in "$*";do
  4. echo $i
  5. done
  6. [root@node1 ~]# sh test.sh 1 2 3 4
  7. 1 2 3 4
  8. [root@node1 ~]# cat test.sh
  9. #!/bin/sh
  10. for i in "$@";do
  11. echo $i
  12. done
  13. [root@node1 ~]# sh test.sh 1 2 3 4 5
  14. 1
  15. 2
  16. 3
  17. 4
  18. 5

[shell] linux exec与重定向

[shell] shell学习之变量

[shell] 定义列表

  • 使用小括号为数组赋值

    a=(1 2 3)注意: 默认空格隔开

  • 为数组b赋值-方法1

  1. $ b=(bbs www http ftp)
  2. $ echo ${b[*]}
  3. bbs www http ftp
  • 打印出第一个和第三个数据项
  1. $ echo ${b[0]};echo '*******';echo ${b[2]}
  2. bbs
  3. *******
  4. http

注: 记住是小括号,不是大括号

  • 为数组b赋值-方法2
  1. name=(
  2. alice
  3. bob
  4. cristin
  5. danny
  6. )
  7. for i in "${!name[@]}";do
  8. echo ${name[$i]}
  9. done
  • 取得数组元素的个数-方法1
  1. length=${#array_name[@]}
  • 取得数组元素的个数-方法2
  1. length=${#array_name[*]}
  • 取得数组单个元素的长度
  1. lengthn=${#array_name[n]}

优化小结:

一清: 定时清理日志/var/spool/clientsqueue

一精: 精简开机启动服务

一增: 增大文件描述符

两优: linux内核参数的优化、yum源优化

四设:设置系统的字符集、设置ssh登录限制、设置开机的提示信息与内核信息、设置block的大小

七其他:文件系统优化、sync数据同步写入磁盘、不更新时间戳、锁定系统关键文件、时间同步、sudo集权管理、关闭防火墙和selinux

centos一键优化脚本:

本文 centos 6.5 优化 的项有18处:

  • 1、centos6.5最小化安装后启动网卡
  • 2、ifconfig查询IP进行SSH链接
  • 3、更新系统源并且升级系统
  • 4、系统时间更新和设定定时任
  • 5、修改ip地址、网关、主机名、DNS
  • 6、关闭selinux,清空iptables
  • 7、创建普通用户并进行sudo授权管理
  • 8、修改SSH端口号和屏蔽root账号远程登陆
  • 9、锁定关键文件系统(禁止非授权用户获得权限)
  • 10、精简开机自启动服务
  • 11、调整系统文件描述符大小
  • 12、设置系统字符集
  • 13、清理登陆的时候显示的系统及内核版本
  • 14、内核参数优化
  • 15、定时清理/var/spool/clientmqueue
  • 16、删除不必要的系统用户和群组
  • 17、关闭重启ctl-alt-delete组合键
  • 18、设置一些全局变量

优化内核:

  1. \cp /etc/sysctl.conf /etc/sysctl.conf.$(date +%F)
  2. cat >>/etc/sysctl.conf<<EOF
  3. net.ipv4.tcp_fin_timeout = 2
  4. net.ipv4.tcp_tw_reuse = 1
  5. net.ipv4.tcp_tw_recycle = 1
  6. net.ipv4.tcp_syncookies = 1
  7. net.ipv4.tcp_keepalive_time = 600
  8. net.ipv4.ip_local_port_range = 4000 65000
  9. net.ipv4.tcp_max_syn_backlog = 16384
  10. net.ipv4.tcp_max_tw_buckets = 36000
  11. net.ipv4.route.gc_timeout = 100
  12. net.ipv4.tcp_syn_retries = 1
  13. net.ipv4.tcp_synack_retries = 1
  14. net.core.somaxconn = 16384
  15. net.core.netdev_max_backlog = 16384
  16. net.ipv4.tcp_max_orphans = 16384
  17. net.netfilter.nf_conntrack_max = 25000000
  18. net.netfilter.nf_conntrack_tcp_timeout_established = 180
  19. net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
  20. net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
  21. net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
  22. EOF
  23. sysctl -p

注: 以下参数是对centos6.x的iptables防火墙的优化,防火墙不开会有提示,可以忽略不理。

如果是centos5.X需要吧netfilter.nf_conntrack替换成ipv4.netfilter.ip

centos5.X为net.ipv4.ip_conntrack_max = 25000000

  1. net.nf_conntrack_max = 25000000
  2. net.netfilter.nf_conntrack_max = 25000000
  3. net.netfilter.nf_conntrack_tcp_timeout_established = 180
  4. net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
  5. net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
  6. net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120

linux更换内核

参考

CentOS6 内核更换为: 2.6.32-504.3.3.el6.x86_64

  1. rpm -ivh http://soft.91yun.org/ISO/Linux/CentOS/kernel/kernel-firmware-2.6.32-504.3.3.el6.noarch.rpm
  2. rpm -ivh http://soft.91yun.org/ISO/Linux/CentOS/kernel/kernel-2.6.32-504.3.3.el6.x86_64.rpm --force

CentOS7 内核更换为: 3.10.0-229.1.2.el7.x86_64

  1. rpm -ivh http://soft.91yun.org/ISO/Linux/CentOS/kernel/kernel-3.10.0-229.1.2.el7.x86_64.rpm --force
查看是否成功
  1. reboot
  2. uname -r
  3. rpm -qa | grep kernel
  4. 百度:
  5. site:centos.org 你需要的内核
  6. site:centos.org kernel-2.6.32-504.3.3.el6.x86_64.rpm

[svc]linux常用手头命令-md版-2017年11月12日 12:31:56的更多相关文章

  1. WPS 表格筛选两列相同数据-完美-2017年11月1日更新

    应用: 1.选出A列中的数据是否在B列中出现过: 2.筛选出某一批序号在一个表格里面的位置(整批找出) 3.其实还有其他很多应用,难描述出来... ... A列中有几百的名字,本人想帅选出B列中的名字 ...

  2. Python 爬虫练习(二)爬取补天公益SRC厂商域名URL (2017年11月22日)

    介绍下: 补天是国内知名的漏洞响应平台,旨在企业和白帽子共赢. 白帽子在这里提交厂商漏洞,获得库币和荣誉,厂商从这里发布众测.获取漏洞报告和修复建议. 在2017年3月份之前,补天的厂商域名URL是非 ...

  3. 了解ASP.NET Core 依赖注入,看这篇就够了 于2017年11月6日由jesseliu发布

    DI在.NET Core里面被提到了一个非常重要的位置, 这篇文章主要再给大家普及一下关于依赖注入的概念,身边有工作六七年的同事还个东西搞不清楚.另外再介绍一下.NET  Core的DI实现以及对实例 ...

  4. 2017年11月3日 VS三大类&数组&VS的冒泡排序&集合&泛型集合

    三大类 共分为两个大类: 基本数据型&引用类型 基本数据型---值类型---整型---常用的整型: Int , 长整型:  Long, 小整型: byle, 中整型 short --浮点型 - ...

  5. 2017年11月1日 初学者易上手的SSH-spring 01控制反转(IOC)

    这章开始学习SSH中最后的一个框架spring.Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用. 首先就来学习一下I ...

  6. 2017年11月23日**科技面试java工程师面试总结

    从整个面试看了,表现并不理想,有很多不足. 1.关于手机页面闪现问题的处理.从原理上观察,页面闪现是因为异步请求导致的.在进入页面中首先会加载默认的东西,此时也会访问数据库端,会用ajax判断是否满足 ...

  7. 2017年11月8日最新仿互站导航t5友价商城-9套模板首页都增加微信登陆

    今天测试效果如下,直接看图吧,入口在下方,点击图片直达 把9套餐模板都添加了微信首页登陆,仿互站的导航,操作比互站还要方便,官方一直对https 支持不太友好,索性把所有的https bug都修复了, ...

  8. 2017年11月30日 C#TreeNode递归&邮箱验证&新用户窗体

    TreeNode递归 递归:自己调用自己一层一层的把数据找出来 TreeNode:可以创建多个节点 private void button1_Click(object sender, EventArg ...

  9. 2017年11月28日 C#进程和线程

    进程 需要放using System.Diagnostics;才可以用进程 用时的方法名为Process 用两个按钮一个为选择文件夹一个为打开可以打开系统内的进程. 注意:打开时一定要用进程名 Pro ...

随机推荐

  1. FORM - FILE.EXPORT 导出功能

    对于数据块项,是不用添加任何触发器就可以使用导出功能的. 如果不能使用该功能,可能是由于在需要使用导出功能的块上添加了block级触发器when-new-item-instance,但执行层次为&qu ...

  2. 【fastJSON】利用fastJSON处理循环引用的问题

    下载fastJSON jar   com.alibaba.fastjson 第一种:[写死的] 将需要序列化的字段传递进去,得到结果 //需要序列化的实体+字段 SimplePropertyPreFi ...

  3. Bower管理依赖库初体验

    比如一开始我用了jquery-1.10.2.min.js,后来要用bootstrap,但bootstrap依赖的确实2.0.3版本的jquery,那又要下载一个去替换原来的,这样的事情发生多了就会觉得 ...

  4. mac远程链接 windows

    https://bbs.feng.com/read-htm-tid-10516042.html 一.利用电脑系统自带的远程(喜欢懒人版的方法,或者小白用户,可以跳过这个方法看下一个) 1.打开mac, ...

  5. Spring Security实现后台管理员登录(一)

    一.实现功能 二.数据表设计 为了测试方便,这里创建一个简单的数据表,只含有name和password两个字段.至于角色,权限等,这里都先不考虑. 插入一条数据,name为admin,password ...

  6. 与Xamarin.Forms跨平台的用户界面

    Xamarin.Forms 与Xamarin.Forms跨平台的用户界面 Xamarin的. 形式是一个跨平台的UI工具包,它允许开发人员 轻松地创建本地用户界面布局,可以共享 在Android,iO ...

  7. 有关﹤![CDATA[ ]]> 说明

    CDATA DTD中的属性类型 全名:character data 在标记CDATA下,所有的标记.实体引用都被忽略,而被XML处理程序一视同仁地当做字符数据看待, CDATA的形式如下: <! ...

  8. 利用Referer请求头阻止"盗链"

    转自:http://wisdomsong2007.blog.163.com/blog/static/47783725200882523820664/ 前言 有一些站点自己没有提供下载空间,但是为了吸引 ...

  9. 15个CSS3和jQuery的超棒页面过渡效果教程

    来源:GBin1.com CSS3和jQuery从根本上改变了网页设计和程序开发.通过CSS3和jQuery,设计员和开发者不需要太多的精力或编码,就可以创造出非常 美丽令人叹惊的效果,同时还可以令你 ...

  10. MySQL数据库如何导入导出

    1 点击任意一个数据库,然后点击导出,导出为SQL格式,其他一切保持默认(不要勾选"添加 DROP TABLE/DROP VIEW") 2 勾选"另存为文件"点 ...