Sunday 19 February 2023

Linux Basic Commands - Tips and Tricks


#find the file responsible for ssh

which ssh

/usr/bin/ssh

rpm -qf /usr/bin/ssh


#find the rpm file and search for the config file.

rpm -qa | grep ssh

rpm -ql openssh-server-8.7p1-28.el9.x86_64


#set the port number

sudo vim /etc/ssh/sshd_config

#Port 22

systemctl reload sshd


#if not working disable SELinux

sudo cat /var/log/secure

getenforce

setenforce 0

systemctl reload/start sshd


#deny user

sudo vim /etc/ssh/sshd_config

DenyUsers tom


#list files with size

du -sch * | sort

du -sch * | sort -rn

du -sch * | sort -rn | head


#check the open port on the server

nmap -A 8.8.8.8


#to print all files in dir except 1st and 2nd files:  

ls -l | sed '1,3d'


#for deleting before 15 days file

find path_name -type f -mtime -15 -exec rm -rf {} +


#largest file in the directory

ls -lhS

ls -lhS | awk "NR==2"


#to delete blank lines of a file

sed  -i '/^$/d' file_name


#find a file with particular permission

sudo find /home/devops/ -type f -perm 775


#to see the hard-linked files in different locations

find / -xdev -samefile /tmp/abc.txt

sudo find / -xdev -inum 17375669


#to see the hard-linked files in pwd

ls -li

find -type f -links +1

 sudo find /home/devops/test/ -xdev -type f -links +1 -ls | sort -n


#7 fields in the password file

tail -n1 /etc/passwd

tom:x:1002:1003::/home/tom:/bin/bash

username:password hash:userid:groupid:info about user:home path: user shell


#reset tom user password, next login time

sudo chage -l tom

chage -d 0 tom


#change the shell of a user

tail -n1 /etc/passwd

tom:x:1002:1003::/home/tom:/sbin/nologin


sudo usermod -s /bin/bash tom


 cat /etc/passwd | grep tom

tom:x:1002:1003::/home/tom:/bin/bash


#change the home directory for a user

cat /etc/passwd | grep tom

tom:x:1002:1003::/home/tom:/bin/bash


sudo mkdir --mode=700 /opt/tom

sudo chown tom:tom /opt/tom/

sudo usermod -d /opt/tom tom


cat /etc/passwd | grep tom

tom:x:1002:1003::/opt/tom:/bin/bash


#change the default home directory for the future user

sudo vim /etc/default/useradd

HOME=/opt


sudo useradd nik

cat /etc/passwd | grep nik

nik:x:1003:1004::/opt/nik:/bin/bash


blkid - It can determine the type of content (e.g., filesystem or swap) that a block device holds.

lsblk - list block devices


Wednesday 15 February 2023

Packaging Source Code inside the RPM Packages


Prepare build environment


yum install rpm-build

mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

tree rpmbuild

rpmbuild

├── BUILD

├── RPMS

├── SOURCES

├── SPECS

└── SRPMS


5 directories, 0 files


 echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros


Pull down the source code


cd rpmbuild/SOURCES/

curl -LO https://github.com/open-iscsi/rtslib-fb/archive/refs/tags/v2.1.74.tar.gz

tar -xzvf v2.1.74.tar.gz


yum install python-rtslib

mv rtslib-fb-2.1.74/ python-rtslib-2.1.fb74/

rm -rf v2.1.74.tar.gz

tar -zcvf python-rtslib-2.1.fb74.tar.gz python-rtslib-2.1.fb74/


#edit setup.py

