Рубрики
Bash Linux

Restore executable permission to Chmod command in Linux

Problem: chmod command not executable anymore.

chmod -x $(which chmod)

# result
chmod +x $(which chmod)
bash: /usr/bin/chmod: Permission denied

Solution №1
Copy the contents of the chmod binary to other working binaries

# Backup original executable file
cp /usr/bin/mkdir /usr/bin/mkdir.bak

# Copy contents of chmod command to mkdir
cat /usr/bin/chmod > /usr/bin/mkdir

# Set executable permissions with "new" chmod file
mkdir +x /usr/bin/chmod

# Restore original chmod name
mv /usr/bin/mkdir /usr/bin/chmod

# Restore original mkdir file
mv /usr/bin/mkdir.bak /usr/bin/mkdir

# Check if restored chmod is executable now
ls -lah $(which chmod)

Solution №2
Using the cp command with only attributes parameter

cp --attributes-only --preserve=mode /proc/self/exe /usr/bin/chmod

Solution №3
Use setfacl command

setfacl -m u::rx /usr/bin/chmod

# If command setfact not found install packet
apt install acl

Solution №3
Using rsync

rsync /usr/bin/chmod /tmp/chmod --chmod=ugo+x

mv /tmp/chmod /usr/bin

Solution №4
Using Busybox

busybox chmod +x /usr/bin/chmod

Рубрики
File system Linux

Extend root partition on CentOS7 on a fly

Problem:
no space left on » / » (centos-root).
0 bytes left.


Solution:
Rescan device
Create a partition (sda3)
Extend VG without meta backup
Extend LV without meta backup
xfs.grow root partition.

echo «1» > /sys/class/block/sda/device/rescan

# if add new disk,
# or if reboot is available expand partiton with gparted
# or fdisk by deleting and creating new partition
echo "1" > /sys/class/block/sda/device/rescan

fdisk /dev/sda
  ### create sda3 partition
  ### n
  ### primary 
  ### (yyy)
  
# check VG name
vgdisplay

# 'centos' is a VG name in this example: VG Name centos
vgextend -A n centos /dev/sda3

# Get lv volume Example: /dev/mapper/centos_centos-root
blkid

# Extend volume to 100% of free space in VG
lvextend -A n -l 100%VG /dev/mapper/centos-root

# Grow filesystem to max available space
xfs_growfs /dev/mapper/centos-root

# verify free space on " / "
df -h


Рубрики
Linux samba

Samba

Install and configure samba on CentOS7:

Install samba and tools:

yum install -y samba samba-client samba-common

firewalld add service (if using firewalld):
firewall-cmd --permanent --zone=public --add-service=samba
firewall-cmd --reload

backup original samba config:
cp /etc/samba/smb.conf /etc/samba/smb.conf.orig

Add group and Linux user:
groupadd smbgrp
useradd smbuser
passwd smbuser


Add samba user to samba group and change samba user passwd:
usermod smbuser -aG smbgrp
smbpasswd -a


Change samba share permissions:
chmod -R 0770 /samba/share
chown -R root:smbgrp /samba/share


Change SELinux context:

chcon -t samba_share /samba/share

Add samba share to config file:
vi /etc/samba/smb.conf

[data]
comment = File Server Share
path = /data
valid users = @smbgrp
guest ok = no
writable = yes
browsable = yes


Execute the test command to check if any errors acquired:
testparm

Enable smb and nmb and start services:
systemctl enable smb.service
systemctl enable nmb.service
systemctl start smb.service
systemctl start nmb.service



Done.
Mount it on remote PC with smbuser credentials.

Рубрики
Ansible Linux

Ansible

Install ansible on CentOS7:

yum install epel-release
yum install -y ansible

Standard directory structure:
[root@lab-1 ansible_playbooks]# tree
.
├── fs_servers.yml
├── inventories
│   ├── production
│   │   ├── group_vars
│   │   └── host_vars
│   └── staging
│   ├── group_vars
│   ├── hosts
│   └── host_vars
├── roles
│   └── fileserver
│   ├── files
│   ├── tasks
│   ├── templates
│   └── vars
└── site.yml

When using git with GitHub create (touch) .gitkeep file in each directory to sync the directory structure.

To provide ssh_key use next parameter in the hosts file:
ansible_ssh_private_key_file=/root/.ssh/id_rsa

List all information about ansible inventory:
[root@lab-1 ansible_playbooks]# ansible-inventory --list -i ./inventories/staging/hosts

Tree-like inventory info:
ansible-inventory --graph -i ./inventories/staging/hosts

