Must know terminal commands before touching production servers
I use windows 10 on a day to day basis. My colleagues and friends use windows too. The problem starts when the GUI disappears and we just have SSH
access to the production boxes. On a production machine, running one wrong Unix/Linux command could risk your job. Here I am compiling the must know Unix/Linux commands keeping in mind the deployment and operations related tasks.
1. What is the terminal about
The Terminal is a fancy name for command prompt
in Unix/Linux versions. The terminal allows you to enter a command to interact with the operating system. When you ssh into another machine, the terminal can be used as an interface to work with other OS. When you get on a terminal, check where your commands are going to run especially when working with higher development environments like Cert, Prod.
2. How to practice Linux commands on Windows 10.
There are multiple ways to run Linux commands on Windows 10 depending on if you have a home or pro license. Here is my approach as I have windows 10 pro
- Install ubuntu on the hypervisor
- SSH to the guest OS would have been cool, if you do not want to SSH then use the terminal inside the VM.
- I did an SSH from windows Powershell.
ssh username@IPAddressOfVM
. Powershell because of the color of the shell and it gives color to the text which is easy to read. Putty does not show colors but it can be useful to save the host etc. - To get IP address, use
ifconfig
or in GUI.sudo apt-get install net-tools
to installifconfig
package.
For setting up SSH with Hyper V. SSH would not work with the default switch.
3. What are the basic CRUD commands
- Create a file
touch hello.txt
- Edit a file
cat hello.txt
- Update a file
- You can use editors like
vim
ornano
or whatever is available - In all likely hood you will have
vim
editor so usevi
commands
- You can use editors like
- Delete a file
rm hello.txt
- Rename a file
mv hello.txt helloOld.txt
- Copy a file
cp hello.txt folderName/
4. How about commands to handle folders
- Create a folder
mkdir nameOfFolder
- Change directory
- level above
cd ..
- level below
cd folderName
- level above
- Remove folder
rmdir folderName
. If folder has contents, userm -r folderName
- Move folder (also used for renaming folder)
mv -r foldername path
- Copy folder
cp -r folderName path
5. Are there any specific commands which advanced developers use
I would call these commands as slightly esoteric as only a few will use them.
-
to see the last few lines of the log file as and when they get updated.
tail -f someFile.log
-
to check the permissions of the file on the server along with the most recently updated file at the bottom.
ls -ltr
-
List all the files recursively in a folder.
ls -R
-
Change permissions for users, users of a group, etc. Here is a good link having clear details about Chmod command.
the first 3 chars represent permission for the group, the next for users of the group and the last 3 is for everyone else.
To change use chmod 664 fileName/folderName
-
Set environment variables
export MODE=PROD
& to retrieve$MODE
. To see the mode useecho $MODE
. -
To see file sizes in human-readable format use option
-h
, likels -lth
. -
Using wildcard char to filter out files in the selection, like
*
-
Using symbolic links
-
Watching the resource utility on the VM instances with
top
. -
Clearing old log files if they are in the same box.
-
Bouncing applications when they have errored or misbehaving.
6. Are there any interesting keyboard shortcuts to know?
Let's say we have a long command & it's hard to navigate.
ctrl + a
takes you to the beginning of the command
ctrl + e
takes you to the end of the command
up / down
arrow will take you to previously typed commands
tab
key will autocomplete the path
If the file or folder name had space, wrap it with double-quotes.
7. Are there any reserved words worth remembering?
Yeah, we have a few of them. The noteworthy is the ~
which takes you to the root dir. The others are
- echo
- pwd
8. What are these symbolic links
They are similar to windows shortcuts
. They are pointers to other files. They are created by ln
. The link files are treated like any other files. They can be renamed, deleted, etc.
They have many use-cases like you can skip copying a bulky folder by referencing it with a symbolic link.
9. What about the find files command.
It's a powerful command that is used to filter files, directories, fileTypes, files recursively.
find [Directory] [fileType/Name] NameOrPatternToBeSearched
example, find . -name 'hello.txt'
-- finds hello.txt in the same folder
You can chain conditions with -or
, -and
.
10. What about file content search
We use the grep
command. Grep takes in options followed by the pattern we need to search.
grep "Hey" *"
To open the result file in nano editor use nano "$(grep -ilr "Hey" * | head -1)"
11. How do I set execute permission only to the owner
chmod u+x fileName
-- u for the user, g for group, o for others, a for all. You can chain permissions like +rwx
or give numbers like chmod 777 a.txt
More fine-grained access can be given like chmod u=rwx, g=rw, o= file.txt
12. Is learning vim editor necessary
I have seen developers who are accustomed to git gui
that they cannot use git outside of it. I have some dependency on the IntelliJ idea for resolving conflicts. The problem is that I do not get IntelliJ on prod servers so I have to fall back to git commands. So is life without learning vim? There is an assumption that vim is installed on UNIX/Linux servers universally. There is no guarantee that nano is present on prod servers. So better learn vim.
Actually, to get the ball rolling you need to
- Know about reading & insert mode
- Save file & discard the changes
- Scrolling through & jumping to the beginning, end of line
- Searching for keywords, replacing text
13. Whats this PIPE operator
as the word says, it's an operator that takes the output of the first statement and gives it as input to the second.
14. How to find the process which is taking the most CPU
We can use the top
command and order the CPU column. top -o CPU
. You can see the top process of the user by ps -cvx
and further narrow it down with grep ps -cvx | grep firefox
.
15. How do you terminate a process?
Managing processes happen through signals. We can use the term or kill
command. kill $PID
will kill the application. The process id will change after the restart of the same app so do not cache the ID or store it in a variable.
16. How do you create your own command to do a bunch of stuff
Basically, we need to create a method in the bash profile and invoke it.
17. How to set alias & invoke it
alias myAlias="ll -ltr"
. Call the alias with myAlias after restarting or sourcing the bash profile.
18. How to know if your password is expiring soon in Linux
The password management policies are quite strict on production machines so remember to change the password periodically or someday it might get locked. To know the expiry date of your password and other details, type
chage -l $(whoami)
To change the password, you must know your existing password. If you do not know your password then its better you raise a ticket to reset the password.
passwd $(whoami)
Switch user account in unix/linux
Due to permission issues you will be expected to switch to another functional account. In that case do
su - username
and enter the password associated with the username. Verify by executing whoami