Category: Linux

  • ping: unknown host localhost

    On a server, ping localhost did not work

    root@cloud1:~# ping localhost
    ping: unknown host localhost
    root@cloud1:~#
    

    /etc/hosts files was proper, had entry for localhost.

    root@cloud1:~# cat /etc/hosts | grep localhost
    127.0.0.1 localhost
    ::1 ip6-localhost ip6-loopback
    root@cloud1:~# 
    

    problem was due to /etc/nsswitch.conf file missing in the server. To fix create file

    vi /etc/nsswitch.conf
    

    Add

    # /etc/nsswitch.conf
    #
    # Example configuration of GNU Name Service Switch functionality.
    # If you have the `glibc-doc-reference' and `info' packages installed, try:
    # `info libc "Name Service Switch"' for information about this file.
    
    passwd:         compat systemd
    group:          compat systemd
    shadow:         compat
    gshadow:        files
    
    hosts:          files mdns4_minimal [NOTFOUND=return] dns myhostname
    networks:       files
    
    protocols:      db files
    services:       db files
    ethers:         db files
    rpc:            db files
    
    netgroup:       nis
    

    Set permission for the file

    chmod 644 /etc/nsswitch.conf
    
  • yum-config-manager

    yum-config-manager allows you to manage yum repositories.

    To add a repo, run

    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    

    Disable a repository

    yum-config-manager --disable rpmfusion-free-updates
    

    See yum

  • Start MongoDB from command line

    To start MongoDB from command line, run

    mongod --auth --dbpath /path/to/mongodb
    

    See MongoDB

  • Ubuntu allow a normal user to edit WiFi Connection

    Ubuntu allow a normal user to edit WiFi Connection

    When a normal user edit Wi-Fi connection on Wi-Fi Settings, they will be asked to enter administrator user pasword.

    wifi ubuntu settings

    To allow a normal user to modify network connection, you can edit the connection configuration file in folder

    /etc/NetworkManager/system-connections
    

    This file look like

    root@sok-01:/etc/NetworkManager/system-connections# cat SOK
    [connection]
    id=SOK
    uuid=982596a7-44c8-4e52-a696-6acf4daeec12
    type=wifi
    permissions=
    
    [wifi]
    mac-address=E8:DE:27:0B:39:55
    mac-address-blacklist=
    mode=infrastructure
    ssid=SOK
    
    [wifi-security]
    auth-alg=open
    key-mgmt=wpa-psk
    psk=Sup3rD0nkey
    
    [ipv4]
    dns-search=
    method=auto
    
    [ipv6]
    addr-gen-mode=stable-privacy
    dns-search=
    method=auto
    root@sok-01:/etc/NetworkManager/system-connections# 
    

    Find

    permissions=
    

    Replace with

    permissions=user:USER_NAME_HEARE:;
    

    USER_NAME_HEARE = replace this with actual username, that need to edit the network configuration.

  • Change Linux user shell

    To change shell for a user, run

    chsh --shell PATH_TO_SHELL USERNAME
    

    Example

    chsh --shell /bin/bash okadmin
    

    I will add a user, if no shell specified, it use sh as shell.

    root@lab:~# useradd okadmin
    root@lab:~# su - okadmin
    No directory, logging in with HOME=/
    $ echo $SHELL
    /bin/sh
    $ exit
    root@lab:~# chsh --shell /bin/bash okadmin
    root@lab:~# su - okadmin
    No directory, logging in with HOME=/
    okadmin@lab:/$ echo $SHELL
    /bin/bash
    okadmin@lab:/$ logout
    root@lab:~# 
    

    You can find what shell you are using now with command

    echo $SHELL
    
  • CentOS add IP Address

    To add IP address on a Linux server, first find name of your network interface. This can be done with command

    ip link
    

    In most case, it can be enp3s0 or eth0, if you have more than one network card, this may be differnt, in such case, you need to identify which network interface you need to configure the the IP address .

    Now create file

    vi /etc/sysconfig/network-scripts/ifcfg-enp3s0:1
    

    Here i used enp3s0 as network inferface name, replace it with whatever network interface name you use.

    Add following content

    DEVICE="enp3s0:1"
    IPADDR=IP_ADDR_HERE
    NETMASK=255.255.255.255
    

    IP_ADDR_HERE = replace with the IP address you need to add.

    enp3s0 = replace with actual network interface name.

    Restart network service with command

    systemctl restart network
    

    If you need to add another IP, create file with name

    INTERFACE_NAME:2
    

    for example “enp3s0:2”, you need to use same alias name in DEVICE entry (first line) in the file.

    Adding IP Range in CentOS 6

  • gonit golang replacement for monit

    gonit is a replacent for monitoring software monit. monit was removed from Debian 10 due to security issues and the devloper will not provide a patch for the security vlunerabilty in time for Debian 10 release.

    You can download gonit from

    https://github.com/bitnami/gonit

    root@ip-172-26-14-184:~# gonit status
    
    Uptime                         22h5m41s
    Last Check                     2019-08-05 08:23:57.612314368 +0000 UTC m=+79440.116643915
    Next Check                     2019-08-05 08:25:57.612314368 +0000 UTC m=+79560.116643915
    Pid                            3162
    Pid File                       /var/run/gonit.pid
    Control File                   /etc/gonit/gonitrc
    Socket File                    /var/run/gonit.sock
    Log File                       /var/log/gonit.log
    Process 'apache'
      status                                        Running
      pid                                              2020
      uptime                                       22h5m41s
      monitoring status                           monitored
    
    Process 'mysql'
      status                                        Running
      pid                                              1911
      uptime                                       22h5m41s
      monitoring status                           monitored
    
    Process 'php-fpm'
      status                                        Running
      pid                                              2014
      uptime                                       22h5m41s
      monitoring status                           monitored
    
    
    root@ip-172-26-14-184:~# 
    
  • Check if you are using 32 or 64 bit Linux

    To identify if you are using 32 bit or 64 bit Linux OS, run command arch.

    boby@sok-01:~$ arch
    x86_64
    boby@sok-01:~$
    

    You can also use uname command

    boby@sok-01:~$ uname -i
    x86_64
    boby@sok-01:~$ 
    

    Another way is to use hostnamectl command and look for “Architecture”.

    boby@sok-01:~$ hostnamectl
       Static hostname: sok-01
             Icon name: computer-desktop
               Chassis: desktop
            Machine ID: 70486772aac1410c9a8031851ab60a0d
               Boot ID: 46ff31881cca4e7090edb35f2af89a4a
      Operating System: Ubuntu 18.04.2 LTS
                Kernel: Linux 4.15.0-55-generic
          Architecture: x86-64
    boby@sok-01:~$ 
    
  • linux sound

    aplay

    sudo alsa force-reload 
    
    vi /etc/modprobe.d/alsa-base.conf
    

    Show Sound Devices

    boby@fwhlin:~$ cat /proc/asound/cards
     0 [Intel          ]: HDA-Intel - HDA Intel
                          HDA Intel at 0xfb9f8000 irq 75
     1 [HDMI           ]: HDA-Intel - HDA ATI HDMI
                          HDA ATI HDMI at 0xfbcfc000 irq 76
    boby@fwhlin:~$
    
    boby@boby-pc ~ $ cat /proc/asound/card0/codec* | grep Codec
    Codec: Realtek ALC892
    boby@boby-pc ~ $ 
    
    boby@boby-pc ~ $ cat /proc/asound/card0/pcm0c/info
    card: 0
    device: 0
    subdevice: 0
    stream: CAPTURE
    id: ALC892 Analog
    name: ALC892 Analog
    subname: subdevice #0
    class: 0
    subclass: 0
    subdevices_count: 1
    subdevices_avail: 1
    boby@boby-pc ~ $ 
    
  • aplay

    aplay is a command line sound player for ALSA soundcard driver.

    To list all available sound devices, run

    boby@sok-01:~$ aplay -l
    **** List of PLAYBACK Hardware Devices ****
    card 0: Generic [HD-Audio Generic], device 3: HDMI 0 [HDMI 0]
      Subdevices: 1/1
      Subdevice #0: subdevice #0
    card 0: Generic [HD-Audio Generic], device 7: HDMI 1 [HDMI 1]
      Subdevices: 1/1
      Subdevice #0: subdevice #0
    card 0: Generic [HD-Audio Generic], device 8: HDMI 2 [HDMI 2]
      Subdevices: 1/1
      Subdevice #0: subdevice #0
    card 0: Generic [HD-Audio Generic], device 9: HDMI 3 [HDMI 3]
      Subdevices: 1/1
      Subdevice #0: subdevice #0
    card 1: headset [Sennheiser USB headset], device 0: USB Audio [USB Audio]
      Subdevices: 1/1
      Subdevice #0: subdevice #0
    card 2: Generic_1 [HD-Audio Generic], device 0: ALC887-VD Analog [ALC887-VD Analog]
      Subdevices: 0/1
      Subdevice #0: subdevice #0
    card 2: Generic_1 [HD-Audio Generic], device 1: ALC887-VD Digital [ALC887-VD Digital]
      Subdevices: 1/1
      Subdevice #0: subdevice #0
    boby@sok-01:~$ 
    
  • Nginx vs Apache

    I recently added nginx as front end for apache. Now nginx serve static content, PHP requests are peroxided to Apache.

    Nginx frontend, Apache backend

    [root@server12 ~]# ab -n 1000 -c 100 http://netfree.netfreehost.com/
    This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    Copyright 2006 The Apache Software Foundation, http://www.apache.org/
    
    Benchmarking netfree.netfreehost.com (be patient)
    Completed 100 requests
    Completed 200 requests
    Completed 300 requests
    Completed 400 requests
    Completed 500 requests
    Completed 600 requests
    Completed 700 requests
    Completed 800 requests
    Completed 900 requests
    Finished 1000 requests
    
    
    Server Software:        nginx/1.1.0
    Server Hostname:        netfree.netfreehost.com
    Server Port:            80
    
    Document Path:          /
    Document Length:        16844 bytes
    
    Concurrency Level:      100
    Time taken for tests:   5.463353 seconds
    Complete requests:      1000
    Failed requests:        0
    Write errors:           0
    Total transferred:      17357000 bytes
    HTML transferred:       16844000 bytes
    Requests per second:    183.04 [#/sec] (mean)
    Time per request:       546.335 [ms] (mean)
    Time per request:       5.463 [ms] (mean, across all concurrent requests)
    Transfer rate:          3102.49 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    0   0.8      0       5
    Processing:    44  518  93.9    534     719
    Waiting:       44  517  94.0    533     718
    Total:         47  518  93.2    534     719
    
    Percentage of the requests served within a certain time (ms)
      50%    534
      66%    553
      75%    566
      80%    574
      90%    592
      95%    606
      98%    642
      99%    665
     100%    719 (longest request)
    [root@server12 ~]#
    

    Apache Only

    [root@server12 ~]# ab -n 1000 -c 100 http://netfree.netfreehost.com:81/
    This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    Copyright 2006 The Apache Software Foundation, http://www.apache.org/
    
    Benchmarking netfree.netfreehost.com (be patient)
    Completed 100 requests
    Completed 200 requests
    Completed 300 requests
    Completed 400 requests
    Completed 500 requests
    Completed 600 requests
    Completed 700 requests
    Completed 800 requests
    Completed 900 requests
    Finished 1000 requests
    
    
    Server Software:        Apache/2.2.3
    Server Hostname:        netfree.netfreehost.com
    Server Port:            81
    
    Document Path:          /
    Document Length:        16844 bytes
    
    Concurrency Level:      100
    Time taken for tests:   7.102347 seconds
    Complete requests:      1000
    Failed requests:        1
       (Connect: 0, Length: 1, Exceptions: 0)
    Write errors:           0
    Total transferred:      17351384 bytes
    HTML transferred:       16827683 bytes
    Requests per second:    140.80 [#/sec] (mean)
    Time per request:       710.235 [ms] (mean)
    Time per request:       7.102 [ms] (mean, across all concurrent requests)
    Transfer rate:          2385.69 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    0   1.1      0       6
    Processing:    34  676 174.9    669    1261
    Waiting:       32  675 175.0    668    1260
    Total:         34  676 174.4    669    1261
    
    Percentage of the requests served within a certain time (ms)
      50%    669
      66%    696
      75%    732
      80%    754
      90%    893
      95%    974
      98%   1081
      99%   1128
     100%   1261 (longest request)
    [root@server12 ~]#
    

    See Apache, Nginx