setup (

    name = 'python-rtslib',

    version = '2.1.fb74',


yum install python-setuptools

python setup.py bdist_rpm --packager="Jojan Paul <jpmolekunnel@gmail.com>" --spec-only

cp dist/python-rtslib.spec ../../SPECS/

cd ../../SPECS/


#edit file

vim python-rtslib.spec

%description

python-rtslib


Build RPM


cd ~/rpmbuild/

rpmbuild -ba SPECS/python-rtslib.spec

cd RPMS/noarch

ls

python-rtslib-2.1.fb74-1.noarch.rpm


#test

yum install python-rtslib-2.1.fb74-1.noarch.rpm


Build RPM From Scratch in RHEL 9


cat /etc/redhat-release

CentOS Stream release 9


RPM:- Package Manager and the package format used by many Linux distributions such as Fedora, RedHat, and CentOS to manage and distribute software in binary form.


rpmbuild directory is known as the root directory for the rpm build environment

tree rpmbuild

rpmbuild

├── BUILD

├── RPMS

├── SOURCES

├── SPECS

└── SRPMS


BUILD        - directory, where the source code of the program want to package, is built

RPMS         - where the rpm packages generated

SOURCES   - have compressed source code of the software inthe form of a tarball or a zip file.

SPECS   - have a .spec file with instructions to build the package

SRPMS   - same as RPMS, but for source RPMS. these special packages contain the original source code of the application.


the spec file is where all instructions and information needed to build an rpm package are defined.


 Build RPM: Example to build an RPM Create a local yum repo.

#as root

yum repolist

yum install -y rpmdevtools rpm-build

mv /etc/yum.repos.d/centos.repo /tmp

yum repolist

ls -l /etc/yum.repos.d/


#su to user account (recommended)

rpmdev-setuptree

ls -l rpmbuild

tree rpmbuild

rpmbuild

├── BUILD

├── RPMS

├── SOURCES

├── SPECS

└── SRPMS


5 directories, 0 files


cd rpmbuild/SOURCES/

ls

mkdir -p localrepo-1/etc/yum.repos.d/

ls

localrepo-1


Mount RHEL 9 ISO File or DVD

sudo mkdir /var/repo

sudo mount -o loop rhel-baseos-9.0-x86_64-dvd.iso /var/repo/

sudo mount /dev/sr0 /var/repo/

vim localrepo-1/etc/yum.repos.d/RHEL9local.repo

[Local-BaseOS]

name=Red Hat Enterprise Linux 9 - BaseOS

metadata_expire=-1

gpgcheck=1

enabled=1

baseurl=file:///var/repo/BaseOS/

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release


[Local-AppStream]

name=Red Hat Enterprise Linux 9 - AppStream

metadata_expire=-1

gpgcheck=1

enabled=1

baseurl=file:///var/repo/AppStream/

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release


 tar -cvzf localrepo-1.tar.gz localrepo-1/

localrepo-1/

localrepo-1/etc/

localrepo-1/etc/yum.repos.d/

localrepo-1/etc/yum.repos.d/RHEL9local.repo


cd ../SPECS/

ls

#create spec file

rpmdev-newspec localrepo.spec

localrepo.spec created; type minimal, rpm version >= 4.16.


 vim localrepo.spec

Name:           localrepo

Version:        1

Release:        0

Summary:        Local Repo For RHEL 9


Group:          System Environment/Base

License:        GPL

URL:            https://www.linuxmissive.com/

Source0:        localrepo-1.tar.gz

BuildArch:      noarch

BuildRoot:      %{_tmppath}/%{name}-buildroot


%description

Create a local yum repo file in /etc/yum.repos.d/ directory.


%prep

%setup -q


%install

mkdir -p "$RPM_BUILD_ROOT"

cp -R * "$RPM_BUILD_ROOT"


%clean

rm -rf $RPM_BUILD_ROOT


%files

%defattr(-,root,root,-)

/etc/yum.repos.d/RHEL9local.repo


%changelog

* Wed Feb 15 2023 Jojan Paul <jpmolekunnel@gmail.com>

-


cd 

#build now

rpmbuild -v -bb rpmbuild/SPECS/localrepo.spec

rpm -qpl rpmbuild/RPMS/noarch/localrepo-1-0.noarch.rpm 

/etc/yum.repos.d/RHEL9local.repo


#install rpm as root

su -

 rpm -ivh /home/devops/rpmbuild/RPMS/noarch/localrepo-1-0.noarch.rpm


rpm -qa | grep localrepo

localrepo-1-0.noarch


 cd /etc/yum.repos.d/

 ls

centos-addons.repo  RHEL9local.repo


#check yum repo working after installing rpm package

yum repolist

repo id                      repo name

Local-AppStream              Red Hat Enterprise Linux 9 - AppStream

Local-BaseOS                 Red Hat Enterprise Linux 9 - BaseOS

extras-common                CentOS Stream 9 - Extras packages


#test

yum install httpd