Linux下关于openssh和putty密钥的配置

openssh-key

生成密钥

ssh-keygen -t rsa -b 4096 -C "it@zuocheng.net"

密钥生效

$eval ssh-agent -s #注意这里是反引号
$ssh-add

把openssh-key 转为 puttyKey

使用puttygen.exe转换,load openssh-key,然后导出
可能会遇到"Putty key format too new"的问题,则在puttygen的选项卡 key->parameter for saving key files 中,把 "ppk file version" 改为V2

其他

Linux设置密钥登陆

vim常见插件汇总

Vim作为强大的文本编辑器,具有丰富的插件和功能。以下是一些常用的Vim插件:

NERDTree:在Vim中打开一个侧边栏,可以浏览和管理文件目录。

CtrlP:快速模糊查找文件和缓冲区中的内容。

Vundle:用于管理Vim插件的插件管理器。

YouCompleteMe:提供自动代码完成和语义分析功能,对于编码和改善程序员的工作流程非常有用。

Syntastic:对某些文件类型进行语法检查。

Fugitive:提供Git的集成,方便用户进行源代码版本控制。

Vim-airline:自定义状态栏,提高用户的工作效率和审美体验。

Vim-markdown:对Markdown文档进行语法高亮,方便编辑和查看。

Vimtex:LaTeX编辑器插件,提供自动完成以及LaTeX相关功能。

UltiSnips:提供代码段补全和自定义代码片段功能,方便编写重复的代码或快速输入命令。

这只是Vim插件的冰山一角,还有很多其他有用的插件可以扩展Vim的功能和增强用户体验。

查看Linux Cpu软中断脚本

代码如下:

#!/bin/sh
# filename softirqs_status.sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH

interrupts_=cat /proc/interrupts |grep -E "CPU0|em[0-9]{1,2}.*-[0-9]{1,3}$|eth[0-9]{1,2}.*-[0-9]{1,3}$|p[0-9]{1,2}p[0-9]{1,2}.*-[0-9]{1,3}$|nvme" |cat -s |column -t 
#interrupts_=cat /proc/interrupts |grep -E "CPU0|em[0-9]{1,2}.*-[0-9]{1,3}$|eth[0-9]{1,2}.*-[0-9]{1,3}$|p[0-9]{1,2}p[0-9]{1,2}.*-[0-9]{1,3}$" |cat -s |column -t 
#interrupts_=cat /proc/interrupts |grep -E "CPU0|em[0-9]{1,2}|eth[0-9]{1,2}|p[0-9]{1,2}p[0-9]{1,2}" |grep -i  "TxRx" |cat -s |column -t 
interrupts_=echo "${interrupts_}" |awk '{ if(NR == 1){one=$0;gsub(one, "- &", $0); print $0}else{print $0}}' |cat -s |column -t 
interrupts_=echo "${interrupts_}" |awk '{ for(i=2;i<=NF;i++){ if( $i ~ /^[[:digit:]]*$/ && $i >= 1000 && $i < 1000000 ){$i=int( $i / 1000 + 1)"K"}else if( $i ~ /^[[:digit:]]*$/ && $i >= 1000000 ){$i=int( $i / 1000000)"M"}}; print $0 }'
echo "${interrupts_}" |sed 's/CPU/U/g' |column -t

echo

cpu_c=expr $(cat /proc/cpuinfo |grep -i processor |wc -l) + 1 ;

softirqs_=cat /proc/softirqs |grep -E 'TX|RX|CPU' 
softirqs_=echo "${softirqs_}" |awk -v cpu_c=$cpu_c '{ if(NR == 1){one=$0;gsub(one, "- &", $0)}; for(i=1;i<=cpu_c;i++){print $i} }' 
softirqs_=echo "${softirqs_}" |xargs -n $cpu_c |column -t 
softirqs_=echo "${softirqs_}" |awk '{ for(i=2;i<=NF;i++){ if( $i ~ /^[[:digit:]]*$/ && $i >= 1000 && $i < 1000000 ){$i=int( $i / 1000 + 1)"K"}else if( $i ~ /^[[:digit:]]*$/ && $i >= 1000000 ){$i=int( $i / 1000000)"M"}}; print $0 }'
echo "${softirqs_}" |sed 's/CPU/U/g' |column -t

echo

显示结果如下

 bash /tmp/softirqs_status.sh
-    U0   U1  U2  U3  U4  U5  U6  U7  U8  U9  U10  U11  U12  U13  U14  U15  U16  U17  U18  U19  U20  U21  U22  U23  U24  U25  U26  U27  U28  U29  U30  U31
89:  102  0   0   0   0   0   0   0   0   0   0    0    0    0    0    0    0    0    0    0    0    0    0    4M   0    0    0    0    0    0    0    0    IR-PCI-MSI-edge  em49-fp-0
90:  4K   0   0   0   6M  0   0   0   0   0   0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    IR-PCI-MSI-edge  em49-fp-1
91:  50   5M  0   0   0   0   0   0   0   0   0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    IR-PCI-MSI-edge  em49-fp-2
92:  51   0   7M  0   0   0   0   0   0   0   0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    IR-PCI-MSI-edge  em49-fp-3
93:  64   0   0   6M  0   0   0   0   0   0   0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    IR-PCI-MSI-edge  em49-fp-4
94:  32   0   0   0   3K  0   0   0   0   0   0    0    0    0    0    0    0    5M   0    0    0    0    0    0    0    0    0    0    0    0    0    0    IR-PCI-MSI-edge  em49-fp-5
95:  41   0   0   0   0   6M  0   0   0   0   0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    IR-PCI-MSI-edge  em49-fp-6
96:  53   0   0   0   0   0   6M  0   0   0   0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    IR-PCI-MSI-edge  em49-fp-7

