There might be different shell types when you try to work on shells:
You can check the shell type
echo $SHELL
here echo commands lets you print and $ sign means an environment.
Some basic commands
If you want to create multiple directories, you can follow this:
or,
here you are using -p which means you are creating parent directory.
Also , you can remove a directory using this:
use -r before file path.
Note: “-r” option for recursive
To copy a directory, we can use:
This will copy my_dir1 to the next location.
Work with files
This is how you can create a file, edit contents in it, copy the file, remove the file/rename the file.
Here you can see I created a dir_1 & dir_2 directory.
Then I entered into dir_1 directory and created a file called hey.txt . After that I added texts on that file. I also created a file named hlw.txt. I copied the content of hey.txt into hlw.txt
All this, I have done in a directory named Demo.
Check out them in your system:
Basics of VI Editor
Most of the operating system has vi editor. To use it, go to terminal and use
vi <file name>
For example,
We are in a directory where we have hey.txt and hlw.txt . Let's open the hey.txt using vi
You can see this kind of output.
Vi eidtor has 2 modes. When we open a file , it is generally in Command mode.
But you can not add content using command mode. For that, use insert mode by pressing "i" in keyboard.
To switch from insert mode to command mode, press esc key
Some basic commands:
To delete a word, use "x" but to delete a line , use "dd"
To copy, use "yy" but to paste use "p"
To scroll up use "Ctrl+u" and to scroll down, use "Ctrl+d"
Also, press ESC and then typing ":" will take you to Command prompt, to save some changes, press ":w" . To quit changes , press ":q". To save and quit, press ":wq".
Now , if you want to find a word "of" , you need to use "/of" in prompt.
To see all occurrences of "of", you need to use "n" to go to next "of".
More linux commands
- To know the user name of your system, you need to use "whoami" command.
- To check user ids , use "id"
To switch to other user, use "su " . Here su is for switch user.
To use a host using a username, follow the format, ssh username@hostname
Now, assume we have a user called "Mathew" who wants to access root user , but as he is a normal user ;he can not access it.
Root user is something that can perform any task towards the system.
But if you need to work as a root user, you have a option which is "sudo"
using
sudo ls/root
or ,
Enter the command sudo passwd root
. This will create a password for root, essentially "enabling" the account. Don't forget this password.
Type sudo -i
. Enter the root password when prompted.
The prompt will change from $ to # , indicating you have root access.
sudo -i
will give access to root by adding you to /etc/sudoers file.
SO, the user Mathew is still a normal user but can have root benefits .
How to download files?
Use the curl command and then the link and also -O to download it , otherwise it will just show the file but won't download it.
curl <link> -O
Also, you can use wget command.
wget <link> -O <file name to which you want to save as>
For example.
Check the OS version of your system
ls /etc/*release*
cat /etc/*release*
For example,
Package managers
To install files, we need to use package managers. RPM (Red hat package manager) is one of them. See the commands to install some package using rpm.
To check all of the packages installed using rpm
rpm -qa
Assume, you have been asked to do this
You have to first enable root access using
sudo -i
then go into /opt folder and then check which file we have.
we had
rpm -i <package you want to install>
Again, assume you have been told to remove that ftp package
Here you can check the list of downloaded rpm packages using rpm -qa
and search using grep ftp
BUt you can do this both in 1 command using
rpm -qa | grep ftp
SO,
But rpm, does not install dependencies to run that package, thus we came up with "yum"
To install ansible using yum,
We use the command
yum install ansible
This will search in cloud the dependencies it needs and download it . Also, yum uses RMP (Red hat package manager) to install things. Just the additional things it adds is that it downloads the dependencies.
The dependencies are listed in /etc/yum.repos.d
To check a installed package details , for example ansible use
yum list ansible
to remove use
yum remove ansible
to check packages that you might download of previous versions and new ones
yum --showduplicates list ansible
to install a particular version, use
yum install <packagename>-<version>
if we are asked to Install ansible with yum but version should be 2.8.11
Services
If any server is installed, it acts like sever and if there is any boot happening, it automatically starts working again.
To start a server called httpd
You can use both
service httpd start
or,
systemctl start httpd
Also, you can stop, check status, enable & disable servers
For example, you have a my_app.py code which has
print("Hello, World!")
to run the python file,
use
/usr/bin/python3 /opt/cpde/my_app.py
/usr/bin/python3 is used to use python3 dependencies
and /opt/cpde/my_app.py is the location of the python file.
Now to show the output of the file to a host http://localhost:5000
use
curl http://localhost:5000
The my_app.py file will be executed there.
To make this code as a service, I can use
To stop it as a service, I can use
Now if we want our services to automatically start when system boots up etc then we need to make it systemd service. systemctl uses this actually. SO, lets make our my_app.py as one of them.
systemd service is configured from systemd unit file.
THis file is located at
Now start the service we created
Now you can check status and output in host
To make our service run when the system bootups:
THen enable it
For example, enabling in real life would look like this:
(WE started a httpd server and enabled it so that it starts automatically when system boots up and we dont need to manually start the service.)
FOr additional metadata is needed to provide,
use [unit]
If the service has dependencies to run before and after , add them
To restart it when system crashes, set
Restart=always
SO, that is it.
For example, this is the docker service file
You can learn more from this course from KodeKloud.
Top comments (1)
Very informative blog vaiya