Category: Linux

  • 查看路由与添加删除静态路由

    静态路由可以细化,定制网络运行方式。很多时候网络走不通,也要加静态路由,指定包的转发。

    显示当前的路由表

    root@agent-test:~# ip route show
    default via 192.168.198.2 dev eth0 
    192.168.198.0/24 dev eth0  proto kernel  scope link  src 192.168.198.137

    内核的路由表

    root@agent-test:~# route
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    default         localhost       0.0.0.0         UG    0      0        0 eth0
    192.168.198.0   *               255.255.255.0   U     0      0        0 eth0

    内核的路由表,全部使用数字方式显示

    root@agent-test:~# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.198.2   0.0.0.0         UG    0      0        0 eth0
    192.168.198.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0

    添加路由,格式为 ip route add {目标网络} via {ip地址} dev {设备},如192.168.3.0/24的网络从192.168.1.254走

    ip route add 192.168.3.0/24 via 192.168.1.254 dev eth0

    旧命令格式为

    route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.1.254 dev eth1

    以上命令立即生效,重启失效。永久静态路由

    如CentOS下

    #vi /etc/sysconfig/network-scripts/route-eth0

    既可以如下添加一行

    10.0.0.0/8 via 10.9.38.65

    也可以按这种格式

    ADDRESS0=192.168.0.62
    NETMASK0=255.255.255.255
    GATEWAY0=192.168.8.51
    ADDRESS1=192.168.0.71
    NETMASK1=255.255.255.255
    GATEWAY1=192.168.8.51
    ADDRESS2=192.168.1.0
    NETMASK2=255.255.255.0
    GATEWAY2=192.168.8.28

    重启网络后生效

    在Debian下,找到对应的interface,编辑/etc/network/interface

    auto eth0
    iface eth0 inet static
    address 10.9.38.76
    netmask 255.255.255.240
    network 10.9.38.64
    broadcast 10.9.38.79
    ### static routing ###
    post-up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.9.38.65
    pre-down route del -net 10.0.0.0 netmask 255.0.0.0 gw 10.9.38.65

    在启动eth0时,添加一条路由,关闭时删掉对应路由即可。

    除了路由的添加删除,可以使用ip route replace改变已有路由的属性。如

    ip route replace default via 192.168.8.33 dev eth0

    关于默认路由,在CentOS中,既可以在/etc/sysconfig/networ-scripts/ifcfg-eth0中指定gateway,也可以在/etc/sysconfig/network中指定gateway,也可以在上面的ip route add default via 192.168.8.1 dev eth0。

  • shell判断文件,目录是否存在或者具有权限

    #!/bin/sh

    myPath=”/var/log/httpd/”
    myFile=”/var/log/httpd/access.log”

    #这里的-x 参数判断$myPath是否存在并且是否具有可执行权限

     if [ ! -x "$myPath"]; then
     mkdir "$myPath"
     fi

    #这里的-d 参数判断$myPath是否存在

     if [ ! -d "$myPath"]; then
     mkdir "$myPath"
     fi

    #这里的-f参数判断$myFile是否存在

     if [ ! -f "$myFile" ]; then
     touch "$myFile"
     fi

    #其他参数还有-n,-n是判断一个变量是否是否有值

     if [ ! -n "$myVar" ]; then
     echo "$myVar is empty"
     exit 0
     fi

    #两个变量判断是否相等

     if [ "$var1" = "$var2" ]; then
     echo '$var1 eq $var2'
     else
     echo '$var1 not eq $var2'
     fi

    判断一个变量是不是纯数字

    if [[ $object_id =~ ^[0-9]+$ ]]; then
        echo "$object_id is a number"
    fi
  • mysql主从复制

    大致思路:设置master端,用于复制的账户,锁表导入数据库,在slave中导入数据库,设置slave端

    mysql主从复制是一种比较靠谱的备份方式,这里用最简单的,一个master,一个slave

    12.34.56.789- Master Database

    12.23.34.456- Slave Database

    前提条件:

    主从数据库大版本要一样,可以用mysql -V查看,我这里用的是mysql  Ver 14.14 Distrib 5.5.31

    设置master端

    配置文件vim /etc/mysql/my.cnf,mysql的bind-address只能绑一个特定的ip或0.0.0.0,我现在用的是0.0.0.0,即

    bind-address            = 0.0.0.0

    在 [mysqld]段下为server-id设置一个独立的编号

    server-id               = 1

    主从复制需要binary的log日志,将log_bin这一行取消注释

    log_bin                 = /var/log/mysql/mysql-bin.log

    设置要复制的数据库jpuyydb

    binlog_do_db            = jpuyydb

    master的配置文件设置完了,重启mysql

    /etc/init.d/mysql restart

    接下来在mysql中操作,mysql -u root -p,添加一个复制用的账户slave_user密码是password,这里的权限是可以复制所有数据库

    GRANT select, replication client, replication slave *.* TO 'slave_user'@'%' IDENTIFIED BY 'password';

    刷新权限

    FLUSH PRIVILEGES;

    接下来的操作需要按步骤操作,将jpuyydb锁表,防止有其他操作影响当前状态

    use jpuyydb;
    flush tables with read lock;

    接下来查看master状态,并将下面的值file和position记录下来

    mysql> show master status;
    +------------------+----------+--------------+------------------+
    | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    | mysql-bin.000001 | 107 | jpuyydb | test |
    +------------------+----------+--------------+------------------+
    1 row in set (0.00 sec)

    如果在当前的界面有一些操作的话,数据库的表会自动解锁,所以新开一个窗口,将数据库备份下来,

    mysqldump -u root -p --opt jpuyydb > jpuyydb.sql

    然后在原窗口解锁,退出,master端就设置好了

    UNLOCK TABLES;
    QUIT;

    在slave中创建并导入数据库,

    在mysql中创建数据库

    create database if not exists jpuyydb default charset utf8 collate utf8_general_ci;

    导入数据库

    mysql -u root -p jpuyydb < jpuyydb.sql

    设置slave端,vim /etc/mysql/my.cnf,需要修改的如下

    server-id               = 100

    添加一条relay-log

    relay-log               = /var/log/mysql/mysql-relay-bin.log
    log_bin                 = /var/log/mysql/mysql-bin.log
    binlog_do_db            = jpuyydb

    修改好之后退出,重启mysql,之后进入mysql控制界面,运行

    CHANGE MASTER TO MASTER_HOST='12.34.56.789',MASTER_USER='slave_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=  107;

    现在slave设置好了,启动slave

    START SLAVE;

    查看slave的状态,\G将输出以适合阅读的方式显示出来

    SHOW SLAVE STATUS\G

    如果下面两条都是Yes表示成功

    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes

    Trouble Shooting:

    如果一直是连接状态,需要做如下排查,

    在slave端尝试连接master

    mysql -u slave_user -p -P 3306 -h 12.34.56.789

    在master端查看3306的连接情况,应该有一条类似于下面的ESTABLISHED的记录

    lsof -i tcp:3306
    mysqld  10310 mysql   15u  IPv4 7636301      0t0  TCP ip-10-128-.internal:mysql->112.65.13.1s:55781 (ESTABLISHED)

    在slave端如果一直是connecting状态,则尝试如下操作

    SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; SLAVE START;

    在slave端出现如下错误时

    ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log

    解决方法一:

    进入到/var/lib/mysql/删掉

    master.info
    relay-log.info

    解决方法二:

    mysql> flush slave;
    
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> reset slave;
    Query OK, 0 rows affected (0.00 sec)

    参考:

    https://www.digitalocean.com/community/articles/how-to-set-up-master-slave-replication-in-mysql

  • 使用iperf测试网络的性能

    准备工作:

    安装epel源

    rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm

    更新本地cache安装iperf

    yum makecache -y
    yum install iperf -y
    

     

    测试工作:

    192.168.0.244为server端,192.168.0.236为client端

    在server端和client端可以查看到传输的情况,还可以通过ifstat, iptraf查看网卡的流量

    TCP测试

    server(0.244)

    iperf -s -i 1
    
    -s 服务器模式
    
    -i 报告显示间隔秒数
    

    client(0.236)

    iperf -t 20 -i 1 -c 192.168.0.244
    

    -t 测试用时的秒数

    -c 客户端模式,后面接要连接的服务器

    服务端显示:

    ------------------------------------------------------------
    Server listening on TCP port 5001
    TCP window size: 32.0 KByte (default)
    ------------------------------------------------------------
    [ 4] local 192.168.0.244 port 5001 connected with 192.168.0.236 port 54921
    [ ID] Interval Transfer Bandwidth
    [ 4] 0.0- 1.0 sec 112 MBytes 938 Mbits/sec
    [ 4] 1.0- 2.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 2.0- 3.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 3.0- 4.0 sec 112 MBytes 941 Mbits/sec
    [ 4] 4.0- 5.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 5.0- 6.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 6.0- 7.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 7.0- 8.0 sec 112 MBytes 941 Mbits/sec
    [ 4] 8.0- 9.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 9.0-10.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 10.0-11.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 11.0-12.0 sec 112 MBytes 941 Mbits/sec
    [ 4] 12.0-13.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 13.0-14.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 14.0-15.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 15.0-16.0 sec 112 MBytes 941 Mbits/sec
    [ 4] 16.0-17.0 sec 112 MBytes 941 Mbits/sec
    [ 4] 17.0-18.0 sec 112 MBytes 941 Mbits/sec
    [ 4] 18.0-19.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 19.0-20.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 0.0-20.0 sec 2.20 GBytes 941 Mbits/sec
    

    客户端显示:

    ------------------------------------------------------------
    Client connecting to 192.168.0.244, TCP port 5001
    TCP window size: 23.2 KByte (default)
    ------------------------------------------------------------
    [ 3] local 192.168.0.236 port 54921 connected with 192.168.0.244 port 5001
    [ ID] Interval Transfer Bandwidth
    [ 3] 0.0- 1.0 sec 115 MBytes 965 Mbits/sec
    [ 3] 1.0- 2.0 sec 112 MBytes 937 Mbits/sec
    [ 3] 2.0- 3.0 sec 113 MBytes 946 Mbits/sec
    [ 3] 3.0- 4.0 sec 113 MBytes 946 Mbits/sec
    [ 3] 4.0- 5.0 sec 112 MBytes 935 Mbits/sec
    [ 3] 5.0- 6.0 sec 113 MBytes 946 Mbits/sec
    [ 3] 6.0- 7.0 sec 112 MBytes 935 Mbits/sec
    [ 3] 7.0- 8.0 sec 113 MBytes 946 Mbits/sec
    [ 3] 8.0- 9.0 sec 113 MBytes 946 Mbits/sec
    [ 3] 9.0-10.0 sec 111 MBytes 934 Mbits/sec
    [ 3] 10.0-11.0 sec 113 MBytes 946 Mbits/sec
    [ 3] 11.0-12.0 sec 111 MBytes 934 Mbits/sec
    [ 3] 12.0-13.0 sec 113 MBytes 945 Mbits/sec
    [ 3] 13.0-14.0 sec 113 MBytes 945 Mbits/sec
    [ 3] 14.0-15.0 sec 113 MBytes 946 Mbits/sec
    [ 3] 15.0-16.0 sec 111 MBytes 931 Mbits/sec
    [ 3] 16.0-17.0 sec 113 MBytes 948 Mbits/sec
    [ 3] 17.0-18.0 sec 111 MBytes 934 Mbits/sec
    [ 3] 18.0-19.0 sec 113 MBytes 945 Mbits/sec
    [ 3] 19.0-20.0 sec 113 MBytes 948 Mbits/sec
    [ 3] 0.0-20.0 sec 2.20 GBytes 942 Mbits/sec
    

    UDP 测试

     

    server(0.244): iperf -u -s -i 1

    client(0.236): iperf -t 20 -i 1 -u -b 1000M -c 192.168.0.244

     

    -u 使用udp协议

    -b 后面接每秒带宽发送量(udp下适用)

     

    服务器显示

    ------------------------------------------------------------
    Server listening on UDP port 5001
    Receiving 1470 byte datagrams
    UDP buffer size: 224 KByte (default)
    ------------------------------------------------------------
    [ 3] local 192.168.0.244 port 5001 connected with 192.168.0.236 port 34489
    [ ID] Interval Transfer Bandwidth Jitter Lost/Total Datagrams
    [ 3] 0.0- 1.0 sec 128 KBytes 1.05 Mbits/sec 0.013 ms 0/ 89 (0%)
    [ 3] 1.0- 2.0 sec 128 KBytes 1.05 Mbits/sec 0.010 ms 0/ 89 (0%)
    [ 3] 2.0- 3.0 sec 128 KBytes 1.05 Mbits/sec 0.015 ms 0/ 89 (0%)
    [ 3] 3.0- 4.0 sec 128 KBytes 1.05 Mbits/sec 0.012 ms 0/ 89 (0%)
    [ 3] 4.0- 5.0 sec 128 KBytes 1.05 Mbits/sec 0.008 ms 0/ 89 (0%)
    [ 3] 5.0- 6.0 sec 129 KBytes 1.06 Mbits/sec 0.008 ms 0/ 90 (0%)
    [ 3] 6.0- 7.0 sec 128 KBytes 1.05 Mbits/sec 0.009 ms 0/ 89 (0%)
    [ 3] 7.0- 8.0 sec 128 KBytes 1.05 Mbits/sec 0.019 ms 0/ 89 (0%)
    [ 3] 8.0- 9.0 sec 128 KBytes 1.05 Mbits/sec 0.011 ms 0/ 89 (0%)
    [ 3] 9.0-10.0 sec 128 KBytes 1.05 Mbits/sec 0.009 ms 0/ 89 (0%)
    [ 3] 10.0-11.0 sec 128 KBytes 1.05 Mbits/sec 0.014 ms 0/ 89 (0%)
    [ 3] 11.0-12.0 sec 129 KBytes 1.06 Mbits/sec 0.014 ms 0/ 90 (0%)
    [ 3] 12.0-13.0 sec 128 KBytes 1.05 Mbits/sec 0.009 ms 0/ 89 (0%)
    [ 3] 13.0-14.0 sec 128 KBytes 1.05 Mbits/sec 0.017 ms 0/ 89 (0%)
    [ 3] 14.0-15.0 sec 128 KBytes 1.05 Mbits/sec 0.016 ms 0/ 89 (0%)
    [ 3] 15.0-16.0 sec 128 KBytes 1.05 Mbits/sec 0.011 ms 0/ 89 (0%)
    [ 3] 16.0-17.0 sec 128 KBytes 1.05 Mbits/sec 0.012 ms 0/ 89 (0%)
    [ 3] 17.0-18.0 sec 129 KBytes 1.06 Mbits/sec 0.017 ms 0/ 90 (0%)
    [ 3] 18.0-19.0 sec 128 KBytes 1.05 Mbits/sec 0.018 ms 0/ 89 (0%)
    [ 3] 19.0-20.0 sec 128 KBytes 1.05 Mbits/sec 0.012 ms 0/ 89 (0%)
    [ 3] 0.0-20.0 sec 2.50 MBytes 1.05 Mbits/sec 0.013 ms 0/ 1785 (0%)
    

    客户端显示

    ------------------------------------------------------------
    Client connecting to 192.168.0.244, UDP port 5001
    Sending 1470 byte datagrams
    UDP buffer size: 224 KByte (default)
    ------------------------------------------------------------
    [ 3] local 192.168.0.236 port 34489 connected with 192.168.0.244 port 5001
    [ ID] Interval Transfer Bandwidth
    [ 3] 0.0- 1.0 sec 129 KBytes 1.06 Mbits/sec
    [ 3] 1.0- 2.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 2.0- 3.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 3.0- 4.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 4.0- 5.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 5.0- 6.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 6.0- 7.0 sec 129 KBytes 1.06 Mbits/sec
    [ 3] 7.0- 8.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 8.0- 9.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 9.0-10.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 10.0-11.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 11.0-12.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 12.0-13.0 sec 129 KBytes 1.06 Mbits/sec
    [ 3] 13.0-14.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 14.0-15.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 15.0-16.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 16.0-17.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 17.0-18.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 18.0-19.0 sec 129 KBytes 1.06 Mbits/sec
    [ 3] 19.0-20.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 0.0-20.0 sec 2.50 MBytes 1.05 Mbits/sec
    [ 3] Sent 1785 datagrams
    [ 3] Server Report:
    [ 3] 0.0-20.0 sec 2.50 MBytes 1.05 Mbits/sec 0.013 ms 0/ 1785 (0%)
    
  • proxmox虚拟化环境-pve

    proxmox VE 是一个非常棒的同时支持openvz和kvm虚拟化环境平台,简称PVE,基于debian制作。
    下载好镜像之后,可以用u盘安装,http://pve.proxmox.com/wiki/Install_from_USB_Stick
    在linux下使用dd命令制作一个可以安装用的u盘:
    dd if=pve-cd.iso of=/dev/XYZ bs=1M
    安装过程,略
    打开https://ip:8006,使用安装时设置的用户名和密码登陆。
    可以创建的有VM和CT,VM即kvm虚拟机,kvm可以装windows,linux;CT即ConTainer(容器),也就是openvz虚拟机,不能装windows。
    制作CentOS的模板
    对于 kvm 虚拟化的资源 210 是里面的 id ,复制一台新机器 211
    qm clone 210 211 -full 1 -format qcow2 -name ready-to-use.jpuyy.com -storage data

    kvm 从宿主机切换虚拟机使用 terminal

    在宿主机配置文件中编辑,kvm 虚拟机为 2549

    /etc/pve/qemu-server/2549.conf

    添加一行

    serial0: socket

    虚拟机为 centos6

     vim /boot/grub/grub.conf

    在 kernel 一行中追加

    console=tty0 console=ttyS0

    示例

    kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=UUID=dbc46481-4b4c-420f-9b32-8e8ac82a4979 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet console=tty0 console=ttyS0

    这些做完,

    qm stop 2549
    qm start 2549

    重启机器后

    qm terminal 2549

    即可从宿主机进入虚拟机 terminal.

    参考:

    https://pve.proxmox.com/wiki/Serial_Terminal

    https://pve.proxmox.com/wiki/Qm_manual

  • linux shell操作快捷键

    使用快捷键节省生命

    使用命令操作linux,需要敲不少命令,而命令并不是一次性敲对的,中间改主意的时候很多,所以需要快速对已经打出的命令进行修改。

    剪切光标至行首的字符,效果等同于ctrl+c,也等同于按backspace不放。打字打到一半改变主意就用这个快捷键。

    ctrl+u

    剪切光标至行尾的字符

    ctrl+k

    清除屏幕内容,等同于clear命令

    ctrl+l

    清除最后一个单词,常用

    ctrl+w

    删除前一个字符,同退格键

    ctrl+h

    粘贴所剪切的字符,不是系统剪切板,而是执行剪切或删除命令的undo

    ctrl+y

    删除后一个字符,相当于delete键

    ctrl+d

    光标的跳转

    有些时候shell左右方向键移动很管用,但是Home和End却不管用。这里有万用的跳转方式:

    光标向右(前)

    ctrl+f

    光标向左(后)

    ctrl+b

    跳转到行头,a代表alphabet

    ctrl+a

    跳转到行尾,e代表end

    ctrl+e

    行头和当前光标位置互相跳转

    ctrl+x

    回车

    ctrl+j
    ctrl+m
    

    重用历史命令

    查看历史3条命令

    history 3

    历史记录里都有一个编号,比如说是233,那么重新执行这条命令

    !233

    执行倒数第 4 条命令

    !-4

    再次执行上一条命令

    !!

    搜索历史命令(比history | grep ping更快),回车后执行

    ctrl+r

    显示上一条命令

    ctrl+p

    显示下一条命令

    ctrl+n

    显示最近命令的第一个参数

    !^

    显示最近命令的最后一个参数

    !$

    执行一条命令之后,还要执行一次与上次命令稍有出入的命令,可以使用替换,如

    git checkout dev

    再切换到 master,可以这样

    ^dev^master

    执行最近一次以!后面字母开头的命令

    !vim

    参考:

    http://www.linuxplanet.com/linuxplanet/tutorials/6639/1