-        U0  U1  U2   U3   U4  U5   U6   U7   U8   U9   U10  U11  U12  U13  U14  U15  U16  U17  U18  U19  U20  U21  U22  U23  U24  U25  U26  U27  U28  U29  U30  U31
NET_TX:  35  26  667  114  93  160  106  1    0    0    0    0    0    0    0    0    1    2K   0    2    0    0    0    51   0    0    0    0    0    0    0    0
NET_RX:  5K  5M  7M   6M   6M  6M   6M   707  720  462  542  507  541  866  392  332  127  5M   289  199  251  244  194  4M   204  3K   396  446  217  165  164  128

Linux一些查看系统状态的命令

查看网卡速率

ethtool eth0 |grep Speed

这里的Speed是网卡的实际最大速率;如果开启了自动协商,就是是网卡和交换机协商之后的速率。

附带一个自动化脚本

PREFIX="p1p1"
ETH=ifconfig |grep ${PREFIX} | head -n1 | awk -F":" '{print $1;}'

if [ -z "${ETH}" ];then
   echo "emxx is not found"
   ifconfig
   exit 1
fi

ethtool eth0 |grep Speed

查看内存信息

  • 支持的内存槽数
  • 支持的最大内存

sudo dmidecode -t memory

输出如下:

# dmidecode 3.2
Getting SMBIOS data from sysfs.
SMBIOS 2.8 present.

Handle 0x0028, DMI type 16, 23 bytes
Physical Memory Array
        Location: System Board Or Motherboard
        Use: System Memory
        Error Correction Type: None
        Maximum Capacity: 8 GB
        Error Information Handle: Not Provided
        Number Of Devices: 2

Handle 0x002A, DMI type 17, 40 bytes
Memory Device
        Array Handle: 0x0028
        Error Information Handle: Not Provided
        Total Width: 64 bits
        Data Width: 64 bits
        Size: 4 GB
        Form Factor: DIMM
        Set: None
        Locator: A1_DIMM0
        Bank Locator: A1_BANK0
        Type: DDR3
        Type Detail: Unknown
        Speed: 1600 MT/s
        Manufacturer: Samsung
        Serial Number: 37A52995
        Asset Tag: A1_AssetTagNum0
        Part Number: M471B5173DB0-YK0
        Rank: 1
        Configured Memory Speed: 1333 MT/s
        Minimum Voltage: 1.35 V
        Maximum Voltage: 1.5 V
        Configured Voltage: 1.35 V

Handle 0x002C, DMI type 17, 40 bytes
Memory Device
        Array Handle: 0x0028
        Error Information Handle: Not Provided
        Total Width: Unknown
        Data Width: 64 bits
        Size: No Module Installed
        Form Factor: DIMM
        Set: None
        Locator: A1_DIMM1
        Bank Locator: A1_BANK1
        Type: Unknown
        Type Detail: Unknown
        Speed: Unknown
        Manufacturer: A1_Manufacturer1
        Serial Number: A1_SerNum1
        Asset Tag: A1_AssetTagNum1
        Part Number: Array1_PartNumber1
        Rank: Unknown
        Configured Memory Speed: Unknown
        Minimum Voltage: Unknown
        Maximum Voltage: Unknown
        Configured Voltage: Unknown

Docker搭建minio server

环境说明

  • Centos 7/8
  • docker/podman
  • minio version RELEASE.2021-07-08T19-43-25Z

搭建步骤

初始化目录

mkdir /minio
mkdir /minio/data
mkdir /minio/config

拉取镜像

docker pull minio/minio

如果是podman,执行:

podman pull minio/minio

启动镜像

ENGINE=podman
MINIO_ROOT=/minio
${ENGINE} run \
    --name minio \
    -p 9005:9005 \
    -p 9006:9006 \
    -e "MINIO_ROOT_USER=XXXXXXXXXXXXXX" \
    -e "MINIO_ROOT_PASSWORD=XXXXXXX/XXXXXX/XXXXXXX" \
    -v ${MINIO_ROOT}/data:/data \
    -v ${MINIO_ROOT}/config:/root/.minio \
    -d \
    minio/minio server /data --console-address ":9006" --address ":9005"

查看启动日志:

docker logs minio

运行成功:

API: http://10.88.0.97:9005  http://127.0.0.1:9005

Console: http://10.88.0.97:9006 http://127.0.0.1:9006

Documentation: https://docs.min.io

总结

