justinmassiot
10/18/2016 - 7:47 AM

Useful commands of the Linux shell

########################################
# System
########################################
# Eteindre le système :
shutdown now
# Redémarrer le système :
reboot

# Afficher la date et l'heure du système :
date
# Show the time since boot
uptime
# Mettre à jour l'horloge système :
/usr/sbin/ntpdate pool.ntp.org
# Update system time from BIOS clock:
hwclock --hctosys

# List information on the hardware architecture
cat /proc/cpuinfo

# Connaître la version du kernel
uname -a
# Connaître sa version (générique)
cat /etc/os-release
# Connaître sa version de Debian
cat /etc/debian_version
# Connaître sa version de Redhat/CentOS
cat /etc/redhat-release
cat /etc/*release*

# List *all* the running processes:
ps xa
# Look for a running program
ps aux | grep {{program_name}} | grep -v "grep"
# Get the PID of a running program
ps aux | grep {{program_name}} | grep -v "grep" | awk -F" " '{ print $1 }'
# List the running processes and the associated occupied ressources
top
# List the RAM occupation
free

# Systèmes de fichiers supportés par l'OS :
cat /proc/filesystems

# Show the current working directory
pwd

# List the existing user groups:
awk -F ":" '{print $1}' /etc/group
# List the existing users:
awk -F ":" '{print $1}' /etc/passwd
# Determine at which groups belongs a user
groups user-name
# Show the current SSH server config
nano /etc/ssh/sshd_config

# Add a user:
adduser user-name
# Remove a user:
userdel user-name
# Add a group:
groupadd group-name
# Remove a group:
groupdel group-name
# Add a user to a group:
addgroup username groupname
# Remove a user from a group:
deluser www-data www-data

# Search for a software package:
apt-get update && apt-cache search [[my_package_name]]
# Show the packages available for upgrade:
apt-get --just-print upgrade
########################################


########################################
# User configuration
########################################
# Activate the "ll" alias command:
nano ~/.bashrc # then uncomment the lines "alias"


########################################
# Network
########################################
# Connaître les addresses IP de toutes les interfaces réseau :
ip addr
# Connaître l'addresse IP de l'interface Ethernet :
ip addr show eth0
# IP address of the eth0 interface
echo $(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')

# Modifier l'adresse IP de l'interface Ethernet :
ifconfig eth0 down && ifconfig eth0 192.168.XXX.XXX netmask 255.255.255.0 up
# alternative :
ifconfig eth0 down && ifconfig eth0 192.168.XXX.XXX/24 up
# Restaurer l'adresse IP par défaut de l'interface Ethernet :
ifconfig eth0 down && ifconfig eth0 192.168.37.250/24 up
# Acquérir automatiquement une adresse IP pour l'interface Ethernet (DHCP) :
ifconfig eth0 up && dhclient eth0 && ip addr show eth0
# Supprimer une adresse IP particulière de l'interface Ethernet :
ip addr del 192.168.XXX.XXX/24 dev eth0
########################################


########################################
# Console
########################################
# Faire sortir tout affichage (stdout/stderr) sur la sortie série :
echo Mon texte 2>&1 > /dev/ttyAMA0

# Activate the "ll" alias command:
nano ~/.bashrc
# Uncomment the lines "alias" ...
########################################


########################################
# Storage
########################################
# Lister les disques disponibles :
fdisk -l
# Monter une clé USB, détection de format automatique :
mkdir /media/usb/ & mount /dev/sda1/ /media/usb/
# Monter une clé USB au format FAT32 :
mkdir /media/usb/ & mount -t vfat /dev/sda1/ /media/usb/

# Mount a Windows network share
mkdir /mnt/[[dest_foler]]/
mount -t cifs //[[server_address]]/[[folder_name]] /mnt/[[dest_foler]]/ --options user=[[authorized_username]]

# Show the total space usage:
df -h
du -sh /
# Search '/' for the paths/files of 1GB or more (big files):
du -ah / | grep ^[0-9,\.]*G
# Show the size of '/volume1/homes' and 2 levels below
du -h -d 2 /volume1/homes

# Find a file:
find / -name "MyFileName"
find / -name "MyFileName*"
########################################

########################################
# Apache 2 web server
########################################
# Restart the Apache server:
service apache2 restart

# Test Apache configuration file (syntax):
apache2 -t
########################################


########################################
# Scripting
########################################
# Create a start-up script:
cp /etc/init.d/skeleton /etc/init.d/[[my script name]] # then edit the script manually
# Register a start-up script:
update-rc.d [[my script name]] defaults
# Verify that a script is registered to be executed:
ls /etc/rc*/*[[my script name]]

# Debug script execution
set -x && ./myscript.sh && set +x
########################################


########################################
# Miscellaneous
########################################
# Check mail in Debian 8:
ls -al /var/spool/mail
nano /var/spool/mail/[[user]]

# List mails in queue (exim4 on Debian 8)
exim -bp
# remove a specific message from queue
exim -Mrm {message-id}
# flush all the queue
exim -bp | exiqgrep -i | xargs exim -Mrm
########################################