Tag: linux

  • virsh list

    To list all VM on a KVM server, run

    virsh list

    Example

    root@s3-m12:~# virsh list
     Id    Name                           State
    ----------------------------------------------------
     6     foo                            running
    
    root@s3-m12:~# 

    To view all Virtual machines including stopped VM’s, use –all option.

    root@mail:~# virsh list --all
     Id   Name       State
    ---------------------------
     1    iredmail   running
     -    win10      shut off
    
    root@mail:~# 

    To see all network

    root@server1:~# virsh net-list --all
     Name      State    Autostart   Persistent
    --------------------------------------------
     Bridge0   active   yes         yes
    
    root@server1:~#

    See virsh

  • virsh nodeinfo

    To see information about a KVM node, run

    virsh nodeinfo
    

    Example

    root@s3-m12:~# virsh nodeinfo
    CPU model:           x86_64
    CPU(s):              8
    CPU frequency:       1861 MHz
    CPU socket(s):       1
    Core(s) per socket:  4
    Thread(s) per core:  2
    NUMA cell(s):        1
    Memory size:         16423092 KiB
    
    root@s3-m12:~# 
    

    See virsh

  • virsh

    virsh command is used to manage KVM vps. It is part of libvirt package.

    http://libvirt.org

    To list KVM virtual machines, run

    virsh list

    Start a VM

    virsh start VM_ID

    To shutdown a VM, run

    virsh shutdown VM_ID

    To delete a VM, run

    virsh shutdown VM_ID

    Connect to a Remote Server

    virsh --connect qemu+ssh://[email protected]/system

    You need SSH Key based authentication setup.

    List All VM

    boby@fwhlin:~ $ virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     -     vm1                            shut off
    
    boby@fwhlin:~ $

    List All Running VM

    [root@server70 ~]# virsh list
     Id    Name                           State
    ----------------------------------------------------
     1     vm10                           running
     3     vm12                           running
     5     vm15                           running
     7     vm17                           running
     12    vm16                           running
    
    [root@server70 ~]#

    Get Info About a VM

    virsh dominfo VM_NAME

    example

    [root@server70 ~]# virsh dominfo vm18
    Id:             10
    Name:           vm18
    UUID:           b6c3a3d8-d96a-c3fa-1fad-04aff28e476a
    OS Type:        hvm
    State:          running
    CPU(s):         2
    CPU time:       422.9s
    Max memory:     524288 KiB
    Used memory:    524288 KiB
    Persistent:     yes
    Autostart:      disable
    Managed save:   no
    Security model: none
    Security DOI:   0
    
    [root@server70 ~]#

    Change the Memory of a VM

    virsh dumpxml vm18 > vm18.xml
    virsh shutdown vm18
    vi vm18.xml

    Find

    <memory unit='KiB'>524288</memor<memory unit='KiB'>524288</memory>
    <currentMemory unit='KiB'>524288</currentMemory>

    To change Memory to 1 GB, Replace it with

    <memory unit='KiB'>1048576</memory>
    <currentMemory unit='KiB'>1048576</currentMemory>

    Load configuration

    virsh define vm18.xml 

    Example

    [root@server70 ~]# virsh define vm18.xml
    Domain vm18 defined from vm18.xml
    
    [root@server70 ~]#

    Start VM

    virsh start vm18
    [root@server70 ~]# virsh start vm18
    Domain vm18 started
    
    [root@server70 ~]#

    See kvm

  • LXD container snapshot and restore

    lxc allow you to take snapshot, restore snapshot of a container.

    Here we create a snapshot, then delete the /etc folder from the container. Now the container is useless. Restore the container from snapshot to get it work again.

    root@UNUSED:~# lxc snapshot my-vm-2 my-vm-2-snap1
    root@UNUSED:~# lxc exec my-vm-2 -- rm -rf /etc
    root@UNUSED:~# lxc exec my-vm-2 -- bash
    I have no name!@my-vm-2:~# ls -l / | grep etc
    I have no name!@my-vm-2:~# exit
    root@UNUSED:~# lxc restore my-vm-2 my-vm-2-snap1
    root@UNUSED:~# lxc exec my-vm-2 -- bash
    root@my-vm-2:~# ls -l / | grep etc
    drwxr-xr-x  89 root   root    4096 Aug 27 07:06 etc
    root@my-vm-2:~# exit
    root@UNUSED:~#
    
  • Install LXD on Ubuntu

    To create LXD containers, run

    lxc launch ubuntu:22.04 VM-NAME-HERE
    lxc launch ubuntu:24.04 VM-NAME-HERE
    lxc launch images:almalinux/8 VM-NAME-HERE
    lxc launch images:almalinux/9 VM-NAME-HERE
    lxc launch images:amazonlinux/2023 VM-NAME-HERE
    lxc launch images:archlinux VM-NAME-HERE
    lxc launch images:centos/9-Stream VM-NAME-HERE
    lxc launch images:debian/11 VM-NAME-HERE
    lxc launch images:debian/12 VM-NAME-HERE
    lxc launch images:fedora/39 VM-NAME-HERE
    lxc launch images:fedora/40 VM-NAME-HERE
    lxc launch images:oracle/8 VM-NAME-HERE
    lxc launch images:oracle/9 VM-NAME-HERE

    LXD is a tool to manage LXC (Linux Containers). lxd allow you to create and manage lightweight Linux containers, it is like docker, but more like an actual virtual machine. LXD is more user-friendly than using LXC directly, has more features like clustering of servers.

    On Ubuntu/Debian, run

    apt install -y lxd lxd-client

    Before you start using lxd, run

    lxd init --auto --storage-backend=dir

    Or

    lxd init

    Just press enter for all questions. Defaults should work. lxd is a demon, only used one time during “lxd init”. After this, you will be using lxc command for everything.

    Example

    root@b24:~# lxd init
    Would you like to use LXD clustering? (yes/no) [default=no]: 
    Do you want to configure a new storage pool? (yes/no) [default=yes]: 
    Name of the new storage pool [default=default]: 
    Name of the storage backend to use (dir, lvm, ceph, btrfs) [default=btrfs]: 
    Create a new BTRFS pool? (yes/no) [default=yes]: 
    Would you like to use an existing empty block device (e.g. a disk or partition)? (yes/no) [default=no]: 
    Size in GB of the new loop device (1GB minimum) [default=30GB]: 
    Would you like to connect to a MAAS server? (yes/no) [default=no]: 
    Would you like to create a new local network bridge? (yes/no) [default=yes]: 
    What should the new bridge be called? [default=lxdbr0]: 
    What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: 
    What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: none
    Would you like the LXD server to be available over the network? (yes/no) [default=no]: 
    Would you like stale cached images to be updated automatically? (yes/no) [default=yes] 
    Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]: 
    root@b24:~# 

    When it ask for IPv6, I selected “none”, everything else is the default.

    To create a container, run

    lxc launch ubuntu:22.04 vm-1

    Create CentOS container

    lxc launch images:centos/7 vm-2

    Create Debian 10 container

    lxc launch images:debian/10 vm-3

    To list all containers, run

    lxc ls
    lxc list containers

    To stop a VM, run

    lxc stop VM-NAME

    To start VM,

    lxc start VM-NAME

    To see information about a container

    lxc info VM-NAME

    Folders

    /var/snap/lxd/

    To see storage, run

    lxc storage ls

    All containers share the following disk image.

    /var/snap/lxd/common/lxd/disks/default.img
  • blkid

    To list UUID for all your storage devices, run ”’blkid -c /dev/null”’

    blkid - locate/print block device attributes
    

    Example

    boby@bizhat ~ $ sudo blkid -c /dev/null
    [sudo] password for boby: 
    /dev/sda1: UUID="c17be3a7-dd65-448a-8930-b9d0369c6575" TYPE="ext4" 
    /dev/sda2: UUID="c72e257f-b210-4f81-b1de-e5130d61454e" TYPE="ext4" 
    /dev/sda3: UUID="ab289ee4-fa14-4da0-af60-a9c4360327b2" TYPE="swap" 
    /dev/sda5: UUID="b1ebdc91-64a8-46b0-a9ee-8e71c659bba0" TYPE="ext4" 
    /dev/sda8: LABEL="Drive J" UUID="01CE4B5BC914BE10" TYPE="ntfs" 
    /dev/sdb1: UUID="67fd4ee7-1123-433c-aab4-ed877051976d" TYPE="ext4" 
    /dev/sdb5: UUID="28f4e904-ecfa-4a9f-b632-60c58c826cfc" TYPE="swap" 
    /dev/sdb6: UUID="19ca47a8-d894-44ff-a7af-77fa16635e98" TYPE="ext4" 
    /dev/sdc1: LABEL="Drive E" UUID="709409F99409C298" TYPE="ntfs" 
    /dev/sdc2: LABEL="Drive C" UUID="BC108B2C108AECA8" TYPE="ntfs" 
    /dev/sdc5: LABEL="Drive D" UUID="2ED4A94DD4A917DB" TYPE="ntfs" 
    /dev/sdc6: LABEL="New Volume" UUID="FEA6945FA69419EB" TYPE="ntfs" 
    /dev/sdc7: LABEL="Drive G" UUID="A4A08A44A08A1CC4" TYPE="ntfs" 
    /dev/sr1: LABEL="Idea Net Setter" TYPE="iso9660" 
    boby@bizhat ~ $ 
    

    To mount /dev/sda5, i added following to /etc/fstab

    UUID=b1ebdc91-64a8-46b0-a9ee-8e71c659bba0 /home/boby/store ext4  defaults  0 2
    

    It is same as

    /dev/sda5 /home/boby/store ext4  defaults  0 2
    

    Linux Commands > hdd

  • atop

    The program atop is an interactive monitor to view the load on a Linux system.

    To install on Ubuntu/Debian

    apt-get install atop

    On CentOS/RHEL

    yum install atop

    On CentOS, you will need epel repo enabled.

    View Historical Data

    One of the powerful features of atop is its ability to record and store detailed snapshots of system performance, allowing you to go back and review exactly how resources were used at any given time. This is invaluable for diagnosing past performance issues, investigating system load patterns, and understanding resource usage over time. With atop’s logging capabilities, you can access historical data quickly and analyze it interactively, just as if you were viewing real-time activity.

    Atop stores its log files by default in the /var/log/atop/ directory, with filenames that include the date (formatted as atop_YYYYMMDD), making it easy to locate and access historical performance data for specific days.

    To view past data with atop, use the replay mode by running the following command:

    atop -r

    This will open today’s log file in replay mode. If you need older logs, you can specify the log file with -r option.

    atop -r /var/log/atop/atop_YYYYMMDD

    Replace YYYYMMDD with the desired date to load the log file for that day.

    Once in replay mode, you can navigate through the data using keyboard shortcuts t and T to jump forward or backward in 10-minute intervals, and b or e to jump to the beginning or end of the log. This lets you explore historical resource usage interactively, just as you would with real-time data.

    Back to Linux Commands

  • Using apt-get to manage software

    apt-get used to manage packages in Debian/Ubuntu server.

    Install A Package

    apt-get install PKG_NAME
    

    Uninstall a software (keep configuration file)

    apt-get remove PKG_NAME
    

    Uninstall a software and its configuration files

    apt-get --purge remove PKG_NAME
    

    To upgrade a software

    apt-get upgrade PKG_NAME
    

    Update Package Info

    apt-get update
    

    Remove Unwanted Programs

    apt-get autoremove
    

    Back to apt

  • PHP script to monitor exim mail queue

    When you run exim mail server, it is good to keep an eye on number of emails in mail queue. here is a PHP script that will check number of emails in queue, if it exceed pre-set number, it will email you.

    Create file

    mkdir /usr/serverok/
    vi /usr/serverok/mail_q_monitor.php
    

    Add following content.

     $alertOn) {
        $hostname = exec('/bin/hostname');
        $subject = 'Mail queue alert on ' . $hostname;
        $mail_text = 'Mail queue on server ' . $hostname . ' have ' . $num_mails . ' mails';
        mail($adminEmail, $subject, $mail_text);
    }
    

    In this case, if mails in queue exceeded 100, you get email.

    $alertOn = 100;
    

    You can change 100 to whatever number you need.

    $adminEmail = "[email protected]";
    

    Replace [email protected] with your email address.

    Set script to run every 10 minutes using cronjob

    */10 * * * * /usr/local/bin/php /usr/serverok/mail_q_monitor.php >/dev/null 2>&1