Difference between revisions of "User:Cyrusty/Useful Commands"

From Organic Design wiki
m (Useful Commands and Info)
m (SSH)
 
(61 intermediate revisions by the same user not shown)
Line 1: Line 1:
## LINUX COMMANDS ##
+
== Linux Commands ==
  
 +
 +
<source lang="bash">
 
cd # Home directory or changes directory
 
cd # Home directory or changes directory
 +
</source>
 +
 +
<source lang="bash">
 
cd .. # Up a directory and to up 2 directories ../.. and so on
 
cd .. # Up a directory and to up 2 directories ../.. and so on
cd - # Returns you to the directory you previously were in
+
</source>
 +
 
 +
<source lang="bash">
 +
cd - # Returns you to the previous directory you were in
 +
</source>
 +
 
 +
<source lang="bash">
 
cd / # Changes directory to root
 
cd / # Changes directory to root
 +
</source>
 +
 +
<source lang="bash">
 +
cd /proc # Changes directory to your running processes
 +
</source>
 +
 +
<source lang="bash">
 
ls # Lists the files that are in a directory
 
ls # Lists the files that are in a directory
 +
</source>
 +
 +
<source lang="bash">
 
ls –a # Shows all files/hidden files in a directory
 
ls –a # Shows all files/hidden files in a directory
 +
</source>
 +
 +
<source lang="bash">
 
ls –al # Shows all files/hidden files and time/write permissions in a directory
 
ls –al # Shows all files/hidden files and time/write permissions in a directory
 +
</source>
 +
 +
<source lang="bash">
 
ls -alh # Shows same as above command but shows file sizes in a better readable format
 
ls -alh # Shows same as above command but shows file sizes in a better readable format
 +
</source>
 +
 +
<source lang="bash">
 
ls -l # Shows the files and write permissions in the directory
 
ls -l # Shows the files and write permissions in the directory
 +
</source>
 +
 +
<source lang="bash">
 
ls / # Shows the contents of the root directory
 
ls / # Shows the contents of the root directory
 +
</source>
 +
 +
<source lang="bash">
 
cat <file name. extension> # Prints the contents of a file on the shell
 
cat <file name. extension> # Prints the contents of a file on the shell
 +
</source>
 +
 +
<source lang="bash">
 
mkdir <Directory Name> # Makes a directory
 
mkdir <Directory Name> # Makes a directory
 +
</source>
 +
 +
<source lang="bash">
 
mkdir -p EXAMPLE/RIGHT/IN/HERE # Makes multiple folders in the directory created
 
mkdir -p EXAMPLE/RIGHT/IN/HERE # Makes multiple folders in the directory created
 +
</source>
 +
 +
<source lang="bash">
 
mv /FILE/NAME/OR/DIR /TO/DIR/WANTED # Moves a file to a directory or renames a file
 
mv /FILE/NAME/OR/DIR /TO/DIR/WANTED # Moves a file to a directory or renames a file
 +
</source>
 +
 +
<source lang="bash">
 
rm <FILE NAME> # Removes a file
 
rm <FILE NAME> # Removes a file
 +
</source>
 +
 +
<source lang="bash">
 
rmdir <DIRECTORY> # Removes an empty directory
 
rmdir <DIRECTORY> # Removes an empty directory
 +
</source>
 +
 +
<source lang="bash">
 
rm –r # Removes a directory and its contents
 
rm –r # Removes a directory and its contents
 +
</source>
 +
 +
<source lang="bash">
 
rm -rf # Force deletes everything
 
rm -rf # Force deletes everything
 +
</source>
 +
 +
<source lang="bash">
 
rm –rf / # Deletes everything under root directory
 
rm –rf / # Deletes everything under root directory
 +
</source>
 +
 +
<source lang="bash">
 
sudo # Runs command as sudo
 
sudo # Runs command as sudo
sudo -i # Changes to root privellages
+
</source>
 +
 
 +
<source lang="bash">
 +
sudo bash # Changes to root privellages
 +
</source>
 +
 
 +
<source lang="bash">
 
sudo su # Gives SUPERUSER privellages
 
sudo su # Gives SUPERUSER privellages
man <COMMAND> # Displays how a command can be used and what it does
+
</source>
 +
 
 +
<source lang="bash">
 +
sudo !! # This will redo the previous command but instead runs it as sudo
 +
</source>
 +
 
 +
<source lang="bash">
 +
su # switch USER
 +
</source>
 +
 
 +
<source lang="bash">
 +
adduser USERNAME # Create a USER
 +