和网上的一些文档不同, 新版MINIO参数已经发生了变化。比如:

MINIO_ROOT_USER替代了MINIO_SECRET
MINIO_ROOT_PASSWORD替代了MINIO_SECRET_KEY

另外一处变化则为web管理的地址和API地址已经分离,分别需要参数配置--console-address ":9006"--address ":9005"

附录

Linux 常用Yum仓库

EPEL

EPEL的全称叫 Extra Packages for Enterprise Linux 。EPEL是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质量软件包的项目。

官方网址为:https://fedoraproject.org/wiki/EPEL

Centos安装命令: sudo yum install epel-release

rpmfusion

RPM Fusion provides software that the Fedora Project or Red Hat doesn’t want to ship. That software is provided as precompiled RPMs for all current Fedora versions and current Red Hat Enterprise Linux or clones versions; you can use the RPM Fusion repositories with tools like yum and PackageKit.

RPM Fusion is a merger of Dribble, Freshrpms, and Livna; our goal is to simplify end-user experience by grouping as much add-on software as possible in a single location. Also see our FoundingPrinciples.

官方网站为 https://rpmfusion.org/

Remi repository

Remi repository 是包含最新版本 PHP、MySQL、Python 包的 Linux 源,由 Remi 提供维护。

官方网站为 http://rpms.remirepo.net/

Enterprise Linux 7 (with EPEL) x86_64 的安装方法:

wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
wget https://rpms.remirepo.net/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm epel-release-latest-7.noarch.rpm

如何要安装php的7.4版本则执行下面的命令:

yum -y --nogpgcheck install epel-release \
&& yum -y --nogpgcheck install https://rpms.remirepo.net/enterprise/remi-release-7.rpm \
&& yum -y --nogpgcheck install yum-utils \
&& yum-config-manager --enable remi-php74 \
&& yum -y --nogpgcheck install yum install nginx php  php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json

WANDisco

安装新版本的git 库

# 卸载旧版 git
yum -y remove git

# 安装 centos7 WANDisco 仓库
yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm

yum -y install git

podman使用经验

修改镜像容器存储地址(GraphRoot)

podman info # 找到配置文件地址,默认是/etc/containers/storage.conf

store:
  configFile: /etc/containers/storage.conf
  containerStore:

打开或者新建配置文件,修改GraphRoot和runroot地址。

[storage]
# Default Storage Driver
driver = "overlay"

# Temporary storage location
runroot = "/home/podman/containers/temp_storage"

# Primary Read/Write location of container storage
graphroot = "/home/podman/containers/storage"

修改镜像源地址

错误汇总

错误信息

Error processing tar file(exit status 1): there might not be enough IDs available in the namespace (requested 0:42 for /etc/gshadow): lchown /etc/gshadow: invalid argument

机器重启后,服务启动不起来

podman start mariadb

ERRO[0000] Error adding network: failed to allocate for range 0: 10.88.0.127 has been allocated to bb5e67927536b496894013ca3c42221bdfc4f7f72e30878e408bd97d88999e16, duplicate allocation is not allowed
ERRO[0000] Error while adding pod to CNI network "podman": failed to allocate for range 0: 10.88.0.127 has been allocated to bb5e67927536b496894013ca3c42221bdfc4f7f72e30878e408bd97d88999e16, duplicate allocation is not allowed
ERRO[0000] Error preparing container bb5e67927536b496894013ca3c42221bdfc4f7f72e30878e408bd97d88999e16: error configuring network namespace for container bb5e67927536b496894013ca3c42221bdfc4f7f72e30878e408bd97d88999e16: failed to allocate for range 0: 10.88.0.127 has been allocated to bb5e67927536b496894013ca3c42221bdfc4f7f72e30878e408bd97d88999e16, duplicate allocation is not allowed
Error: unable to start container "bb5e67927536b496894013ca3c42221bdfc4f7f72e30878e408bd97d88999e16": failed to mount shm tmpfs "/home/podman/containers/storage/overlay-containers/bb5e67927536b496894013ca3c42221bdfc4f7f72e30878e408bd97d88999e16/userdata/shm": no such file or directory

解决方法:清空网络

echo "" > /var/lib/cni/networks/podman/10.88.0.127

Centos8搭建Mini Server笔记

系统安全设置

配置防火墙

service firewalld start #重启防火墙

firewall-cmd --list-all #查看防火墙规则

firewall-cmd --permanent --add-port=80/tcp #开放80端口,HTTP

firewall-cmd --permanent --add-port=67/udp #开放67端口,DHCP
firewall-cmd --permanent --add-protocol=igmp #支持IGMP协议,路由发现协议
firewall-cmd --reload #重启防火墙

还有另一种方法,设置service,service列表在/usr/lib/firewalld/services目录中

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=samba
firewall-cmd --permanent --add-service=hdcp
firewall-cmd --permanent --add-service=llmnr

修改防火墙配置文件 /etc/firewalld/zones/public.xml



  Public
  For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
  
  
  
  
  
  
  
  
  
  
  
  

禁止root远程ssh登陆

修改/etc/ssh/sshd_config文件,将其中的PermitRootLogin改成no,

