DEV Community

technonotes-hacker
technonotes-hacker

Posted on

Linux Commands - DAY 6

26-07-2023

Image description

REBOOT

  • restart or reboot of the Linux system .
sudo reboot
sudo shutdown -r now
sudo reboot -f --> force reboot
ssh root@remote-server /sbin/reboot --> remote server restart
cat /sbin/reboot  --> not in readable form
Enter fullscreen mode Exit fullscreen mode

Image description


HELP

  • help command provides information on built-in commands.
  • which helps get information about all commands.
help help
help cd
help -d help
help -d ls
help -d cd
help -m help
help -m pwd

d --> short description
m --> pseudo-manpage format

help -m ls  [ not working , why ? ]
help -d cat [ not working , why ? ]
help cat [ not working , why ? ]
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

Image description


AT, ATQ, BATCH, ATRM

  • to schedule commands to be executed at a particular time.
  • queue, examine, or delete jobs for later execution.
sudo apt install at
at
date
at 23.33
at> df -Th > df.txt
at> CTRL+D
atq --> to list the jobs in queue
atrm <job_number> --> cancel the jobs in queue
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Why these commands not working ???

Image description


NOLOGIN

  • it will allow users with no shell access.
sudo useradd -s /sbin/nologin user
cat /etc/passwd | grep nologin
tail -2 /etc/passwd
Enter fullscreen mode Exit fullscreen mode

Image description


CHSH

  • change to shell.

sudo chsh -s /bin/bash user4
tail -2 /etc/passwd
chsh ( just typing this will be helpful for current user logged in or To change the login shell for user) 
Enter fullscreen mode Exit fullscreen mode

Image description


CRONTAB

  • used to automatic schedule.
crontab -l
crontab -e

Eg., create a script name sample.sh to display amount of free
and used memory in the system with timestamp

cat > sample.sh
#!/bin/bash
free -h
echo "this is memory available"
current_time=$(date)
echo "time is: $current_time"

To execute this script for every 2 minutes and write to
file called free.log
create free.log file

touch free.log
crontab -e
*/2 * * * * /bin/bash /home/venus/sample.sh >>
/home/venus/free.log

Restart the service which is very important,
sudo systemctl restart cron.service
crontab -l
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description


WGET

  • used to non-interactive download of files from the web or to view the site status like testing the site is accessible or not.
wget http://path/to/url
wget https://www.kaniyam.com/download/Learn%20GNU
%20Linux%20in%20Tamil%20-%20Part%201.pdf
wget https://www.kaniyam.com/download/Learn-GNU-Linux-
in-Tamil-Part-2.pdf

cat downloads.txt  --> download multiple sites
http://path/to/url1
http://path/to/url2
http://path/to/url3

wget -i downloads.txt
wget http://path/to/url1 http://path/to/url2
Enter fullscreen mode Exit fullscreen mode

DU

  • to estimate file space usage.
du /etc   --> it will show all files or folders size
du /home
du -h /etc  --> size in human readable format
du -h /home/testing
du -sh /etc --> total usage size of a particular directory
du -hs * --> size of current folder
du -chs *
du -hsc /home/testing --> grand total of the folder
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

Image description


SYSTEMCTL

  • to control the service managers.
sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl status mariadb.service
sudo systemctl enable mariadb.service --> this will help to start the service when the system gets rebooted.
sudo systemctl disable mariadb.service

sudo systemctl list-units --type=service --> status of all service

sudo systemctl list-units --type=service --state=active/running/stopped/enabled/disabled/failed
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

sudo systemctl kill -s 9 <service_name> ( kill the service too)
systemctl daemon-reload ( reload all daemon services )
Enter fullscreen mode Exit fullscreen mode

TAR

  • an archiving utility
mkdir tar_examples && cd tar_examples
mkdir files && cd files
touch file{0..1000}.txt
cd ..
tar cf myfiles.tar files  --> archiving using tar
tar tf myfiles.tar  --> list the files in tar
tar -xvf myfiles.tar --> to extract
tar cf myfiles.tar.gz files --> archive using gzip
tar tf myfiles.tar.gz  --> to list
tar -xvzf myfiles.tar.gz  --> to extract
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description


APT

  • command-line interface for the package management.
  • Advanced package tool, or APT, is a free-software user interface that works with core libraries to handle the installation and removal of software on Debian, and Debian-based Linux distributions.
sudo apt update
sudo apt install vsftpd apache2 mariadb-server --> install packages
sudo apt depends bind9  --> check dependencies
sudo apt search apache2 --> search packages
sudo apt show apache2   --> information about the package
sudo apt upgrade        --> mostly don't try this , until any issues are faced in the current version.
sudo apt autoremove     --> remove unused package
sudo apt autoclean      --> clean the repository

Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

To removed any package, first keep in mind - stop the service,

sudo systemctl stop apache2
sudo apt purge apache2
sudo apt list

Enter fullscreen mode Exit fullscreen mode

Image description


ADD-APT-REPOSITORY

  • Adds a PPA repository into the /etc/apt/sources.list.
  • Is a script which adds an external APT repository to either /etc/apt/sources.list or a file in /etc/apt/sources.list.d/ or removes an already existing repository.
  • A personal package archive (PPA) is a repository that hosts software packages built and maintained by individuals or groups and made available for others.
  • PPAs are a convenient way to access the latest software versions that are not yet available in the official repositories of the operating system.
sudo add-apt-repository ppa:PPA_REPOSITORY_NAME/PPA
sudo add-apt-repository ppa:ondrej/php --> php PPA repository
sudo apt update
sudo apt install php