</source>
 +
 
 +
<source lang="bash">
 +
adduser USERNAME sudo # Adds USER to the sudo group
 +
</source>
 +
 
 +
<source lang="bash">
 +
passwd # Reset password for current user
 +
</source>
 +
 
 +
<source lang="bash">
 +
man <COMMAND> # Displays a manual for a specific command
 +
</source>
 +
 
 +
<source lang="bash">
 +
man hier # Displays a description of the filesystem hierarchy
 +
</source>
 +
 
 +
<source lang="bash">
 
grep # searching, finding, filtering command
 
grep # searching, finding, filtering command
 +
</source>
 +
 +
<source lang="bash">
 
top # Shows running processes
 
top # Shows running processes
 +
</source>
 +
 +
<source lang="bash">
 
htop # Shows more information than top and in more detail
 
htop # Shows more information than top and in more detail
 +
</source>
 +
 +
<source lang="bash">
 +
sudo renice -10 PID # This will prioritize a process (-20 being highest priority and 19 being lowest priority)
 +
</source>
 +
 +
<source lang="bash">
 
ps -A # Shows all running processes
 
ps -A # Shows all running processes
 +
</source>
 +
 +
<source lang="bash">
 
kill PID # Kills process under a certain PID
 
kill PID # Kills process under a certain PID
 +
</source>
 +
 +
<source lang="bash">
 
kill -9 PID # Force kills a process that wont kill normally
 
kill -9 PID # Force kills a process that wont kill normally
 +
</source>
 +
 +
<source lang="bash">
 
sudo killall NAME # Kill all processes under the NAME
 
sudo killall NAME # Kill all processes under the NAME
 +
</source>
 +
 +
<source lang="bash">
 
sudo pkill -u USER # Kills all USER processes
 
sudo pkill -u USER # Kills all USER processes
 +
</source>
 +
 +
<source lang="bash">
 
head FILENAME # Prints the first 10 lines
 
head FILENAME # Prints the first 10 lines
 +
</source>
 +
 +
<source lang="bash">
 
tail FILENAME # Prints the last 10 lines
 
tail FILENAME # Prints the last 10 lines
 +
</source>
 +
 +
<source lang="bash">
 
clear # Clears the screen
 
clear # Clears the screen
 +
</source>
 +
 +
<source lang="bash">
 
pwd # Print working directory
 
pwd # Print working directory
 +
</source>
 +
 +
<source lang="bash">
 
<file name. extension> # Makes a file in current directory ##unsure of this##
 
<file name. extension> # Makes a file in current directory ##unsure of this##
 +
</source>
 +
 +
<source lang="bash">
 
cal # Shows current calender date (also cal mm/yy)
 
cal # Shows current calender date (also cal mm/yy)
 +
</source>
 +
 +
<source lang="bash">
 
whatis # Type a command after to see a brief description of what it does/is
 
whatis # Type a command after to see a brief description of what it does/is
adduser USERNAME # Create a USER
+
</source>
adduser USERNAME sudo # Adds USER to the sudo group
+
 
 +
<source lang="bash">
 
who # Shows current users on the machine and when they logged in
 
who # Shows current users on the machine and when they logged in
 +
</source>
 +
 +
<source lang="bash">
 
w # Shows whos logged in, from where, how long and what they're doing
 
w # Shows whos logged in, from where, how long and what they're doing
 +
</source>
 +
 +
<source lang="bash">
 
nano # Edit a text file
 
nano # Edit a text file
 +
</source>
 +
 +
<source lang="bash">
 
vi # Nano is better...
 
vi # Nano is better...
 +
</source>
 +
 +
<source lang="bash">
 
vim # Another editor... type vimtutor on commandline to learn more
 
vim # Another editor... type vimtutor on commandline to learn more
 +
</source>
 +
 +
<source lang="bash">
 
sort # Sort lines of text files
 
sort # Sort lines of text files
 +
</source>
 +
 +
<source lang="bash">
 
wc # Wordcount a text file
 
wc # Wordcount a text file
 +
</source>
 +
 +
<source lang="bash">
 
rig # Random Identity Generator
 
rig # Random Identity Generator
 +
</source>
 +
 +
<source lang="bash">
 
apt-cache search # ??
 
apt-cache search # ??
 +
</source>
  
 +
== Networking ==
  
## SYSTEM INFO ##
 
dmidecode | less # Shows bios information
 
lscpu # Shows cpu/hardware information
 
lspci # List all PCI devices
 
lsusb # List all USB devices
 