然后重新启动ssh服务/bin/systemctl restart sshd.service

系统设置

挂载的数据硬盘设置自动休眠

  • hdparm -B 127 /dev/sdb1
  • hdparm -S 60 /dev/sdb1

挂载的数据硬盘设置自动休眠(二)

在crontab中添加定时任务,每隔15分钟,关闭磁盘

*/15 * * * * (sdparm --command=stop /dev/sdc >> /tmp/sdparm.log)

设置 supervisord 开机启动

systemctl enable supervisord.service

设置用户 i 可以无密码获得sudo权限

添加 i ALL=(ALL) NOPASSWD: ALL 到 /etc/sudoers

centos 配置无线网卡

参考 https://www.cnblogs.com/asker009/p/10212045.html

安装配置rsyslog

  • sudo yum install rsyslog

查看CPU温度、硬盘温度

  • yum install lm_sensors hddtemp
  • sensors
  • hddtemp /dev/sdb

更新系统时间

yum install -y ntpdate

ntpdate ntp1.aliyun.com

hwclock --systohc

可选的时间服务器 www.pool.ntp.org/zone/cn

cn.ntp.org.cn #中国
edu.ntp.org.cn #中国教育网
ntp1.aliyun.com #阿里云
ntp2.aliyun.com #阿里云
cn.pool.ntp.org #最常用的国内NTP服务器

优化

创建swap文件

sudo -i
dd if=/dev/zero of=/opt/swapfile bs=1024 count=4194304
chown root:root /opt/swapfile
chmod 0600 swapfile
mkswap /opt/swapfile
swapon /opt/swapfile
echo "/opt/swapfile   none  swap  sw    0 0" >> /etc/fstab
swapon -s

卸载订阅管理器 rhsmcertd服务

rhsmcertd服务,即Red Hat Subscription Manager CERTification Daemon

systemctl stop rhsmcertd.service # 停止订阅服务
systemctl disable rhsmcertd.service # 取消订阅服务开机启动
yum remove subscription-manager

问题汇总

USB安装centos8,无法正常启动安装程序

解决方法: 刻录时,把UltraISO的写入方式改为为RAW

Linux wget遭遇证书不可信(Wget error: ERROR: The certificate of is not trusted.)

解决方法:
安装ca-certificates,并同步系统时间为最新。

  • yum install -y ca-certificates
  • ntpdate -u 0.north-america.pool.ntp.org

Error "curl: (60) Peer certificate cannot be authenticated with known CA certificates"

同上一个问题的解决方法

无法 mount ntfs磁盘

  • yum install epel-release
  • yum install ntfs-3g

umount: /mnt: target is busy.

  • 执行sync把buffer的数据写入磁盘
  • ls /mnt 查看占用/mnt的进程,并kill掉
  • 重新执行 umount /mnt

根目录变为只读,只能root登陆

重新挂载根目录, mount -o remount, rw /
一般为/etc/fstab 配置错误导致。

使用docker搭建C/C++ 开发环境

优点和意义

  • 环境隔离、资源共享、节省机器资源
  • 轻量虚拟机,启动和运行迅速

使用 Dockerfile 构造镜像

Dockerfile 内容

FROM centos:centos7.4.1708

MAINTAINER Zuocheng Liu 

RUN yum -y --nogpgcheck install gcc gcc-c++ kernel-devel make cmake  libstdc++-devel libstdc++-static glibc-devel glibc-headers \
&& yum -y --nogpgcheck install openssl-devel gperftools-libs \
&& yum -y --nogpgcheck install psmisc openssh-server sudo epel-release \
&& yum -y --nogpgcheck install vim git ctags \
&& mkdir /var/run/sshd \
&& echo "root:123456" | chpasswd \
&& sed -ri 's/^#PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config \
&& sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config \
&& ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key \
&& ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key

EXPOSE 22

CMD ["/usr/sbin/sshd", "-D"]

创建镜像

docker build -t cpp_dev:last .

创建容器

初始化容器, 宿主机调用脚本

  • 挂载容器
  • 调用容器内初始化脚本mount_in_docker.sh
#!/bin/bash

NEWUSER=$1
PORT=$2
PATH=$(dirname $(readlink -f "$0"))
echo "add User ${NEWUSER} sshd Port ${PORT}"
if [ ! -d /data/rtrs/${NEWUSER} ]; then
    /usr/bin/mkdir /data/rtrs/${NEWUSER}
fi
/usr/bin/cp /data/liuzuocheng/.profile /data/${NEWUSER}/.profile
/usr/bin/docker run -itd --name ${NEWUSER} --net=host -v /data/${NEWUSER}:/home/${NEWUSER} -v /data:/data rtrs/dev_cpp:centos7.4.1708
/usr/bin/docker exec -i ${NEWUSER} sh ${PATH}/mount_in_docker.sh ${NEWUSER} ${PORT} ${UID}

镜像内调用脚本

  • 添加用户
  • 启动sshd服务
