search previously run command from history
Ctrl + R
#then type word
again press Ctrl + R till you find the command
hit enter once you find the command or hit right arrow -> to edit command
list running processes
ps aux
ps aux | grep process_name
watch "ps aux | grep php"
kill running process
kill -9 processid
killall -9 chrome
check linux version
cat /etc/os-release
check configuration
cat /etc/cpuinfo
cat /etc/meminfo
top
htop
check hard disk space
df -h
check current directory space
du -sch directory_path
create new directory
mkdir <directoryname>
mkdir -p path/directory/subdirectory
change file/directory permission
#to give all permission i.e. read write and execute
sudo chmod -Rf 777 [file_name/folder_name]
#to give executable access to owner, group and other
sudo chmod +x [file_name/folder_name]
#to give owner write access
sudo chmod u+w [file_name/folder_name]
#to give group write access
sudo chmod g+r [file_name/folder_name]
#to give other execute access
sudo chmod o+x [file_name/folder_name]
ssh Connection
ssh account_name@system_name/address
e.g.: ssh root@192.168.36.39
ssh -i pemfile.pem user@ip/host
e.g. ssh -i blog.pem ubuntu@192.168.36.1
ssh connection with port number and server alive interval
ssh -p 1234 -o ServerAliveInterval=60 root@192.168.41.44
to exit or logout from server
jobs
#delete all jobs
Ctrl + D
OR
exit;
To get ssh public key
cat ~/.ssh/id_rsa.pub
To create new ssh key
cd ~
ssh-keygen -t rsa
To add ssh public key on remote server
ssh-copy-id root@192.168.169.123
OR
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.169.123
OR
#you can directly copy ssh public key on remote server in file
~/.ssh/authorized_tokens
#to copy ssh key of client
cat ~/.ssh/id_rsa.pub
Once you copy ssh key on remote host you won’t need to type password again and again.
This is the most secure way to make ssh connection with remote host
to view large file
less filename
to view small file
cat filename
to open file in watch mode
tail -f filename
press Control + C to cancel
list files and directories
ls
ls -l
ls -larth
ls -larth *.txt
ls -larth someword*.extension
#list only directories
ls -ld */
#list only files
ls -p | grep -v /
Find perticular file in directory
find directory_path -name *pattern*.extension
find . -name *.csv
copy file / server copy file
cp src_file_path dest_file_path
scp file user@192.168.41.44:/tmp/
scp user@192.168.41.44:/root/file dest_path
move / rename file
mv src_file_path dest_file_path
search word in directory
grep word directory_path -inrl
grep word directory_path -inr
grep word filename
grep word filename -A 10 -B 10
#where -A 10 will show 10 lines after and -B 10 will show 10 lines before word
vimdiff difference between 2 files
vimdiff file1 file2
directory difference between 2 direcotories
:DirDiff dir1 dir2
to find file in file system
locate filename
update filesystem
updatedb &
to check log
tail -f /var/log/logfile
tail -f /var/log/logfile | grep word
to see previously run command
history
to start/stop/status/restart service
sudo service service_name status
sudo service service_name start
sudo service service_name stop
sudo service service_name restart
add new user in linux
useradd <username>
OR
useradd -G examplegroup exampleusername
e.g.
useradd -G ftp jsmith
OR
adduser <username>
set password for new user
passwd <username>
e.g.
passwd jsmith
Check newly added or existing users or list all users
cat /etc/passwd
OR
cat /etc/passwd | grep home
Delete user
userdel <username>
add user in sudoers
sudo adduser <username> sudo
you can edit sudoers file
sudo vim /etc/sudoers
#add jenkins as sudoer
jenkins ALL=(ALL) NOPASSWD: ALL
add user to group(s)
usermod -a -G group1,group2,group3 <username>
list all groups
cat /etc/group
list all users from specific group
getent group <group_name>
create new group
#groupadd [OPTIONS] GROUPNAME
groupadd <group_name>
groupadd -f <group_name>
check user’s group name
groups <user_name>
delete group
groupdel [OPTIONS] GROUPNAME
groupdel -f engg
To remove user from specific group
gpasswd -d <user_name> <group_name>
#e.g. gpasswd -d priyanka engg
run script using user
sudo -u <user> script
e.g.
sudo -u nginx /var/www/html/test.php
How to reset default user password on Ubuntu – WSL
#open ubuntu terminal
whoami
#open pwershell terminal
ubuntu config --default-user root
#again open Ubuntu terminal it will open default root user
passwd <username>
#open powershell
ubuntu config --default-user <username>
#or you can switch from root to new user
sudo -u <newuser> -s
Intermediate
Create 10 files in current folder using command
touch {1..10}.txt
echo "Hello World" | tee {1..10}.txt > /dev/null
Copy content from 1 file to all files
for i in {2..10}; do cp 1.txt $i.txt; done
Create 10 MB File from terminal
dd if=/dev/zero of=file_name bs=1M count=10
OR
truncate -s 10M <file_name>
Create 1 GB file from terminal
dd if=/dev/zero of=file_name bs=1G count=1
OR
truncate -s 1G <file_name>
How to create readonly user for specific directory
sudo adduser devro
sudo setfacl -R -m u:devro:rx /var/www/
sudo setfacl -d -m u:devro:rx /var/www/
How to open remote software in gui
ssh -X user@host
software-name
e.g. ssh -X root@192.168.1.123
and then
anydesk
This will open anydesk of local machine but it will be connected to remote server
make sure software is installed on both servers
SSH Tunneling
Tunneling: The ssh -L
command sets up a local tunnel to forward traffic through 192.168.1.112
to 192.168.1.113
. Once the tunnel is active, ssh web@localhost -p 2222
connects you to 192.168.1.113
through the tunnel.
SSHFS Mounting: The sshfs
command uses the tunnel to mount the remote directory (/var/www/html/project
on 192.168.1.113
) to a local directory (~/192_168_1_113
).
VS Code Integration: The final step opens the mounted directory in Visual Studio Code for editing.
Syntax
ssh -L local_ip:local_port:target_ip:target_port user@mediator_ip
# Step 1: Set up SSH tunnel to 2nd machine (192.168.1.112)
ssh -L localhost:2222:192.168.1.113:22 user@192.168.1.112
OR
ssh -L 2222:192.168.1.113:22 user@192.168.1.112
OR
ssh -N -L localhost:2222:192.168.1.113:22 user@192.168.1.112
#Verify tunnel established or not
netstat -tulpn | grep 2222
# Step 2: From your local machine, SSH into the 3rd machine (192.168.1.113) using the tunnel
ssh web@localhost -p 2222
# Step 3: (Optional) If needed, create a local mount point for SSHFS
mkdir -p ~/192_168_1_113
# Step 4: Mount the remote directory via SSHFS
sshfs -p 2222 web@localhost:/var/www/html/project ~/192_168_1_113/
# Step 5: Access the mounted directory
cd ~/192_168_1_113/
# Step 6: Open the project in Visual Studio Code
code .