lsblk # List all Hard disks, Cd drives etc
 
cat /proc/meminfo # Shows memory information
 
cat /proc/version # Shows current running kernal version
 
  
 +
<source lang="bash">
 +
ifconfig # Shows network info IP/MAC address
 +
</source>
  
 +
<source lang="bash">
 +
sudo netdiscover # Shows who's on the network and identifies the ip address of each connection
 +
</source>
  
## SHUTDOWN/REBOOT ##
+
<source lang="bash">
shutdown -r +60 # restarts in 60mins
+
sudo lsof -i # Lists the open ports and the process that owns them (to see which process is bound to port use :PORTNUMBER)
shutdown -h +60 # shutdown in 60mins
+
</source>
poweroff # shuts system down
 
reboot # restarts computer
 
init 6 # same as reboot
 
  
 +
<source lang="bash">
 +
sudo netstat # Seeing what might be exposed to the network</source>
  
 +
<source lang="bash">
 +
sudo netstat -tulpn # t=tcp, u=udp ports, l=listening ports, p=processes, n=numerical
 +
</source>
  
## HANDY THINGS TO REMEMBER ##
+
<source lang="bash">
CTRL r # to search for previously used COMMAND
+
sudo macchanger -s enp4s0/eth0/wps/ # To see mac addresses
CTRL p # shows previously used COMMAND
+
</source>
CTRL a # brings you to begin of line
 
CTRL e # brings to the end of line
 
tail -f FILENAME # this will follow the file while it's being edited
 
echo "some kind of text" > example.text # this will overwrite the .text file with the quoted line
 
echo "new line of text" >> example.text # this will write the quoted line on a new line
 
alias COMMANDNAME="DESIREDNAME" # this will temporarily save a command to the desired name
 
echo "alias COMMANDNAME=\"DESIREDNAME\";" >> ~/.bashrc # this will permanently save a desired command to the bash file
 
&& # chain commands together
 
  
 
+
<source lang="bash">
 
 
## NETWORKING ##
 
ifconfig # Shows network info IP/MAC address
 
sudo netdiscover # Shows who's on the network and identifies the ip address of each connection
 
sudo lsof -i # Lists the open ports and the process that owns them (to see which process is bound to port use :PORTNUMBER)
 
sudo netstat # Seeing what might be exposed to the network
 
sudo netstat -tulpn # t=tcp, u=udp ports, l=listening ports, p=processes, n=numerical
 
sudo macchanger -s enp4s0/eth0/wps/ # To see mac addresses
 
 
sudo macchanger -r enp4s0 # Changes the current mac address to a new one
 
sudo macchanger -r enp4s0 # Changes the current mac address to a new one
 +
</source>
  
 +
<source lang="bash">
 
ssh -NCD 1080 user@ip # Proxy network traffic over port 1080 (edit below settings on firefox to port traffic correctly)
 
ssh -NCD 1080 user@ip # Proxy network traffic over port 1080 (edit below settings on firefox to port traffic correctly)
 +
</source>
 
Manual Proxy Configuration
 
Manual Proxy Configuration
 +
 
Socks Host: localhost
 
Socks Host: localhost
 +
 
port: 1080
 
port: 1080
  
  
 +
== SSH ==
  
## EXAMPLES OF THINGS ##
 
ls -alh /home/user/some/random/place >> ANYNAME # cat ANYNAME to see it show the directory with info
 
