Skip to main content

Linux

Manage services

Start service

systemctl start <name>

service <name> start

Restart service

systemctl restart networking.service
systemctl restart <name>
service <name> status

Add service to startup

systemctl enable <name>

Check status of services

service --status-all

Restart SSH server

/etc/init.d/ssh restart
systemctl restart sshd

List running processes on specific port

netstat -ano | findstr :3001

Kill running process

tskill PID

Commands

Determine running OS release

cat /etc/*release
lsb_release -a
cat /etc/debian_version
uname -mrs
source /etc/os-release

echo $ID
echo $NAME

Download and extract file

curl -SL 'url' \
| tar -xvzf - --strip-components=1 -C /path/to/output/dir

Use -k flag to ignore SSL validation. \ -C stands for directory. \ --string-components=# subdirectory level to extract files from.

Create cron jobs

crontab -e -u root

Examples:

Run simple command at scheduled time:

0 18 * * 0-6  <command>

Run command and log results to file:

0 18 * * 0-6  <command> >> /path/to/output.log 2>&1

Use >> to append messages to output file, else > to overwrite.

File descriptors:

  • 1: Standard output (stdout).
  • 2: Standard error (stderr).

OS

Install packages

Update installed packages before installing new ones.

apt-get update && apt-get install <package> <another-package>
dpkg -i <package>

Commit package upgrades:

apt-get upgrade

Install missing dependencies

apt --fix-broken install
apt-get install -f

Exclude or ignore specific package from upgrades

apt-mark hold <package>

List on hold packages:

apt-mark showhold

System

Upgrade OS version

Upgrades from previous releases

apt-get update && apt-get dist-upgrade

Enable SSH root login

/etc/ssh/sshd_config

PermitRootLogin yes
PasswordAuthentication yes

Update $PATH

export PATH="$PATH:/usr/sbin"
echo $PATH

Network interface

/etc/network/interfaces

auto eth0
iface eth0 inet static
address x.x.x.x
netmask x.x.x.x
gateway x.x.x.x

DNS

/etc/resolv.conf

nameserver x.x.x.x

Set Timezone

timedatectl set-timezone UTC

Filesystem

Add user permission to directory

sudo chown -R <username> path/to/directory

Add file execution permission

chmod +x /path/to/file

List OS disks

Lists only usable disks:

df

Lists all attached disks:

fdisk -l

Delete all hidden files (starts with .)

rm -rf /.[!.]*

Check disk

fsck -f /dev/disk

Check and fix corrupted partition

  1. Boot in recovery mode -> Linux OS (recovery mode)

  2. Login with root account

  3. Unmount partition to be checked:

umount /dev/partition
  1. Run command:
sudo fsck -f /
  1. Reboot:
reboot

Check folder permissions

ls -la path
ls -ld path

Conditional mkdir

if not exist path/to/dir mkdir path\\to\\dir

Preserve file attributes on copy

cp -r --preserve=mode,ownership,timestamps <source> <destination>

Available attributes:

  • mode
  • ownership
  • timestamps
  • context
  • links
  • all

cifs-utils

mount -t cifs //<host>/<shared-dir> /mnt/<mount-dir> -o username=<username>,password=<password>,domain=<domain>,rw,file_mode=0777,dir_mode=0777
sudo ln -s /path/to/link/file.txt /path/to/target/file.txt

sudo ln -s /path/to/link/directory /path/to/target/directory
sudo unlink|rm /path/to/target/(file.txt|directory)

User

Change password

passwd

Switch user

su - username

List users in group

getent group groupname

Add user to group

/usr/sbin/adduser username group

Packages

supervisor

supervisorctl update
supervisorctl reread

Manage programs:

supervisorctl

start <program>
stop <program>
restart <program>
tail <program>

status
quit