sudo add-apt-repository --remove ppa:PPA_REPO_NAME/PPA
sudo add-apt-repository --remove ppa:ondrej/php
Enter fullscreen mode Exit fullscreen mode

RSYNC

  • a fast, versatile, remote (and local) file-copying tool.
  • Its an alternate for scp.
  • scp will start from the begining if any network issue happens in between but rsync will start from where it stopped.
mkdir -p /home/venus/Documents/backup
rsync -v *.txt /home/venus/Documents/backup

rsync -av --progress *.txt
your_username@remote_server:/home/testing/ --> local to remote
rsync -av --progress testing@ip:/home/testing/*.txt . --> remote to local
Enter fullscreen mode Exit fullscreen mode

SCP

  • OpenSSH secure file copy.
scp *.txt testing@remote_server:/home/testing/
scp testing@remote_server:/home/testing/file.txt .  --> server to local
scp -r example_folder testing@remote_server:/home/testing/ --> copy folder
scp -r testing@remote_server:/home/testing/example_folder .  --> remote to local
Enter fullscreen mode Exit fullscreen mode

CURL

  • curl (short for "Client URL") is a command line tool that enables data transfer over various network protocols.
  • Linux curl command is used to download or upload data to a server via supported protocols such as HTTP, FTP, IMAP, SFTP, TFTP, IMAP, POP3, SCP, etc. It is a remote utility, so it works without user interaction.
curl https://www.kaniyam.com/foundation/
curl https://www.kaniyam.com/foundation/ > curl_file.txt
curl -# -O https://www.kaniyam.com/download/Learn%20GNU%20Linux%20in%20Tamil%20-%20Part%201.pdf --> shows the download and transfer rate.
Enter fullscreen mode Exit fullscreen mode

Image description

Image description


FREE

  • free and used memory in the system.
free
free -h   --> human readable
free -g/m/k/b  --> in Bytes/KB/MB/GB
free -s 2 --> every 2 sec
free -h > free.log  --> redirect 
Enter fullscreen mode Exit fullscreen mode

Image description


IFCONFIG

  • shows network interface.
ifconfig -a
ifconfig -s --> short 
ifconfig lo --> to display any one network
Enter fullscreen mode Exit fullscreen mode

Image description

Image description


IP

  • show / manipulate routing, network devices, interfaces and tunnels.
  • It is similar to the ifconfig command.
  • all these below commands will have information about the connected networks.
ip -a
ip -4 a
ip link ls up  --> shows the running interface
ip addr show
ip route show  --> IP route table
Enter fullscreen mode Exit fullscreen mode

Image description


NETSTAT

  • The network statistics ( netstat ) command is a networking tool used for troubleshooting and configuration, that can also serve as a monitoring tool for connections over the network.
netstat --all
netstat -l     --> list the port
netstat -at    --> list TCP port
netstat -au    --> UDP port alone
netstat -lt    --> TCP port 
netstat -s     --> statistics for all ports
netstat -c     --> continuously 
sudo netstat -tulpn | grep 80
sudo netstat -tulpn | grep https
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

Image description

Image description

Image description


PING

  • send ICMP ECHO_REQUEST to network hosts.
ping google.com
ping -c 5 google.com  --> limit the number of pings
ping -c 5 -q google.com  --> print the statistics
Enter fullscreen mode Exit fullscreen mode

Image description


ALIAS

  • creating alias for a command which you use frequently or any command.
  • Aliases expire with the current shell session . or
  • Define in ~/.bashrc. which will be permanent.
alias c=clear
alias f=free -h
c
f

Above command won't work with another session.

vi ~/.bashrc
alias c=’clear’
alias u=’uptime’
aliad f=’free -h’
:wq! Save and exit

After adding enter the below command , so that the file gets refreshed.
source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Image description


UNALIAS

  • to remove the alias.

unalias u
unalias f

Image description


CLEAR

  • which is used to clear the terminal.
clear
control + l
reset
Enter fullscreen mode Exit fullscreen mode

SOURCE

  • source is a shell built-in command which is used to read and execute the content of a file(generally set of commands), passed as an argument in the current shell script.
  • Execute commands from a file in the current shell.
source ~/.bashrc
source /etc/profile

cat > example.txt
free -h
pwd
date
time
uptime

source example.txt
Enter fullscreen mode Exit fullscreen mode

Image description


SH

  • is a command language interpreter that executes commands read from a command line string, the standard input, or a specified file.
sh
sh sample.sh
Enter fullscreen mode Exit fullscreen mode

Image description


SSH

  • is a program for logging into a remote machine and for executing commands on a remote machine.
ssh user@122.132.123.33
ssh user@server.com
ssh user@my.server.in -p 2222 --> using port
Enter fullscreen mode Exit fullscreen mode

TLDR

  • Display simple help pages for command-line tools.
  • The TLDR (stands for “Too Long; Didn't Read“) is a community-driven project that provides concise and simplified documentation for various Linux commands.
tldr -u --> update the local cache of tldr pages
tldr free
Enter fullscreen mode Exit fullscreen mode

Image description

Need to discuss this error ,

Image description


Important Notes:

  1. The Internet Control Message Protocol (ICMP) is a protocol that devices within a network use to communicate problems with data transmission. In this ICMP definition, one of the primary ways in which ICMP is used is to determine if data is getting to its destination and at the right time.
  2. Ping works by sending an Internet Control Message Protocol (ICMP) Echo Request to a specified interface on the network and waiting for a reply.

Top comments (0)