NEWUSER=$1
PORT=$2
NEWUID=$3
echo "add User ${NEWUSER} sshd Port ${PORT}"
/usr/sbin/useradd -r -u ${NEWUID} -d /home/${NEWUSER} ${NEWUSER}
echo "${NEWUSER}:123456" | /usr/sbin/chpasswd
echo "Port ${PORT}" >> /etc/ssh/sshd_config
/usr/sbin/sshd

其他问题汇总

宿主机docker的目录迁移

docker的默认目录在/var/lib/docker 下面, 往往/var的磁盘比较小,建议把docker的目录改为大磁盘

把用户加入sudoer列表,sudo 执行命令无需密码

修改/etc/sudoers文件, 添加一行 user-name ALL=(ALL) NOPASSWD: ALL

更多问题可参考《Docker使用经验》

现实过程中使用docker搭建C/C++ 开发环境的工程意义

  • 新人入职代码培训,降低学习成本
  • 推动公司平台化建设

Docker使用经验

安装docker

ubuntu 环境

sudo apt-get install docker.io
sudo systemctl start docker

centos环境

sudo yum install docker
sudo service docker start

常用命令

启动docker

sudo docker run --net=host -v /home/zuocheng:/zuocheng -it zuocheng/dev_cpp /bin/bash

创建docker镜像

docker build -t zuocheng/dev_cpp .

挂载镜像

docker run -it --name dev_cpp -p 10022:22 zuocheng/dev_cpp

要免 sudo 调用 docker 命令方法, 创建docker用户组,并把当前用户添加到docker 用户组中。

sudo groupadd docker #创建 docker 用户组

sudo usermod -aG docker ${USER} # 当前用户加入 docker 用户组

sudo systemctl restart docker # 重启 docker 服务

newgrp - docker # 使设置在当前会话生效

使用docker搭建C++开发环境

参考博文《使用docker搭建C/C++ 开发环境》

更改docker的存储目录,避免默认目录磁盘不足的问题

vim /usr/lib/systemd/system/docker.service # 修改docker.service文件.

ExecStart=/usr/bin/dockerd --graph /data/docker # 在里面的EXECStart的后面

重启docker

systemctl disable docker
systemctl enable docker
systemctl daemon-reload
systemctl start docker

搭建 docker registry(私有仓库)

添加insecure-registries配置

修改/etc/docker/daemon.json,添加insecure-registries配置.把本地ip和端口添加进去。

    "insecure-registries": [
        "10.10.10.10:5000"
    ]
}

重启docker服务

sudo systemctl restart docker

下载 registry 镜像

docker pull registry

启动registry镜像的容器

docker run -d -p 5000:5000 -v /myregistry:/var/lib/registry registry

上传镜像

给本地镜像打tag

docker tag dev_cpp:latest 10.10.10.10:5000/dev_cpp:latest

上传镜像
docker push 10.10.10.10:5000/dev_cpp:latest

sed 使用经验

概述

sed 是处理文本时经常用到的工具,可实现对文本的输出,删除,替换,复制,剪切,导入,导出等各种操作。

本文依据作者的自身经验,记录sed的常用的使用方法。本文虽然不能涵盖sed的所有功能,但本文记录的sed用法能够解决了作者日常工作中 90%以上的问题。希望通过本文的分享,能让读者从sed的字数繁多的说明文档中解救出来,减小学习sed的成本。

sed 常用用法

sed -i 's/regular_expression/replacement/g' file.txt

这个命令格式是最常用的用法,在工作中使用sed场景中出现的频率最高。

对参数和各部分说明:

-i 表示修改文件,如果不带此参数,则将匹配的结果打印到屏幕。

s 表示替换字符串

regular_expression 正则表达式

replacement 对正则表达式匹配的目标,进行替换。如果replacement为空

g 对文件全局做替换

举例:

[www@it.zuocheng.net]$ sed -i 's/[a-z]*/+++/g' file.txt

sed 注意事项

在sed的正则表达式中, 有一些需要转移的特殊字符,比如:

+、(、),这些字符如果是正则表达式的语法中使用的字符,则需要进行转移;如果是文本中需要匹配的字符,则不需要转义。

高级用法

& 变量的使用

& 变量用在replacement部分,表示regular_expression匹配到的文本。

[www@it.zuocheng.net]$ echo "hello world"| sed 's/[a-z]*/(&)/g'
(hello) (world)

正则匹配 \1 – \9 变量

使用sed提取正则匹配的各个部分。

正则匹配的变量,主要用在replacement部分。表示regular_expression的子变量。

可用于截取字符串

[www@it.zuocheng.net]$ echo "hello123 world456"| sed 's/\([a-z]\+\)[0-9]\+/\1/g'
hello world

git-svn 使用经验

简介

git-svn可以帮助工程师使用git的管理方式,来管理svn代码库。

使用场景:

  • 历史原因,团队代码库使用svn管理,但同时又想使用git强大的分支管理的功能
  • 习惯了git且对svn不了解的工程师,但团队代码只能用svn管理的情况下,可以使用git-svn
  • 代码想从svn逐步迁移至git管理,中间过程中可以尝试使用git-svn

编译安装