Check information of the server:
[root@lab-1 ansible_playbooks]# ansible -i ./inventories/staging/hosts staging_lab_servers -m setup

Execute shell command on remote servers:
ansible -i ./inventories/staging/hosts staging_lab_servers -m shell -a "uptime"

Copy file from source VM to all servers:
[root@lab-1 ansible_playbooks]# ansible -i ./inventories/staging/hosts staging_lab_servers -m copy -a "src=/home/file.txt dest=/home mode=777" -b

Delete file from all servers:
[root@lab-1 ansible_playbooks]#ansible -i ./inventories/staging/hosts staging_lab_servers -m file -a "path=/home/file.txt state=absent"

Download file from URL to all servers:
[root@lab-1 ansible_playbooks]#ansible -i ./inventories/staging/hosts staging_lab_servers -m get_url -a "url=https://ya.ru dest=/home" -b

Make Get request to URL:
[root@lab-1 ansible_playbooks]# ansible -i ./inventories/staging/hosts staging_lab_servers -m uri -a "url=https://ya.ru"

Make Get request with content to URL:
[root@lab-1 ansible_playbooks]# ansible -i ./inventories/staging/hosts staging_lab_servers -m uri -a "url=https://ya.ru return_content=true"

Install yum package on all servers:
[root@lab-1 ansible_playbooks]# ansible -i ./inventories/staging/hosts staging_lab_servers -m yum -a "name=tree state=latest" -b

Remove yum package from all servers:
[root@lab-1 ansible_playbooks]# ansible -i ./inventories/staging/hosts staging_lab_servers -m yum -a "name=tree state=removed" -b

Enable service and start it:
[root@lab-1 ansible_playbooks]# ansible -i ./inventories/staging/hosts staging_lab_servers -m service -a "name=httpd state=started enabled=yes" -b

Initialize role in the current folder (will create a subfolder with provided role name):
ansible-galaxy init ROLE_NAME

********************************************************************
********************************************************************
********************************************************************

Ansible vault encrypt a file:
ansible-vault encrypt FILE

Ansible decrypt the encrypted file:
ansible-vault decrypt FILE

Cat encrypted file:
ansible-vault view FILE

Edit encrypted file:
ansible-vault edit FILE

Run encrypted playbook:
ansible-playbook FILE.yml --ask-vault-pass

Run encrypted playbook with predefined passwd:
ansible-playbook FILE.yml --vault-password-file passwd_file.txt

Ansible encrypt variable:
ansible-vault encrypt_string
enter var: VAR_PASSWORD

copy the encrypted string to the playbook.
or
echo -n "VAR_PASSWORD" | ansible-vault encrypt_string

Prepare Windows Server for Ansible automation:
Set-Service -Name "WinRM" -StartupType Automatic
Start-Service -Name "WinRM"
winrm quickconfig
Enable-PSRemoting -SkipNetworkProfileCheck -Force

Рубрики
File system Linux

File systems

Check what is the logging type is used in the filesystem:

dmesg | grep EXT

If you need to change logging type remount partition with data=xxx specified (mount command or /etc/fstab) where xxx can be:
data=ordered
data=writeback
data=journal


data=ordered — first metadata is saved only then real data will be written to disk and confirmed as saved. (reliable but slower writeback)

data=writeback — async logging, saving data can be confirmed before real data is saved, software should end operations based on POSIX spec with fsync command.

data=journal — saving both metadata and data itself (slow)

Change file system fsck check period:

tune2fs -l /dev/sdb1 show current information
tune2fs -c 150 -i 80 /dev/sdb1 change it to 150 mount/remount operations («-c») or 80 days («-i»).

Manually recheck file system:
umount /dev/sdb1
fsck.ext4 -f /dev/sdb1
mount /dev/sdb1

Generate UUID for the file system:

tune2fs -U random /dev/sdb1 Generate random UUID

tune2fs -U 5cf24d64-b279-4565-9bfd-e6ec3436845b /dev/sdb1 Set exact UUID for FS

Check the current UUID:
blkid

Extend LVM volume and FS on it (to 30G):
lvextend -L 30G /dev/mapper/vg1-test
resize2fs /dev/mapper/vg1-test

1 st step expand the disk.
in VMware or another hypervisor.

2nd step:
Re-scan expanded disk on a fly (for example expanded vmware para-virtual drive):
echo "1" > /sys/class/block/sdX/device/rescan

3rd step:
resize the partition using fdisk

fdisk /dev/sdX

