When using virtual machines on AWS and GCP I came across many linux commands, some of them are easy to understand, some may not. Anyway, I never study Linux systematically before so those commands are scattered for me. As it is mid-term break, I would like to summary them here, they are strongly related to deploy web application on VM.
Network Related
- netstat (netstat -tulpn|grep 8000)
This gives the network information, I always use”netstat -tlnp|grep 5000" when error”address is already in use”, it lists the process information including”pid”. “t” — tcp, “u” — udp, “l” — listening, “p” — process, “n” — network, “grep” — to find something match with the following key words, here “5000 or 8000” is port number.
2. kill (kill pid)
After finding out which process is being used by specific port number, we just need to kill this process by using “kill pid”, pid is the process id we get from “netstat -tulpn|grep xxxx”
3. iptables
"sudo iptables -t nat -A PREROUTING -p tcp — dport 80 -j REDIRECT — to-ports 8000"
I use above to redirect the request from port 80 to 8000
4. ssh
To connect linux remotely, followed by username, domain name/IP address. For AWS, we need to save ssh key locally first, so before username we need to insert the key file path. Something like "ssh -i ~/.ssh/deco7381.pem ec2-user@ec2–52–xx–xxx–xx.ap-northeast-1.compute.amazonaws.com"
Navigation
- ls
It lists all the files in current directory, adding "-l" can lists them vertically, and followed by "/directory-name" can lists all the file in that directory
2. cd
literally it means change directory, followed by the directory you want to go. "./" meaning some directory at the same level, "../" go to the parent level, "~"go to the home directory
3. pwd
Print the path of current directory
4. clear
When there are too many command history on the screen, simply use “clear” to make it clean.
System Management
- sudo
Sometimes command is rejected, error message is “permission denial”, usually it require root permission, add “sudo” in front of the command
2. apt-get
When installing packages or softwares, we need this command. apt stands for advanced package tool. Usually we used "apt-get update" to update the repository first.
3. ps
List all the running processes
4. history
It can list all the command you have used before, but usually we just press up button to the previous commands
5. df -m
Checking the disk status, it shows how much space is used by %, "-m" meaning shows in megabytes, if the space is not enough we can stop the instance and increase the disk volume
File Management
- wget
Download files followed by some URL
2. curl
It is a powerful tool, but I only encounter scenarios using this command to download files
3. chmod
To change the access permission, when using the cloud of our university we simply "chmod 777"
4. mkdir
Create directory, and there is also "rmdir" for removing directory.
5. touch/rm
Touch to create new files, rm to delete
6. vim/nano
In some tutorials those commands are used to edit text. For Vim, start editing is "i" meaning insert, saving is ":w", quiting is ":q", those are different from other editors so I was really confused at first.