Common SSH Commands
Overview
This article is an introduction to finding your way around your server in SSH.
- Hit Enter or Return after every command unless specified otherwise.
- The domain example.com should always be replaced with your own domain name.
- Example paths and file names should also be replaced with your own server information.
Prerequisites
The tutorial will assume that:
- You have enabled SSH user which is eaither root or a sudo user.
- You have an ssh terminal ready available and open to login to the server from vpsserver.com.
- You should have an open command prompt.
Home
When first logging into the server this will be your first directory.
root@sample:~#
or if you are not a root user:
thisuser@sample:~#
Folder Structure
/bin : All the executable binary programs (file) required during booting, repairing, files required to run into single-user-mode, and other important, basic commands viz., cat, du, df, tar, rpm, wc, history, etc.
/boot : Holds important files during boot-up process, including Linux Kernel.
/dev : Contains device files for all the hardware devices on the machine e.g., cdrom, cpu, etc
/etc : Contains Applicationâs configuration files, startup, shutdown, start, stop script for every individual program.
/home : Home directory of the users. Every time a new user is created, a directory in the name of user is created within home directory which contains other directories like Desktop, Downloads, Documents, etc.
/lib : The Lib directory contains kernel modules and shared library images required to boot the system and run commands in root file system.
/lost+found : This Directory is installed during installation of Linux, useful for recovering files which may be broken due to unexpected shut-down.
/media : Temporary mount directory is created for removable devices viz., media/cdrom.
/mnt : Temporary mount directory for mounting file system.
/opt : Optional is abbreviated as opt. Contains third party application software. Viz., Java, etc.
/proc : A virtual and pseudo file-system which contains information about running process with a particular Process-id aka pid.
/root : This is the home directory of root user and should never be confused with â/â
/run : This directory is the only clean solution for early-runtime-dir problem.
/sbin : Contains binary executable programs, required by System Administrator, for Maintenance. Viz., iptables, fdisk, ifconfig, swapon, reboot, etc.
/srv : Service is abbreviated as âsrvâ. This directory contains server specific and service related files.
/sys : Modern Linux distributions include a /sys directory as a virtual filesystem, which stores and allows modification of the devices connected to the system.
/tmp :Systemâs Temporary Directory, Accessible by users and root. Stores temporary files for user and system, till next boot.
/usr : Contains executable binaries, documentation, source code, libraries for second level program.
/var : Stands for variable. The contents of this file is expected to grow. This directory contains log, lock, spool, mail and temp files.
Changing Current Directory
You can change the current directory with cd command.
cd /etc/sample
To go back to the home directory:
cd
or
cd ~
To go to the parent directory supposed you are in the directory /etc/sample/items/:
cd ..
Will bring you to etc/sample/.
Display your Current Directory
To display your current directory use the pwd command.
pwd
List Content of a Directory
To list the files and folder in a directory use ls command.
ls
To show hidden files use:
ls -a
To display the content of a directory with formats and size use:
ls -l
To display the size of files in a directory with human readable format use:
ls -lh
Create/Remove a Directory
To make a directory we execute the mkdir command.
mkdir /root/newdir
To make a directory within a non-existing parent directory:
mkdir -p /root/notexists/newdir
To remove a directory:
rmdir /root/newdir
To remove a directory recursively:
rmdir -p /root/notexists/newdir
Create/Remove a File
To create an empty file we use touch.
touch /root/sample.txt
To remove a file forever:
rm /root/sample.txt
To prevent yourself from accidentally removing a file:
rm -i /root/sample.txt
Then it will ask you a question if you really want to remove this particular file.
Copy a File or Directory
To copy a file we execute the cp command.
cp /root/sample.txt /root/newfile.txt
To copy complete directory:
cp -r /root/olddir /root/newdir
To prevent cp from overwriting existing files accidentally.
cp -i /root/sample.txt /root/newfile.txt
Then it will tell you that the file exists and if you want to overwrite the file.
Rename/Move a File or Directory
Use mv to rename a file or to move the file to another directory.
To rename a file:
mv file12.txt file31.txt
To rename a directory:
mv olddir newdir
To ask permission to rename a file:
mv -i file1.txt file20.txt
Working with File Content
To display first 10 lines in a file.
head /etc/passwd
To display the first n lines in a file:
head -5 file23.txt
Will show the first 5 lines in a file.
To display the last 10 lines in a file:
tail file1.txt
To display the last n lines in a file:
tail -15 file1.txt
Will display the last 15 lines in a file.