安装包、依赖包列表

  • openssl # ssl
  • serf #让svn支持https
  • apr
  • apr-util
  • subversion 1.8+
  • git 1.9+
  • python 2.7+
  • scons 2.3+

其中,git-svn 工具已经被集成在了git 1.9+里,但是其同时也依赖于svn的perl接口。

以下是编译安装步骤和方法,安装路径以/home/liuzuocheng/local/为例:

编译安装apr 1.5+

官网: http://apr.apache.org/

./configure --prefix=/home/liuzuocheng/local/apr

make && make install

编译安装apr-util 1.5+

./configure --prefix=/home/liuzuocheng/local/apr-util --with-apr=/home/liuzuocheng/local/apr

make && make install

编译安装serf

官网: http://serf.apache.org/

scons APR=/home/liuzuocheng/local/apr APU=/home/liuzuocheng/local/apr-util OPENSSL=/usr/bin PREFIX=/home/liuzuocheng/local/serf

scons install

配置 LD_LIBRARY_PATH 写入/home/liuzuocheng/.bashrc

export LD_LIBRARY_PATH=/home/liuzuocheng/local/serf/lib:$LD_LIBRARY_PATH

编译安装subversion 1.8+ / 1.9+

官网: https://subversion.apache.org/

./configure --with-apr-util=/home/liuzuocheng/local/apr-util/ --with-apr=/home/liuzuocheng/local/apr/ --prefix=/home/liuzuocheng/local/subversion --with-serf=/home/liuzuocheng/local/serf

make && make install

同时安装 subversion perl 接口,在subversion源码根目录下执行:

make swig-pl

make install-swig-pl

配置 LD_LIBRARY_PATH 写入/home/liuzuocheng/.bashrc :

export LD_LIBRARY_PATH=/home/liuzuocheng/local/subversion/lib:$LD_LIBRARY_PATH

安装git

官网: https://git-scm.com/downloads

./configure --prefix=/home/liuzuocheng/local/git

make && make install

配置 LD_LIBRARY_PATH 写入/home/liuzuocheng/.bashrc :

export LD_LIBRARY_PATH=/home/liuzuocheng/local/git/lib:$LD_LIBRARY_PATH

安装问题汇总

  • 编译安装serf,执行scons 显示 no module named scons.script

解决方法,配置scons 环境变量:

export SCONS_LIB_DIR=$LOCAL/python/lib/python2.7/site-packages/scons-2.5.0-py2.7.egg/scons-2.5.0/

  • 执行git svn clone 等命令,提示错误 Can’t locate SVN/Core.pm

解决方法,将svn的perl接口软链至git库中:

ln -s /home/liuzuocheng/local/subversion/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/SVN /home/liuzuocheng/local/git/lib/perl5/site_perl/5.8.8/SVN

ln -s /home/liuzuocheng/local/subversion/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto /home/liuzuocheng/local/git/lib/perl5/site_perl/5.8.8/SVN/auto

使用方法

  • 检出代码 ,类同于 svn checkout 的功能

git svn clone https://svn.zuocheng.net/test/trunk

为了提高下载速度,避免从第一个版本开始下载,可通过添加参数-r$REVNUMBER:HEAD,检出指定版本的代码

git svn clone -r18000:HEAD https://svn.zuocheng.net/test/trunk

  • 从中心服务器的svn repository获取最新更新, 类同于 svn up 功能

git svn rebase

  • 可以在本地建立分支,操作和git相同

git branch master dev

  • 查看提交历史日志 , 类同于 svn log 功能

    git svn log #svn log的展示方式

或者

git log #git log的展示方式

  • 将本地代码同步到Svn服务器

    git-svn dcommit # 将你在本地使用git commit提交到本地代码库的所有更改逐一提交到svn库

  • 解决冲突

1 git-svn rebase , 更新版本,发现冲突文件conflict.c

2 编辑冲突文件vim conflict.c,解决冲突

3 git add conflict.c , 通知git,已经解决冲突

4 git rebase –continue , 重新更新版本,查看是否还有冲突,如果有,跳到第2步,否则继续下一步

5 git-svn dcommit, 提交更改

  • 如何切换远程分支, 类同于 svn relocate 的功能

wiki中曾提到有一个方法,但是在我的环境上不起作用 https://git.wiki.kernel.org/index.php/GitSvnSwitch

  • 其他

官方文档地址: https://git-scm.com/docs/git-svn

使用陷阱

  • 待完善

总结

Git 使用经验总结

常用命令

按使用频度排序

  • git pull
  • git log
  • git diff
  • git commit
  • git commit –amend
  • git review
  • git clone
  • git push
  • git reset –hard/soft
  • git checkout
  • git fetch –all
  • git merge
  • git branch -b develop origin/develop
  • git clean -df

git 命令非常多,但常用的只有以上几个

常用配置

用户名和密码

$ git config --global user.name liuzuocheng

$ git config --global user.email zuocheng.liu@gmail.com

权限验证

在git pull/push/clone 时输入密码会比较麻烦。在所有的解决方法中,除了使用密钥之外,还可以使用.git-credentials配置文件。这种方法的坏处则是密码用明文存储,安全风险大。

