My linux world » Useful Commands

Useful Commands


Contents

Services access

Execution level

There are 7 execution levels:

Each one of these levels can be found in the directory: /etc/rc.d/rc.d, where is the execution level.
To modify the default execution level, modify the file /etc/inittab.

Services management

We will use the command: chkconfig

# To check a service, use this command:
chkconfig --list myservice
 
# For example, to disable a service from levels 3,4,5: 
chkconfig --level 345 myservice off
 
# To enable a service at startup:
chkconfig myservice on
 
# To disable a service at statup
chkconfig myservice off

Virtual network interfaces

Creating Virtual Interface

cp ifcfg-ens33 ifcfg-ens33:0
service network restart

If you ask ifconfig, you will have see the ens33:0 entry.

Assign Multiple IP Address Range

create the file /etc/sysconfig/network-scripts/ifcfg-ens33-range0 with the following entries:

IPADDR_START=192.168.0.1
IPADDR_END=192.168.0.15
NETMASK=255.255.255.0
CLONENUM_START=0

This will map interface ens33:0 to 192.168.0.1; ens33:1 to 192.168.0.2; etc…

Log rotate

create file here /etc/logrotate.d/myconfiguration:

# size   – logrotate runs only if the filesize is equal to (or greater than) this size.
# create – rotate the original file and create the new file with specified permission, user and group.
# rotate – limits the number of log file rotation. So, this would keep only the recent 4 rotated log files.
 
# compress – Indicates that compression should be done.
# compresscmd – Specify what type of compression command should be used. For example: /bin/bzip2
# compressext – Specify the extension on the rotated log file. Without this option, the rotated file wouldd have the default extension as .gz. So, if you use bzip2 compressioncmd, specify the extension as .bz2 as shown in the above example.
 
# maxage : Remove older rotated log files
# missingok : Dont return error if the log file is missing
 
/var/log/myconfiguration/*.log {
        missingok
        notifempty
 
        size 30k
        copytruncate
        create 0600 root root
 
        compress        
        compresscmd /bin/bzip2
        compressext .bz2
 
        rotate 4
 
        maxage 100
}

Other useful commands

Get mac address

cat /sys/class/net/ens33/address

Get my ip address

hostname -I

Get current network device

nmcli d | grep connected | awk '{split($1,a,"\t"); print a[1]}'

Export X Display

yum -y install xhost
echo "export display"
export DISPLAY=myremotehost:0.0
echo "enable connections from all hosts"
xhost +

On myremotehost must have a X Server.
For windows user, you can use Xming here
Note: do not forget to add the server ip in X0.hosts !

Execute command as another user

runuser -l  myuser -c 'command'

Create pdf from png

 yum -y install ImageMagick
 # ask to compress pictures to minimize pdf size
 convert -compress jpeg `ls *.png` mygenerated.pdf

Running instances informations

echo "You'll see the pid/binary-name on far right column, match this to your running instance"
netstat -tulpn
 
echo "get running port for MYSERVICE (example sshd)"
netstat -tulnp | grep sshd | awk '{split($4,a,":"); print a[4]}'
 
echo "get running pid for MYSERVICE  (example sshd)"
netstat -tulnp | grep sshd | awk '{split($7,a,"/"); print a[1]}'

Quick install fonts

mkdir -p /usr/share/fonts/myFontName
cp myfont.ttf /usr/share/fonts/myFontName
 
echo "refresh the cache"
fc-cache -f -v

Kill pid from file

kill -9 $(cat tmp/pid)

Make change in service file

If you want to make any change to a .service file (example httpd.service), you have to reload daemon

systemctl daemon-reload

before (re) starting your service :

systemctl restart myservice.service

Shows since how long your system is running

uptime

Get os informations

[root@localhost ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
 
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.