Delete partition:
Command (m for help): d
Selected partition 1
Partition 1 is deleted

Create a new primary partition:
Command (m for help): n
Select (default p):
Using default response p

Write changes to disk:
Command (m for help): w
The partition table has been altered!

Rescan partitions:
#partprobe

4th step:
check type of filesystem with blkid
if filesystem id «xfs»
to expand partition use:
#xfs_growfs -d /dev/sdX1

check new «xfs» file system info:
xfs_info /dev/sdX1

If the partition is GPT:
use
#partprobe
if you see an error similar to this:
Error: The backup GPT table is not at the end of the disk, as it should be.
Fix partition at first:
parted -l
fix
fix

Now you can use fdisk to recreate the partition:
#fdisk /dev/sdX
d
n
w


Resize file system (ext4)
resize2fs /dev/sdX1

Done

Рубрики
Linux

tar

Show contents of the package:
root# tar -tzf archiv.tar.gz

Extract files here:
root# tar -xzf archiv.tar.gz

Extract only some file by mask:
root# tar -xzf archiv.tar.gz "*.log"

Extract to a directory:
root# tar -xzf archiv.tar.gz -C dir



Show contents of the bzip2 package:
tar -tjf archiv.tar.bz2

Extract files here:
root# tar -xjf archiv.tar.gz

Рубрики
apt-get Linux

apt-get

To make 1 machine cache apt packages for hole net install apt-cacher (or apt-cacher-ng, squid-deb-proxy, apt-proxy, approx)

You do NOT need httpd (apache) for standard configuration (port 3142).
Logs file location: /var/log/apt-cacher

root# apt-get install apt-cacher

# File /etc/default/apt-cacher
vi /etc/default/apt-cacher
AUTOSTART=1


# File /etc/apt-cacher/apt-cacher.conf
vi  /etc/apt-cacher/apt-cacher.conf
...
daemon_addr=192.168.0.1
allowed_hosts=192.168.0.0/24
...


root# service apt-cacher start

#Import already downloaded apt packages to apt-chacher
root# cd /usr/share/apt-cacher
root# ./apt-cacher-import.pl /var/cache/apt/archives

Client-side configuration.
Check if
http://apt-cacher:3142
http://apt-cacher:3142/report
are reachable. (with curl or browser)

And change apt conf file:

# File /apt/apt.conf.d/01proxy
vi /apt/apt.conf.d/01proxy
Acquire::http::Proxy "http://apt-cacher:3142/"
Рубрики
Bash Linux ping

ping

Log ping to host with timestamp.

@echo off

set /p host=HOST ADDRESS: 
set logfile=Log_%host%.log

echo Target Host = %host% >%logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1 ') do (echo %%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
    timeout 1 >NUL 
    GOTO Ping)

Ping some host with date every minute:

screen -dmS ping_from_edge1_to_edge2_eth0 bash -c 'while true; do date +"%Y-%m-%d  %T" ; ping -c 60 IP_ADDRESS; done >> /home/user/ping_edge1_to_edge2_eth0.log'

Рубрики
k8s Linux Performance tests storage

fio (IOPS test)

Original topic can be accessed by https://www.ibm.com/cloud/blog/using-fio-to-tell-whether-your-storage-is-fast-enough-for-etcd


Test IOPS in Linux system.

tar -xzf fio.tgz
yum localinstall *.rpm
cd /data0/etcd
mkdir test-data;fio —rw=write —ioengine=sync —fdatasync=1 —directory=test-data —size=22m —bs=2300 —name=mytest

#!/bin/bash
now=$(date "+%Y%d%m-%H%M");
while true;
do
fio --rw=write --ioengine=sync --fdatasync=1 --directory=/data0/etcd/test-data --size=22m --bs=2300 --name=${now}_mytest >> /data0/etcd/results.log;
sleep 600;
done
Рубрики
Bash Linux

Linux repositories

To install Centos 7 EPEL repository execute:
#yum install -y epel-release

or install it from rpm:
#wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
#yum install -y epel-release-latest-7.noarch.rpm


list repos:
yum repolist

Search package in the repository:
yum search package_name

Get information about the package:
yum info package_name

Install package:
yum install package_name

List all available packages in a specific repository called repo_name (will also disable all other repositories):
yum --disablerepo="*" --enablerepo="repo_name" list available

Find a specific package in a specific repository:
yum --disablerepo="*" --enablerepo="repo_name" list available | grep 'package_name'

List output of packages in a specific repository:
yum --disablerepo="*" --enablerepo="repo_name" list available | less