touch ${HOME}/.git-credentials
输入内容
https://username:password@gitlab.zuocheng.net
执行下面命令使其生效
git config --global credential.helper store

默认编辑器

$ git config --global core.editor vim

配置比较工具

git config --global merge.tool vimdiff

git config --global diff.tool vimdiff

git config --global difftool.prompt false

git config --global alias.d difftool

或者直接更改配置文件 ~/.gitconfig,添加手工添加配置

常用经验

  • 将远程主干合并到本地分支

在代码上线前,这一步非常重要

git fetch && git rebase origin/master

git fetch --all && git merge origin master

分支间文件差异大,频繁切换显示aborting, git status后发现大量为trace的文件

通过下面的命令清理它们
git clean -df

解决版本冲突

暂存本地修改

$ git stash

拉取版本库中最新版本

$ git pull

将本地修改与版本库中最新版本合并

$ git stash pop stash@{0}

解决冲突,使用下面的工具会非常方便

$ git d

  • 将源码导出tar包

git对应的功能是归档

mkdir ../working

git archive master | tar -x -C ../working

Git 高级功能

submodule

git submodule add 仓库地址 路径

git submodule update --init

git submodule update

fork后如何同步源的新更新 ?

  • 首先要先确定一下是否建立了主repo的远程源

git remote -v

  • 如果里面只能看到你自己的两个源(fetch 和 push),那就需要添加主repo的源:

git remote add upstream URL

  • 查看主repo的远程源

git remote -v

  • 拉取主repo源的代码

git fetch upstream

  • 合并

git merge upstream/master

  • 提交

git push

git 项目打包导出

示例, 项目中有为1.0的tag

git archive 1.0 | bzip2 > v1.0.tar.bz2

示例,打包master分支

git archive --format tar.gz --output "../project.tar.gz" master

与 Gerrit 配合使用

提交代码审核 git push origin HEAD:refs/for/mybranch

submodule

一键更新所有submodule,包含所有层级

git submodule update --init --recursive

submodule 更新后,版本不一致,引发意外出core

重新初始化并拉取所有的submodule

git submodule deinit -f --all
git submodule update --init --recursive

高级用法

git rebase

这是介绍rebase用法特别好的博客,http://blog.chinaunix.net/uid-27714502-id-3436706.html

git清除所有commit历史记录

# 新建初始分支
git checkout --orphan new_branch 
# 添加文件
git add -A
# 提交修改
git commit -m "Initial commit" -s
# 覆盖原master分支后push
git branch -D master
git branch -m master
git push origin master -f

常见异常处理

fatal: Not possible to fast-forward, aborting.

处理方法是 rebase
git pull origin master --rebase
git rebase --continue

error: remote unpack failed: error Missing tree

执行push时添加–no-thin选项,参考stackoverflow
git push --no-thin

Server does not allow request for unadvertised object

常为更改了submodule的引用地址造成,可以使用git submodule sync命令解决,参考stackoverflow

Ubuntu 使用问题汇总

SSH 连接Ubuntu反应慢

  • SSH服务默认启用了DNS反向解析的功能
  • /etc/ssh/sshd_config文件中修改或加入UseDNS=noGSSAPIAuthentication no 两行
  • 重启sshd sudo service sshd restart

删除旧内核

uname -a  #使用这个命令可以查看当前系统使用的内核。

dpkg --get-selections|grep linux #列出当前内核,带image的则是已经安装的内核。可以用类似以下命令卸载:

sudo apt-get remove linux-image-2.6.24-11-generic #其中 linux-image-2.6.24-11-generic 为版本号,输全 。

在最新的ubuntu (15.10以上)版本中,执行sudo apt-get autoremove也可以直接卸载旧内核

如何升级 Ubuntu 版本, 比如从 Ubuntu 15.10 升级到 Ubuntu 16.04

终端执行如下命令:

sudo do-release-upgrade -d

如果命令do-release-upgrade不存在,则安装 update-manager-core :

sudo apt-get install update-manager-core

Ubuntu中安装和卸载Gnome、KDE、XFACE桌面环境

  • 安装XFACE: sudo apt-get install xubuntu-desktop
  • 安装KDE:sudo apt-get install kubuntu-desktop
  • 安装Gnome:sudo apt-get install ubuntu-desktop

Ubuntu卸载桌面环境(应备份重要文件)

  • 卸载gnome:sudo apt-get --purge remove liborbit2
  • 卸载kde:sudo apt-get --purge remove kdelibs4c2a libarts1c2a
  • 卸载xface:sudo apt-get --purge remove xfce4

给用户添加sudo权限

  • root权限编辑/etc/sudoers文件

添加一行 xxx ALL=(ALL) ALL

或者添加 xxx ALL=(ALL) NOPASSWD: ALL , 使用不用输密码

配置静态IP后无法解析域名

编辑 /etc/network/interfaces 文件, 添加配置:

dns-nameservers 114.114.114.114 114.114.115.115

update-apt-xapi占用资源过高

  • 后台任务,会定期把cpu和磁盘IO打满
  • 解决方法,直接卸载掉 apt-get autoremove --purge apt-xapian-index

