Category: Linux

  • tmux

    To attach to a specific session, run

    tmux a -t SESSION_ID
    
    CTRL+B % Split window vertically
    CTRL+B “ Split window horizontally
    CTRL+B c Create new window
    CTRL+B d Detach tmux session
    CTRL+B $ Rename session
    CTRL+B n Next window.
    CTRL+B p Previous window.
    CTRL+B arrow keys switch between panes.

    .tmux.conf

    .tmux.conf file is used to configure tmux

    Here is the .tmux.conf i use

    https://gist.github.com/serverok/f8f729e2c22a5ab1fa6b0d82765bcddc

    If you change this file, you need to restart the terminal or run

    tmux source-file .tmux.conf
    

    Scroll Mode in tmux

    To scroll, you need to enter scroll mode by pressing

    CTRL + B, then press PageUp key.
    CTRL + B, then [
    

    To exit scroll mode, press q

  • apt doesn’t support architecture i386

    After adding facebook hhvm repository on Ubuntu 16.04, i get following error when running “apt update” command.

    root@backup:~# apt-get update
    Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
    Hit:2 http://us.archive.ubuntu.com/ubuntu xenial InRelease                                                                                                       
    Get:3 http://us.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]                                                                                      
    Hit:4 http://ftp.osuosl.org/pub/mariadb/repo/10.1/ubuntu xenial InRelease                                                                                        
    Ign:5 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 InRelease                                                                                        
    Get:6 http://us.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]                                                                                    
    Hit:7 http://ppa.launchpad.net/ondrej/php/ubuntu xenial InRelease                   
    Hit:8 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 Release             
    Ign:10 https://pkg.jenkins.io/debian-stable binary/ InRelease
    Hit:11 https://download.docker.com/linux/ubuntu xenial InRelease
    Hit:12 https://pkg.jenkins.io/debian-stable binary/ Release
    Get:13 https://dl.hhvm.com/ubuntu xenial InRelease [2,411 B]
    Get:15 https://dl.hhvm.com/ubuntu xenial/main amd64 Packages [2,244 B]
    Fetched 311 kB in 1s (283 kB/s)
    Reading package lists... Done
    N: Skipping acquire of configured file 'main/binary-i386/Packages' as repository 'https://dl.hhvm.com/ubuntu xenial InRelease' doesn't support architecture 'i386'
    root@backup:~#
    

    This is fixed by editing

    vi /etc/apt/sources.list
    

    Find

    deb https://dl.hhvm.com/ubuntu xenial main
    

    Replace with

    deb [arch=amd64] https://dl.hhvm.com/ubuntu xenial main
    
  • add-apt-repository

    add-apt-repository command can be used to add repository in ubuntu.

    To install, run

    apt-get install software-properties-common python-software-properties
    

    Example

    apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xB4112585D386EB94
    add-apt-repository https://dl.hhvm.com/ubuntu
    
  • rsync

    rsync command with exclude option, full server backup to a remote server.

    rsync -avz --progress --human-readable "-e ssh -p 3333" --exclude /backup --exclude /proc --exclude /sys / root@REMOTE_SERVER_IP:/backup/serverX/
    

    Transfer a file

    I normally use scp to do single file transfer. It may be better to it with rsync as you will be able to resume.

    rsync -P -e ssh root@SERVER_IP:/path/to/file.tar.gz /local/path/

    -P == –partial –progress

    If you use non standard SSH port, then replace -e ssh with “-e ssh -p PORT_NUMBER”.

    Example

    boby@hon-pc-01:~$ rsync -P "-e ssh -p 3333" root@s12:/home/cloud-architect.tar.bz2 /backup/learn/cloud-architect.tar.bz2
      3,120,159,768 100%  995.98kB/s    0:36:27 (xfr#1, to-chk=0/1)
    boby@hon-pc-01:~$ 
    

    See backup

  • smartctl

    To check hard disk for errors, run

    /usr/sbin/smartctl -q errorsonly -H -l selftest -l error /dev/sda
    

    Here is an example of HDD with errors

    # smartctl -q errorsonly -H -l selftest -l error /dev/sda
    ATA Error Count: 8259 (device log contains only the most recent five errors)
    Error 8259 occurred at disk power-on lifetime: 8324 hours (346 days + 20 hours)
    Error 8258 occurred at disk power-on lifetime: 8324 hours (346 days + 20 hours)
    Error 8257 occurred at disk power-on lifetime: 8324 hours (346 days + 20 hours)
    Error 8256 occurred at disk power-on lifetime: 8324 hours (346 days + 20 hours)
    Error 8255 occurred at disk power-on lifetime: 8324 hours (346 days + 20 hours)
    
    #
    

    To get detailed, result, run

    smartctl -a -d ata /dev/sda
    

    hdd

  • gzip

    Enable Gzip in Amazon Linux
    How to enable gzip on Plesk Nginx

    To check if a web site have gzip enabled, run

    curl -I -H 'Accept-Encoding: gzip,deflate' https://YOUR_DOMAIN.EXTN
    

    When you have no GZIP enabled, you will see error as follows

    boby@hon-pc-01:~$ curl -I -H 'Accept-Encoding: gzip,deflate' https://serverok.in
    curl: (7) Failed to connect to serverok.in port 443: Connection refused
    boby@hon-pc-01:~$ 
    

    With gzip enabled, you see something like following

    boby@hon-pc-01:~$ curl -I -H 'Accept-Encoding: gzip,deflate' https://serverok.in
    HTTP/1.1 200 OK
    Server: nginx/1.10.3 (Ubuntu)
    Date: Sat, 23 Dec 2017 16:53:49 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    Link: ; rel="https://api.w.org/"
    Link: ; rel=shortlink
    Strict-Transport-Security: max-age=31536000; includeSubDomains
    Content-Encoding: gzip
    
    boby@hon-pc-01:~$ 
    

    To enable gzip on nginx web server, i added following to server block

    gzip          on;
    gzip_comp_level     5;
    gzip_min_length     256;
    gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/rss+xml
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-font-opentype
        application/x-font-truetype
        application/x-javascript
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/eot
        font/opentype
        font/otf
        image/svg+xml
        image/x-icon
        image/vnd.microsoft.icon
        text/css
        text/plain
        text/javascript
        text/x-component;
    gzip_disable  "MSIE [1-6]\.(?!.*SV1)";
    
  • systemctl

    Service files

    To list all services run

    systemctl -a
    

    List all enabled services

    systemctl list-unit-files | grep enabled
    

    See all running services

    systemctl | grep running
    

    See see dependency for a package, run

    systemctl list-dependencies --reverse PACKAGE_NAME
    

    systemctl list-dependencies

    Service file location

    service files are located in /usr/lib/systemd/system

    [root@web-0001 system]# pwd
    /usr/lib/systemd/system
    [root@web-0001 system]# ls -l | grep php
    -rw-r--r--  1 root root  507 Jul  6  2017 php56-php-fpm.service
    -rw-r--r--  1 root root  502 Jan  3 06:55 php72-php-fpm.service
    -rw-r--r--  1 root root  467 Jun  7  2017 php-fpm.service
    [root@web-0001 system]# 
    

    Systemd unit types

    systemctl -t help
    systemctl -t timer
    systemctl -t service
    
  • Install shadowsocks on Ubuntu

    Install ShadowSocks server on Debian 10
    install ShadowSocks client in Ubuntu 18.04

    On the Server, install shadowsocks with

    apt install python-pip python-m2crypto
    pip install shadowsocks

    Create config file for server

    vi /etc/shadowsocks.json

    Add

    {
    "server":"SERVER_IP",
    "server_port":8044,
    "local_port":0,
    "password":"PASSWORD",
    "timeout":600,
    "method":"aes-256-cfb"
    }

    Replace SERVER_IP and PASSWORD in above config. Start server with

    ssserver -c /etc/shadowsocks.json -d start

    Example

    root@backup:~# ssserver -c /etc/shadowsocks.json -d start
    INFO: loading config from /etc/shadowsocks.json
    2017-12-23 03:46:57 INFO     loading libcrypto from libcrypto.so.1.1
    started
    root@backup:~# 

    Shadowsocks client

    Shadowsocks have clients for different operating systems. Some are GUI. On Ubuntu, i install CLI version, that is part of shadowsocks server. To install run

    apt install shadowsocks

    Now start the sslocal program with command

    sudo ss-local -s server_address -p server_port -l local_port -k password -m encryption_method

    Example

    sudo /usr/bin/sslocal -s 188.11.131.92 -p 4545 -l 7070 -k KVhXq@JkNuH4h -m aes-256-cfb

    Now you will be able to configure your browser to use socks proxy listing at 127.0.0.1:7070

  • Monitor redis with redis-cli

    On Ununtu/Debian, install it with

    apt -y install redis-tools
    

    To get stats, run

    redis-cli --stat
    

    If your redis on differnt sevrer or use non standard port, then use

    redis-cli -h HOSTNAME -p PORT --stat
    

    Example

    root@ip-10-0-0-31:~# redis-cli -h news24redis.cylrbg.0001.use1.cache.amazonaws.com --stat
    ------- data ------ --------------------- load -------------------- - child -
    keys       mem      clients blocked requests            connections          
    9287       20.58M   27      0       179271 (+0)         1208        
    9287       20.62M   27      0       179277 (+6)         1208        
    9288       20.67M   28      0       179377 (+100)       1209        
    9288       20.64M   28      0       179494 (+117)       1209        
    9292       20.71M   28      0       179823 (+329)       1210        
    9292       20.68M   28      0       179969 (+146)       1211        
    9298       20.74M   29      0       180033 (+64)        1212        
    9311       20.67M   28      0       180162 (+129)       1214        
    9314       20.69M   29      0       180198 (+36)        1215        
    9318       20.80M   30      0       180275 (+77)        1216        
    9318       20.68M   30      0       180280 (+5)         1216        
    9318       20.82M   31      0       180307 (+27)        1218        
    9319       20.74M   31      0       180321 (+14)        1218        
    9322       20.70M   29      0       180350 (+29)        1219        
    9324       20.80M   30      0       180377 (+27)        1220        
    9366       20.90M   30      0       180724 (+347)       1221        
    9368       20.77M   29      0       181086 (+362)       1221        
    9368       20.73M   27      0       181133 (+47)        1221        
    9372       20.73M   27      0       181487 (+354)       1222        
    9380       20.73M   27      0       181592 (+105)       1223        
    ------- data ------ --------------------- load -------------------- - child -
    keys       mem      clients blocked requests            connections          
    9384       20.76M   28      0       181639 (+47)        1224        
    9391       20.77M   28      0       181718 (+79)        1224        
    9391       20.79M   27      0       181731 (+13)        1224        
    9391       20.67M   25      0       181771 (+40)        1224        
    9391       20.67M   25      0       181780 (+9)         1224        
    9391       20.67M   25      0       181849 (+69)        1224        
    ^C
    root@ip-10-0-0-31:~# 
    

    INFO

    redis-cli INFO
    

    If you want to see info about clients, run

    redis-cli INFO clients
    

    Monitor

    To see what redis is doing, run

    redis-cli monitor
    

    If your redis is running on remote server, then use

    redis-cli -h REMOTE_SERVER_IP monitor
    
  • Connect to redis from PHP

    To connect to redis, first install php-redis

    apt install php-redis
    

    Now create a php file with following code

    root@ip-10-0-0-31:/var/www/html# cat 2.php 
    connect('news24redis.cylrbg.serverok.use1.cache.amazonaws.com', 6379); 
    
    echo "Connection to server sucessfully"; 
    
    echo "Server is running: ".$redis->ping(); 
    root@ip-10-0-0-31:/var/www/html# 
    

    redis

  • Install Redis PHP module in Ubuntu 16.04

    To install redis php module in Ubuntu 16.04, run

    apt -y install php-redis
    

    If you are using Apache, restart Apache.

    systemctl restart apache2
    

    If php-fpm restart

    systemctl restart php7.0-fpm
    

    You will see following in your phpinfo()

    php redis

  • redis

    Install Redis

    Install latest Redis on Ubuntu/Debian Server
    Install Redis on Plesk Debian Server
    Install Redis from Source

    How Tos

    Installing Redis PHP module CentOS 7
    Connect to redis from PHP
    Monitor redis with redis-cli
    redis commander – Web GUI for Redis
    How to find Redis data directory
    How to flush Redis cache
    Redis error currently not able to persist on disk

    Checking if Redis works

    # telnet 127.0.0.1 6379
    set testkey 10
    get testkey
    

    Example

    root@ip-10-0-0-31:~# telnet news24redis.cylrbg.0001.use1.cache.amazonaws.com 6379
    Trying 10.0.0.223...
    Connected to news24redis.cylrbg.0001.use1.cache.amazonaws.com.
    Escape character is '^]'.
    set testkey 10
    +OK
    get testkey
    $2
    10
    quit
    +OK
    Connection closed by foreign host.
    root@ip-10-0-0-31:~# 
    

    Count Number of Keys

    To count number of keys, use

    DBSIZE
    

    Example

    root@ip-10-0-0-31:~# telnet news24redis.cylrbg.0001.use1.cache.amazonaws.com 6379
    Trying 10.0.0.223...
    Connected to news24redis.cylrbg.0001.use1.cache.amazonaws.com.
    Escape character is '^]'.
    DBSIZE
    :4114
    

    Here DBSIZE returned 4114, that means you have that much key/value stored in redis.

    Flush ALl

    To flush all keys, use

    redis-cli flushall