Friday, June 25, 2021

Shell to check process

=== Test if a port is open============
nc -z localhost 6000 || echo "no tunnel open"

====Cron table check================

We can use ps -aux | grep "<our command>" | sed '$ d' to check whether the connection is established or not. Based on this our script could be:

#!/bin/bash
SSH_COMMAND="ssh user@host -fTN -R 2222:127.0.0.1:22 -i $HOME/.ssh/id_rsa"
if [[ -z $(ps -aux | grep "$SSH_COMMAND" | sed '$ d') ]]
then exec $SSH_COMMAND
fi

Call this script my_autossh, place it in ~/bin and make it executable. Then run crontab -e and add the following job:

* * * * * $HOME/bin/my_autossh
If you do not want to use Cron, modify the scrip my_autossh in this way:
#!/bin/bash
SSH_COMMAND="ssh user@host -fTN -R 2222:127.0.0.1:22 -i $HOME/.ssh/id_rsa"
while true; do
    if [[ -z $(ps -aux | grep "$SSH_COMMAND" | sed '$ d') ]]
    then eval $SSH_COMMAND
    else sleep 60
    fi
done

And use nohup to push it into the background:
nohup my_autossh >/dev/null 2>&1 &

Linux KVM

 ======bridge========
brctl addif br0 p3p2
brctl addif br1 em1
ifdown br1
ifup br1

========create VM=======

Convert disk:
qemu-img convert -O qcow2 venice-disk1.vmdk psm.qcow2

Booting from an existing qcow2 image
virt-install \
  --name MyImportedVM \
  --description "Imported virtual machine" \
  --graphics vnc,listen=0.0.0.0 \
  --noautoconsole \  
  --os-type=linux \
  --memory 8192 \
  --vcpus=4 \
  --disk path=/var/lib/libvirt/images/importedVM.qcow2,bus=virtio\
  --boot hd \
  --network bridge:br0,model=virtio


Building a new Windows VM
Windows can be difficult on KVM because it doesn't ship with Virtio drivers. Using the non-virtio disk and network emulation is comparatively slow. Things are made harder because virt-install doesn't let you use --cdrom twice. To mount two iso files at once (so virtio drivers can be installed), use the --disk argument with device=cdrom. Also be sure to use bus=ide so Windows can read it before installing the virtio drivers.

The virtio drivers can be downloaded here

virt-install \
  --name Server2019 \
  --description "Windows Server 2019 Template" \
  --os-type=windows \
  --memory 8192 \
  --vcpus=4 \
  --disk path=/var/lib/libvirt/images/windows2019.qcow2,bus=virtio,size=30 \
  --disk /var/lib/libvirt/isos/WindowsServer2019StandardCore_1909.iso,device=cdrom,bus=ide \
  --disk /var/lib/libvirt/isos/virtio-win.iso,device=cdrom,bus=ide \
  --graphics vnc,listen=0.0.0.0 \
  --noautoconsole \
  --network bridge=br0,model=virtio


When installing the OS through your VNC viewer, no drives will show up.
Click the "Load Driver" button, and browse to the virtio iso that was mounted.
Expand the viostor folder and choose your OS. Click the amd64 subdirectory. Click OK.
RedHat VirtIO SCSI Controller should show up, click Next
Finish the install,Once the install finishes the server will turn off.


[root@localhost ~]# virsh console study01  
[root@localhost ~]# virsh start study01  
[root@localhost ~]# virsh shutdown study01  

#force stop
[root@localhost ~]# virsh destroy study01  

# destroy but keep the qcow2 virtual disk
[root@localhost ~]# virsh undefine study01  

[root@localhost ~]# virsh autostart study01  
[root@localhost ~]# virsh autostart --disable study01 
[root@localhost ~]# virsh suspend study01 
[root@localhost ~]# virsh resume study01 

virsh net-edit default
virsh net-edit somenet
virsh net-destroy somenet
virsh net-start somenet


virt-install   --name PSM   --description "PNSO PSM"   --graphics vnc,listen=0.0.0.0   --noautoconsole   --os-type=linux   --memory 8192   --vcpus=8   --disk path=/root/psm/psm.qcow2,bus=virtio  --boot hd   --network bridge:br0,model=virtio

Linux firewall related settings

============iptables============
#!/bin/bash
iptables -F
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -i lan0 -j ACCEPT
iptables -A INPUT -i ens21f0 -j ACCEPT
iptables -A INPUT -i ens21f1 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

=============NAT via firewall-cmd=================

vim /etc/sysctl.conf 
net.ipv4.ip_forward = 1
sysctl -p

#define zone
firewall-cmd --permanent --zone=external --change-interface=eth0 
firewall-cmd --permanent --zone=internal --change-interface=eth1

#set NAT masquerade
firewall-cmd --zone=external --add-masquerade --permanent

#NAT rules
firewall-cmd --permanent --direct --passthrough ipv4 -t nat -I POSTROUTING -o em1 -j MASQUERADE -s 172.16.10.0/24
firewall-cmd --reload

#Check:
[root@620 ~]# firewall-cmd  --direct --get-passthroughs ipv4 -t nat -I POSTROUTING -o em1 -j MASQUERADE -s 172.16.10.0/24


Linux network configuration


SSH tunnel:
#!/bin/bash
ssh -fNT -R 62002:127.0.0.1:22 root@blablabla -p 10022

====================================

NIC Parameter:
ethtool -s p2p2 speed 10000 duplex full autoneg on

====================================

Link up/down:
ip addr add 50.1.1.1/29 dev p2p1
ip link set dev p2p1 up

====================================

Autossh:
/usr/bin/autossh -M 0 -f -NT -R 62007:10.2.4.207:22 root@jumphost -p 10022 

===================================
Sample ifcfg

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=172.16.10.21
NETMASK=255.255.255.0
GATEWAY=172.16.10.1
DNS1=8.8.8.8

==================================

UUID:
[root@sysadmins ~]# nmcli connection 
 NAME   UUID                                  TYPE      DEVICE 
 ens32  97f8ee48-fa54-4a91-b20f-17144533b9c0  ethernet  ens32  
 ens33  1633af9e-dcfd-4612-899c-479ea028140f  ethernet  --     

=================================
route add

route add -net 10.255.255.0/24 gw 172.16.10.3
route add -net 10.10.255.0/24 gw 172.16.10.3