升级Ubuntu版本,比如从15.04升级到16.04

先备份!

sudo apt-get update

sudo apt-get install update-manager-core

sudo do-release-upgrade

Ubuntu 无法重启、关机

此问题发生于Ubuntu 16.04 LTS版本,由内核bug导致。

解决办法是,把内核升级到最新版本。截止本文,Ubuntu 16.04 源中的最新版本是4.10.0-22

sudo apt-get install linux-image-4.10.0-22-lowlatency

sudo apt-get install linux-headers-4.10.0-22-lowlatency

最新版本内核查找方法如下:

sudo apt-cache search linux-image

Ubuntu Desktop 软件推荐

本文所列都是作者正在使用的软件,也都是个人爱好的软件。

适用Ubuntu版本 Ubuntu 15.10 、 Ubuntu 15.04

文本编辑

  • wps-office

输入法

  • 搜狗拼音输入法

    百度上搜索搜狗拼音输入法 for linux 可得到下载链接

浏览器

  • Chrome
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
sudo apt-get update
sudo apt-get install google-chrome-stable

系统工具

  • SysPeek

    sudo add-apt-repository ppa:nilarimogard/webupd8
    sudo apt-get update
    sudo apt-get install syspeek

远程桌面

  • xrdp

sudo apt-get install xrdp

下载工具

  • axel 相当于迅雷

sudo apt-get install axel

开发工具

  • vim
  • vpnc
  • git
  • Sublime Text 3

    sudo add-apt-repository ppa:webupd8team/sublime-text-3
    sudo apt-get update
    sudo apt-get install sublime-text

删除原装软件

sudo apt-get remove libreoffice-common
sudo apt-get remove unity-webapps-common  # 删除Amazon的链接
sudo apt-get remove thunderbird totem rhythmbox empathy brasero simple-scan gnome-mahjongg aisleriot gnome-mines cheese transmission-common gnome-orca webbrowser-app gnome-sudoku  landscape-client-ui-install
sudo apt-get remove onboard deja-dup

netsh winsock reset

倒腾windows,重启后突然上不了网

1.ip dns 设置正常

2.能ping dns的ip

3.nslook 正常

4.浏览器打不开网页,socket链接无法建立。

解决办法

netsh winsock reset

原理

重置Winsock目录,winsock工作在应用层,它提供与底层传输协议无关的高层数据传输编程接口。由症状得知就是问题出在应用层。

svn 使用经验总结

都是经验,甚至教训!

移动svn目录 最好的方法是使用svn move;

中间好的方法是先svn copy 原路径 新路径,再svn delete 原路径;

最坏的方法是将本地目录移动一下,然后在svn delete 原路径 和 svn add 新路径,先不说这种方法比较麻烦,其实还存在错误,因为本地移动目录,目录中的.svn隐藏文件夹也被移动;

参考:http://www.subversion.org.cn/svnbook/nightly/svn.ref.svn.c.move.html

apache nginx 配置多端口监听,浏览器自动跳转到80端口

       最近两天在分别在配置apache 和 nginx 时,都遇到了一个相同的问题:        

为apache和nginx开启监听多个端口(比如监听80和8080端口)时,改好配置,重启服务。

在浏览器端访问8080端口,url自动跳转到80端口,例如url输入http://**.**.**.**:8080, 则浏览器自动跳转到http://**.**.**.**。

而如果如果在url中的8080端口的后面添加访问文件,则正常展示8080端口下的目录或文件。

离奇的是,过一段时间,大约1个小时后,再访问http://**.**.**.**:8080,就不会跳转到http://**.**.**.**了。

网上搜索,记录此问题的网文不多。 然后自己分析了一下,大概有2种可能性。

1.浏览器端的缓存造成。但是,我换了浏览器,清空了缓存,依然如上。

2.服务器端缓存,1小时后才能恢复正常。 第2种情况比较靠谱些,顺着这个思路,最终找到了答案: 是因为开启了RPCBind服务造成。

解决方法是,将RPCBind的缓存清空。

LNMP启停控制自动脚本

常常在更改LNMP配置之后,需要重启LNMP,在此写了一个自动化脚本,方便重启LNMP。 除了方便重新启动,还增加了关闭与开启功能。 Operating System: Ubuntu Server 代码


# !/bin/bash
function start(){ sudo service nginx start sudo service php5-fpm start } 
function startall(){ start sudo service mysql start }
function stop(){ sudo service nginx stop sudo service php5-fpm stop } 
function stopall(){ stop sudo service mysql start } 
function restart(){ sudo service nginx restart sudo service php5-fpm restart } 
function restartall(){ restart sudo service mysql restart }

## main()

case "$1" in 
    "start") start ;; 
    "startall") startall ;; 
    "stop") stop ;; 
    "stopall") stopall ;; 
    "restart") restart ;; 
    "restartall") restartall ;; 
    "configtest") configtest ;; 
*) echo "Usage: $0 {start|stop|restart|startall|stopall|restartall}" 

esac exit 0 

webserver restart //重启nginx 和 php-fpm