grep WHATEVERSEARCHINGFOR ./* # Useful for findings things.. If successful this will print out what it finds in the shell
 
  
 +
<source lang="bash">
 +
ssh-keygen -t rsa # creates a key in .ssh
 +
</source>
  
## SSH ##
+
<source lang="bash">
ssh-keygen -t # creates a key in .ssh
 
 
sudo nano /etc/ssh/sshd_config # change ssh parameters (key logging/password authentication)
 
sudo nano /etc/ssh/sshd_config # change ssh parameters (key logging/password authentication)
 +
</source>
 +
 +
<source lang="bash">
 
sudo service ssh restart # saves ssh modifications
 
sudo service ssh restart # saves ssh modifications
 +
</source>
 +
 +
<source lang="bash">
 
ssh-copy-id USER@IP # saves the pub key to be known as an authenticated key
 
ssh-copy-id USER@IP # saves the pub key to be known as an authenticated key
scp -r USER@IP /home/USER/FILE/FOLDER /home/local/machine # copy from server to localmachine
+
</source>
scp -r /home/USER/local/machine/file/folder USER@IP:/home/USER/VPS
+
 
 +
<source lang="bash">
 +
scp -r USER@IP:/home/USER/FILE/FOLDER /home/user/localmachine # copy from server to localmachine
 +
</source>
  
 +
<source lang="bash">
 +
scp -r /home/USER/localmachine/file/folder USER@IP:/home/USER/VPS # copy files from localmachine to server
 +
</source>
  
 +
<source lang="bash">
 +
sudo hostname -b NEWHOSTNAME # Renames the host
 +
</source>
 +
 +
<source lang="bash">
 +
sudo nano /etc/hosts # Set unique names for IP's (makes shelling in faster using a desired name) eg. 172.321.123.123<TAB>VPS1
 +
</source>
 +
 +
<source lang="bash">
 +
sudo apt-get install openssh-server # allows access to ssh into the computer through a LAN
 +
</source>
 +
 +
== System Info ==
 +
 +
<source lang="bash">
 +
df # Displays mounted devices
 +
</source>
 +
 +
<source lang="bash">
 +
dmidecode | less # Shows bios information
 +
</source>
 +
 +
<source lang="bash">
 +
lscpu # Shows cpu/hardware information
 +
</source>
 +
 +
<source lang="bash">
 +
lspci # List all PCI devices
 +
</source>
 +
 +
<source lang="bash">
 +
lsusb # List all USB devices
 +
</source>
 +
 +
<source lang="bash">
 +
lsblk # List all Hard disks, Cd drives etc
 +
</source>
 +
 +
<source lang="bash">
 +
cat /proc/meminfo # Shows memory information
 +
</source>
 +
 +
<source lang="bash">
 +
cat /proc/version # Shows current running kernal version
 +
</source>
 +
 +
 +
== Shutdown/Reboot ==
 +
 +
 +
<source lang="bash">
 +
shutdown -r +60 # restarts in 60mins
 +
</source>
 +
 +
<source lang="bash">
 +
shutdown -h +60 # shutdown in 60mins
 +
</source>
 +
 +
<source lang="bash">
 +
poweroff # shuts system down
 +
</source>
 +
 +
<source lang="bash">
 +
reboot # restarts computer
 +
</source>
 +
 +
<source lang="bash">
 +
init 6 # same as reboot
 +
</source>
  
## RANDOM PASSWORD GENERATORS ##
 
openssl rand -base64 32 # generates random 32 character password
 
</dev/urandom tr -dc 'A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~' | head -c 10 ; echo # Sauls random character/symbol generated password command
 
  
 +
== Account Management ==
  
  
## SYSTEM COMMANDS/SERVICES ##
+
<source lang="bash">
sudo apt-get update && sudo apt-get install # Updates n upgrades system to latest
+
useradd # Adds a user
crontab -e # the crontab (short for "cron table") is a list of commands that are scheduled to run at regular time intervals on your computer system
+
</source>
sudo nano /etc/default/grub # change grub options (boot timer)
 
  
 +
<source lang="bash">
 +
useradd -m -d # Create a new user and makes a home directory for the user and define the home directory /home/DIR/FOR/USER
 +
</source>
  
 +
<source lang="bash">
 +
sudo adduser <username> sudo # adds user to sudo privileges
 +
</source>
  
 +
<source lang="bash">
 +
userdel # Deletes a user
 +
</source>
  
## CRONTAB SCRIPT COMMANDS ##
+
<source lang="bash">
@reboot macchanger -r enp4s0 # (or eth0) # changes to random mac address on each reboot
+
usermod -L USER # Locks the user from logging in
 +
</source>
  
 +
<source lang="bash">
 +
usermod -U USER # Unlocks the user so they can log in again
 +
</source>
  
 +
<source lang="bash">
 +
tail /etc/shadow
 +
</source>
  
## MAIL ##
+
<source lang="bash">
mail # Checks mail for current user
+
tail /etc/passwd
mail -s "SUBJECT" USER < /home/USER/test.text # sends a mail to the user with test.text as the body
+
</source>
mail -f USER # Checks mail for selected user
 
mail -f guest # checks guest messages
 
  
 +
<source lang="bash">
 +
tail /etc/group
 +
</source>
  
## PIPING & REDIRECTION ##
+
Note users will have a "!" after their name if they are locked out of their account
cat /FILE/IN/A/DIRECTORY | less # Shows in a opened document view without clogging the terminal
+
to check this type in the above shadow command
cat /FILE/IN/A/DIRECTORY | espeak # Speaks the text document in linux voice
 
  
 +
== File Permissions ==
  
  
## FILE PERMISSIONS ##
 
 
(R)ead=4
 
(R)ead=4
 +
 
(W)rite=2
 
(W)rite=2
 +
 
(X)ecute=1
 
(X)ecute=1
 +
 
d=directory
 
d=directory
 +
 
l=link
 
l=link
 +
 
4+2+1 = (r+w+x) = 7
 
4+2+1 = (r+w+x) = 7
 +
 
4+2 = (r+w) = 6
 
4+2 = (r+w) = 6
 +
 
4+1 = (r+x) = 5
 
4+1 = (r+x) = 5
 +
 
4 = (r) = 4
 
4 = (r) = 4
 +
 
2+1 = (w+x) = 3
 
2+1 = (w+x) = 3
 +
 
2 = (w) = 2
 
2 = (w) = 2
 +
 
1 = (x) = 1
 
1 = (x) = 1
  
 +
<source lang="bash">
 
chmod xxx dir # Change all files in a single directories permissions
 
chmod xxx dir # Change all files in a single directories permissions
 +
</source>
 +
 +
<source lang="bash">
 
chmod -R xxx dir # Change all file/folders permissions recursively
 
chmod -R xxx dir # Change all file/folders permissions recursively
 +
</source>
 +
 +
<source lang="bash">
 
chown owner:group DIRECTORYNAME # Changes the owner and group name of files
 
chown owner:group DIRECTORYNAME # Changes the owner and group name of files
 +
</source>
 +
 +
 +
== Mail ==
 +
 +
 +
<source lang="bash">
 +
mail # Checks mail for current user
 +
</source>
 +
 +
<source lang="bash">
 +
mail -s "SUBJECT" USER < /home/USER/test.text # sends a mail to the user with test.text as the body
 +
</source>
 +
 +
<source lang="bash">
 +
mail -f USER # Checks mail for selected user
 +
</source>
 +
 +
<source lang="bash">
 +
mail -f guest # checks guest messages
 +
</source>
 +
 +
 +
== Piping & Redirection ==
  
  
 +
<source lang="bash">
 +
cat /FILE/IN/A/DIRECTORY | less # Shows in a opened document view without clogging the terminal
 +
</source>
  
## ACCOUNT MANAGEMENT ##
+
<source lang="bash">
useradd # Adds a user
+
cat /FILE/IN/A/DIRECTORY | espeak # Speaks the text document in linux voice
useradd -m -d # Create a new user and makes a home directory for the user and define the home directory /home/DIR/FOR/USER
+
</source>
userdel # Deletes a user
+
 
usermod -L USER # Locks the user from logging in
+
 
usermod -U USER # Unlocks the user so they can log in again
+
== Git ==
tail /etc/shadow
 
tail /etc/passwd
 
tail /etc/group
 
#Note users will have a "!" after their name if they are locked out of their account
 
to check this type in the following command "tail /etc/shadow"
 
  
  
## GIT ##
+
<source lang="bash">
 
git add . # add all files for committing
 
git add . # add all files for committing
 +
</source>
 +
 +
<source lang="bash">
 
git commit -am "COMMIT MESSAGE" # commit the changes locally
 
git commit -am "COMMIT MESSAGE" # commit the changes locally
 +
</source>
 +
 +
<source lang="bash">
 
git push origin master # push changes to the server
 
git push origin master # push changes to the server
 +
</source>
 +
 +
<source lang="bash">
 +
git remote remove origin # Removes origin master
 +
</source>
 +
 +
== System Commands & Services ==
  
==Commands for VPS==
 
  
 
<source lang="bash">
 
<source lang="bash">
scp /home/USER root@MY-VPS-IP:home/USER # sends files from localmachine to VPS
+
sudo apt-get update && sudo apt-get install # Updates n upgrades system to latest
 
</source>
 
</source>
  
 
<source lang="bash">
 
<source lang="bash">
scp root@VPSIP:/INSERT_FILE_DIRECTORY_HERE /home/USER/WHEREVER # sends files VPS to PC (to do a whole folder) put -r eg.. scp -r /home/USER/FOLDER root@MY-VPS-UP:/home/USER
+
crontab -e # the crontab (short for "cron table") is a list of commands that are scheduled to run at regular time intervals on your computer system
 
</source>
 
</source>
  
 +
<source lang="bash">
 +
sudo nano /etc/default/grub # change grub options (boot timer)
 +
</source>
  
 
<source lang="bash">
 
<source lang="bash">
ssh -NCD 1080 USER@SERVERIP # go to firefox change to manual proxy and enter for Port 1080, no proxy for:localhost, 127.0.0.1 ## this will show put traffic through a VPN
+
cpulimit -b -l <PERCENTAGE> -p <PID> # Install cpulimit and use this command to restrict the amount of resources a program will use
 
</source>
 
</source>
 +
 +
== Crontab Script Commands ==
  
  
 
<source lang="bash">
 
<source lang="bash">
1. Firstly change the ssh password authentication to yes
+
@reboot sudo macchanger -r enp4s0 # (or eth0) # changes to random mac address on each reboot
sudo nano /etc/ssh/sshd_config # modifies ssh parameters
 
2. ssh-copy-id NAME@IP # this uploads pub key to authorized keys
 
3. sudo service ssh restart
 
# If you want to rename for faster access do the following
 
sudo nano /etc/hosts #add a new line and put IP_ADDRESS_OF_SERVER NAMEWANTED
 
can also do the following
 
sudo echo "REMOTEIP INSERTPREFERREDNAME" >> /etc/hosts # does the same as above
 
 
</source>
 
</source>
==Other Info==
 
  
 
<source lang="bash">
 
<source lang="bash">
cat ~/.bash_history | grep apt-get # Show all the stuff I've tried to install or remove
+
@reboot sleep 20 && sudo shutdown 60 # will wait 20mins then schedule a shutdown 60mins later (just put this down as a reminder for sleep in crontab)
 
</source>
 
</source>
 +
 +
<source lang="bash">
 +
*  *    * * *  python3 /home/USER/ddns.py DNS 67f0ce616739484f82ca3837f40b401f # This will run the ddns.py script every 1 minute to detect if the ip changes and will update the domain (so the domain always points to the current ip address of the lappie/pc (of the router actually).
 +
</source>
 +
^The ddns.py script can be found [https://gitlab.com/absorto/ddns/blob/master/ddns.py here]
 +
 +
== Handy things to remember ==
  
  
 
<source lang="bash">
 
<source lang="bash">
ssh -NR 1234:localhost:22 cy.rusty.space # Make an SSH connection to my machine temporarily available on port 1234 in the server:
+
ln -s /PATH/TO/SOMETHING/REAL /usr/local/bin/NAMEWANTED # can use executable file with preferred bind name
 
</source>
 
</source>
  
When you're not sure what the syntax is type
 
 
<source lang="bash">
 
<source lang="bash">
apropos KEYWORD # helps find the correct syntax user is looking for
+
CTRL r # to search for previously used COMMAND
 +
</source>
 +
 
 +
<source lang="bash">
 +
CTRL p # shows previously used COMMAND
 +
</source>
 +
 
 +
<source lang="bash">
 +
CTRL a # brings you to begin of line
 +
</source>
 +
 
 +
<source lang="bash">
 +
CTRL e # brings to the end of line
 +
</source>
 +
 
 +
<source lang="bash">
 +
tail -f FILENAME # this will follow the file while it's being edited
 +
</source>
 +
 
 +
<source lang="bash">
 +
echo "some kind of text" > example.text # this will overwrite the .text file with the quoted line
 +
</source>
 +
 
 +
<source lang="bash">
 +
echo "new line of text" >> example.text # this will write the quoted line on a new line
 +
</source>
 +
 
 +
<source lang="bash">
 +
alias COMMANDNAME="DESIREDNAME" # this will temporarily save a command to the desired name
 +
</source>
 +
 
 +
<source lang="bash">
 +
echo "alias COMMANDNAME=\"DESIREDNAME\";" >> ~/.bashrc # this will permanently save a desired command to the bash file
 +
</source>
 +
 
 +
<source lang="bash">
 +
&& # chain commands together
 +
</source>
 +
 
 +
<source lang="bash">
 +
convert example.png example.jpg book.pdf # Converting files to PDF
 +
</source>
 +
 
 +
> means to overwrite <br>
 +
>> means to append
 +
 
 +
 
 +
== Examples of Things ==
 +
 
 +
 
 +
<source lang="bash">
 +
ls -alh /home/user/some/random/place >> ANYNAME # cat ANYNAME to see it show the directory with info
 +
</source>
 +
 
 +
<source lang="bash">
 +
grep WHATEVERSEARCHINGFOR ./* # Useful for findings things.. If successful this will print out what it finds in the shell
 +
</source>
 +
 
 +
 
 +
== Random Password Generators ==
 +
 
 +
 
 +
<source lang="bash">
 +
openssl rand -base64 32 # generates random 32 character password
 +
</source>
 +
 
 +
<source lang="bash">
 +
</dev/urandom tr -dc 'A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~' | head -c 10 ; echo # Sauls random character/symbol generated password command
 
</source>
 
</source>
If a file in the shell has spacebars used instead of _ or - I need to use a backward \ if I'm trying to do anything with it.
 

Latest revision as of 20:50, 8 August 2019

Linux Commands

cd # Home directory or changes directory
cd .. # Up a directory and to up 2 directories ../.. and so on
cd - # Returns you to the previous directory you were in
cd / # Changes directory to root
cd /proc # Changes directory to your running processes
ls # Lists the files that are in a directory
ls –a # Shows all files/hidden files in a directory
ls –al # Shows all files/hidden files and time/write permissions in a directory
ls -alh # Shows same as above command but shows file sizes in a better readable format
ls -l # Shows the files and write permissions in the directory
ls / # Shows the contents of the root directory
cat <file name. extension> # Prints the contents of a file on the shell
mkdir <Directory Name> # Makes a directory
mkdir -p EXAMPLE/RIGHT/IN/HERE # Makes multiple folders in the directory created
mv /FILE/NAME/OR/DIR /TO/DIR/WANTED # Moves a file to a directory or renames a file
rm <FILE NAME> # Removes a file
rmdir <DIRECTORY> # Removes an empty directory
rm –r # Removes a directory and its contents
rm -rf # Force deletes everything
rm –rf / # Deletes everything under root directory
sudo # Runs command as sudo
sudo bash # Changes to root privellages
sudo su # Gives SUPERUSER privellages
sudo !! # This will redo the previous command but instead runs it as sudo
su # switch USER
adduser USERNAME # Create a USER
adduser USERNAME sudo # Adds USER to the sudo group
passwd # Reset password for current user
man <COMMAND> # Displays a manual for a specific command
man hier # Displays a description of the filesystem hierarchy
grep # searching, finding, filtering command
top # Shows running processes
htop # Shows more information than top and in more detail
sudo renice -10 PID # This will prioritize a process (-20 being highest priority and 19 being lowest priority)
ps -A # Shows all running processes
kill PID # Kills process under a certain PID
kill -9 PID # Force kills a process that wont kill normally
sudo killall NAME # Kill all processes under the NAME
sudo pkill -u USER # Kills all USER processes
head FILENAME # Prints the first 10 lines
tail FILENAME # Prints the last 10 lines
clear # Clears the screen
pwd # Print working directory
<file name. extension> # Makes a file in current directory ##unsure of this##
cal # Shows current calender date (also cal mm/yy)
whatis # Type a command after to see a brief description of what it does/is
who # Shows current users on the machine and when they logged in
w # Shows whos logged in, from where, how long and what they're doing
nano # Edit a text file
vi # Nano is better...
vim # Another editor... type vimtutor on commandline to learn more
sort # Sort lines of text files
wc # Wordcount a text file
rig # Random Identity Generator
apt-cache search # ??

Networking

ifconfig # Shows network info IP/MAC address
sudo netdiscover # Shows who's on the network and identifies the ip address of each connection
sudo lsof -i # Lists the open ports and the process that owns them (to see which process is bound to port use :PORTNUMBER)
sudo netstat # Seeing what might be exposed to the network
sudo netstat -tulpn # t=tcp, u=udp ports, l=listening ports, p=processes, n=numerical
sudo macchanger -s enp4s0/eth0/wps/ # To see mac addresses
sudo macchanger -r enp4s0 # Changes the current mac address to a new one
ssh -NCD 1080 user@ip # Proxy network traffic over port 1080 (edit below settings on firefox to port traffic correctly)

Manual Proxy Configuration

Socks Host: localhost

port: 1080


SSH

ssh-keygen -t rsa # creates a key in .ssh
sudo nano /etc/ssh/sshd_config # change ssh parameters (key logging/password authentication)
sudo service ssh restart # saves ssh modifications
ssh-copy-id USER@IP # saves the pub key to be known as an authenticated key
scp -r USER@IP:/home/USER/FILE/FOLDER /home/user/localmachine # copy from server to localmachine
scp -r /home/USER/localmachine/file/folder USER@IP:/home/USER/VPS # copy files from localmachine to server
sudo hostname -b NEWHOSTNAME # Renames the host
sudo nano /etc/hosts # Set unique names for IP's (makes shelling in faster using a desired name) eg. 172.321.123.123<TAB>VPS1
sudo apt-get install openssh-server # allows access to ssh into the computer through a LAN

System Info

df # Displays mounted devices
dmidecode | less # Shows bios information
lscpu # Shows cpu/hardware information
lspci # List all PCI devices
lsusb # List all USB devices
lsblk # List all Hard disks, Cd drives etc
cat /proc/meminfo # Shows memory information
cat /proc/version # Shows current running kernal version


Shutdown/Reboot

shutdown -r +60 # restarts in 60mins
shutdown -h +60 # shutdown in 60mins
poweroff # shuts system down
reboot # restarts computer
init 6 # same as reboot


Account Management

useradd # Adds a user
useradd -m -d # Create a new user and makes a home directory for the user and define the home directory /home/DIR/FOR/USER
sudo adduser <username> sudo # adds user to sudo privileges
userdel # Deletes a user
usermod -L USER # Locks the user from logging in
usermod -U USER # Unlocks the user so they can log in again
tail /etc/shadow
tail /etc/passwd
tail /etc/group

Note users will have a "!" after their name if they are locked out of their account to check this type in the above shadow command

File Permissions

(R)ead=4

(W)rite=2

(X)ecute=1

d=directory

l=link

4+2+1 = (r+w+x) = 7

4+2 = (r+w) = 6

4+1 = (r+x) = 5

4 = (r) = 4

2+1 = (w+x) = 3

2 = (w) = 2

1 = (x) = 1

chmod xxx dir # Change all files in a single directories permissions
chmod -R xxx dir # Change all file/folders permissions recursively
chown owner:group DIRECTORYNAME # Changes the owner and group name of files


Mail

mail # Checks mail for current user
mail -s "SUBJECT" USER < /home/USER/test.text # sends a mail to the user with test.text as the body
mail -f USER # Checks mail for selected user
mail -f guest # checks guest messages


Piping & Redirection

cat /FILE/IN/A/DIRECTORY | less # Shows in a opened document view without clogging the terminal
cat /FILE/IN/A/DIRECTORY | espeak # Speaks the text document in linux voice


Git

git add . # add all files for committing
git commit -am "COMMIT MESSAGE" # commit the changes locally
git push origin master # push changes to the server
git remote remove origin # Removes origin master

System Commands & Services

sudo apt-get update && sudo apt-get install # Updates n upgrades system to latest
crontab -e # the crontab (short for "cron table") is a list of commands that are scheduled to run at regular time intervals on your computer system
sudo nano /etc/default/grub # change grub options (boot timer)
cpulimit -b -l <PERCENTAGE> -p <PID> # Install cpulimit and use this command to restrict the amount of resources a program will use

Crontab Script Commands

@reboot sudo macchanger -r enp4s0 # (or eth0) # changes to random mac address on each reboot
@reboot sleep 20 && sudo shutdown 60 # will wait 20mins then schedule a shutdown 60mins later (just put this down as a reminder for sleep in crontab)
*  *    * * *   python3 /home/USER/ddns.py DNS 67f0ce616739484f82ca3837f40b401f # This will run the ddns.py script every 1 minute to detect if the ip changes and will update the domain (so the domain always points to the current ip address of the lappie/pc (of the router actually).

^The ddns.py script can be found here

Handy things to remember

ln -s /PATH/TO/SOMETHING/REAL /usr/local/bin/NAMEWANTED # can use executable file with preferred bind name
CTRL r # to search for previously used COMMAND
CTRL p # shows previously used COMMAND
CTRL a # brings you to begin of line
CTRL e # brings to the end of line
tail -f FILENAME # this will follow the file while it's being edited
echo "some kind of text" > example.text # this will overwrite the .text file with the quoted line
echo "new line of text" >> example.text # this will write the quoted line on a new line
alias COMMANDNAME="DESIREDNAME" # this will temporarily save a command to the desired name
echo "alias COMMANDNAME=\"DESIREDNAME\";" >> ~/.bashrc # this will permanently save a desired command to the bash file
&& # chain commands together
convert example.png example.jpg book.pdf # Converting files to PDF

> means to overwrite
>> means to append


Examples of Things

ls -alh /home/user/some/random/place >> ANYNAME # cat ANYNAME to see it show the directory with info
grep WHATEVERSEARCHINGFOR ./* # Useful for findings things.. If successful this will print out what it finds in the shell


Random Password Generators

openssl rand -base64 32 # generates random 32 character password
</dev/urandom tr -dc 'A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~' | head -c 10 ; echo # Sauls random character/symbol generated password command