User Tools

Site Tools


linux

Table of Contents

Linux

Welcome to Jack Heart's secret Linux and FOSS Page. This page was made as a way to keep notes about Linux systems and various open source packages that work with linux. Please Enjoy your stay. Please read at least the next section about assumptions of what you know.

Assumptions of what You know

This page assumes that:

  1. You know how to search things for understanding, rather then just copying what is written here. This page is anti rote
  2. You have installed linux or have a live version of linux.
  3. You are not afraid of command line, or afraid learning command line.
  4. You understand that these are basic simple notes, not extensive tutorials.

There are so many other links, but this seems to be a nice one. Here is a link to a complex comprehensive how to on linux. Feel free to scrap my page and use these guides, which are written by others way more experienced then I.

Terminal

Terminal is the software that allows for the user to use a shell command line interface where they can type commands that are interpreted by the computer operating system, and used to perform functions. Another word for terminal is console. There are primarily 3 ways of accessing a terminal:

  • Using a terminal emulator. This is most typical if you are running a linux distribution (Mint, Gentoo, Knoppix, Puppy, etc) with a Linux desktop environment (Gnome, LDXE, openbox, etc). Usually you can access this by clicking on an icon that looks like a black box. Most distros have this easily accessed as a shortcut or in the main menu.
  • Using a console directly. This is done with Distros that have no desktop environment, or by switching to it while in a DE with something like Alt + Ctrl + F1. This is typically running xterm. To swtich Pack Press Ctrl + Alt + F7
  • Using a remote terminal. This is for network, core, or light distros (Light versions of slack, arch, debian, centos, etc.) which often come pre-installed with a network configuration to access remotely from another computer with a program (SSH, putty, etc)

Shell

A Shell is a program or package that interprets commands and executes them based on the language of the shell. Shell command language is the language and format of how you type in the terminal.

Command Prompt

In a terminal window, there is a command prompt which uses a shell command language such as bash. Here is how it looks:

user ~ $

Here is a breakdown of a common command prompt elements

  • user could be many different things depending on how you are accessing terminal
  • ~ is usually the working directory. It could be something like /home/user/downloads
  • $ is where you type. Everything you type after $ is the command
  • NOTE: In various linux distros, the shell command could be different then $ and that sometimes $ means a regular user and # means admin user. For example, in Mint it will be $ for the regular user. But if you login, possibly by typing $sudo su, and put in your password, it will change to #.
  • On this page, we will always start with $ regardless of user privilege. Some commands may need to be preceded by sudo. More about sudo below.
  • On this page I follow bash, unless otherwise specified.

Mendiculus Format

On this page the commands will be in gray boxes. This is what you would type after the $ or #. Sometimes the prompt syntax is included in the box, but the idea is you can copy the box easily

Shell Syntax

Syntax is the formula and rules made up of a word, words, symbols, combinations and organizations that used in the proper order will perform a command

Execute a command

Once you are in the terminal, and have entered a command, you need to hit Enter to execute it. Here is an Example where the user would type “echo Hello world” and then hit the Enter Key:

User ~ $echo Hello world
Hello world
User ~ $

After the command is executed, the shell is ready for another command.

Words and commands

Here are some example commands
The echo command displays text. it would normally be followed by the text to display.

echo

ls is a simple command which means list. This will list all the files in your current directory. Many of the commands are abreviated or seem to not represent what they do at all.

ls

This command means you are going to use the package apt-get, which is really called Advanced Package tool with a second aspect of get. There could be other versions like apt-cache which does something different.

apt-get

Spaces

Most commands require spaces between the different commands, options, operators, etc. This command means you will be changing directory, and documents is the directory to change to.

cd documents

Options

Options modify behavior. A Dash ( - ) is usually an option followed by a letter or letters. This is not exclusive. The command, option, argument, and/or parameter are all separated by spaces.
This command will list all files in the current directory, showing also hidden files and folders (try just ls for example.)

ls -a

You can often combine multiple options. This command will list all the files in the current directory, and give you a human readable size format.

ls -a -h

You can also execute it as:

ls -ah

Options and parameters

Many options allow you to enter in a value or string after the option, and often it is required. example:

ping 8.8.8.8 -i 5

This will ping the IP address 8.8.8.8 at 5 second intervals. If you do just the -i, you will get a syntax error. And most commands have a default parameter, in pings case, the interval is 1 so if you do not specify, it will do intervals every 1 second.

Arguments

Arguments are items which the command acts on. Also separated by space. For example, you could do ls [dir] and output will list that directory instead of your current directory. This command will output all files in /etc/ regardless of your current directory

ls /etc/

Brackets

In this document, and many other linux documents, brackets [] indicate options. Example:

ls [options, -a, etc]

This means you have some options, which either are listed in the example, or listed below the example of the different options. Another way indicated is angle brackets <>

Directories

Directories are discussed more below, but sometimes a directory might be indicated in an example, or as the language of a command. Often it might just be something like /dir, or <path> or just “directory”. This should be easy to interpret. Just remember that you DO NOT always use the example given.

Terminal Conclusion

This concludes the section about command line and how it is used. From here on out are notes about the different commands, tools, packages and tips on how to use terminal.

Commands

What are commands?
WIP

Manual Page

One of the most useful tools is to view the manual of function, package, or command and all of its options is man.

man [command]

For example, this command will show you the manual for the command echo:

man echo

Your terminal window will now open the document you can scroll up and down, read and copy if you want. To quite out of manual just hit q. Sometimes it is easy to open a second terminal window just to have the man page up. Most man pages are located in /usr/share/man. And there is a command to figure out its location, but an even easier way to examine the man page is to save it as a text file in your current working directory and then view it by a word program. You can save the man by doing > [file] after the command. This way you can easily open it with a word program in your current directory. For example

man fdisk > manfdisk.txt 

Another way to view the man page is to do a web search with the same syntax as the bash command

Distributions

Linux is a general term used to describe a system used by many distributions. A distribution (distro) is a kind of operating systems that functions in the linux system. For example, here are some distributions:

  • Linux Mint - This is Jack Hearts favorite distro and a very common and popular one. It is based off Ubuntu, which is based off Debian.
  • Ubuntu - A very popular distro and has a lot of different types and forks. It is based on Debian
  • Debian - A core distro that many other distros are based off
  • Fedora - This distro is based off Red hat and is meant for personal and home use
  • CentOS - A comunity based enterprise OS
  • Red Hat Enterprise Linux - A core distro maintained by red hat and used mainly for business models
  • Kali - A distribution with a collection of security and forensics tools. Very good for Live USB testing
  • Puppy Linux - A very light distribution that works well on very old computers

There are so many different distros, version, and forks of distros. A very good website to learn about the distros is https://distrowatch.com.

This wiki

Many of the tuturials on this wiki work well with debian and a lot of RHEL systems. A lot of tools are very universal, such as cd, or ls. One thing to keep in mind is how the distributions work. A key way of thinking is:
Debian is a linux distribution, but a linux distribution is not always Debian

Files and Folders

File Structure in Unix

The file structure listed here is generally for Debian. But many situations are similar in other distributions.

System Locations

Here are some locations pertaining to the linux system:

  • /etc/[various] This is usually where the config files of specific packages are hel
  • executables go in /usr/bin, /usr/local/bin or /bin. Some also go to /sbin or /usr/sbin. anything in these files is only executable by root. U can still type /sbin/programname but they dotn work if u just type the name at a terminal.
  • the actual programs go (usualy) in /usr/share or /usr/local although many will install some extra shared libraries are in /lib
  • /opt/ This is a directory for stand alone type programs that do not need to be installed in other directories
  • /media or /mnt or /dev This is where auto mount or fstab mounts usually go.
  • /var/log/ This is the locations of various logs
  • /dev/mem/ This can be the location for the memory and paired with dd, you can do a memory dump.

Home Locations

Here are locations pertaining to the user:

  • /home/[user]/user files This is where user files are. also:
  • /home/[user]/.[config] some config files are in the home directory, and have a . in front to hide them. These are config files not in the regular config file location

Additionally, don't go trying to remove a program 1 file at a time. Using packages managers and instructions from there will help eliminate program files.

Directories

When you are in command line, you are always in a specific directory. This is indicated by the terminal command. Here is an example of how it may look. You would type prompts after the $

user1@host:/home/user1/Documents$

This means you are in the /home/user1/Documents directory. Not all systems will display the entire directory you are in

Working Directory

pwd stands for print working directory. THis will tell you where your working directory is

pwd

List

ls Lists files in directory. The default list will show you your current working directory

ls [option]

Options:

  • -a show hidden files also
  • -1 (number 1) shows a list but just the names
  • -l (letter l) shows extensive details
  • -h human readable format
  • -F shows folders
  • -t sorts by time modified showing newest first

Here is a great common way of using ls:

ls -lha

Change Directory

cd [/dir,$dir,.,..,etc] cd is followed by what directory you want to go to this will send to the home directory:

cd ~

This will send to subfolder of current directory. You do not need to include/ unless its a secondary sub directory:

cd <subfolder>

To Change to the parent directory of wherever you are at, aka go up a level, use the ..

cd ..

More info about cd can be found at http://www.linfo.org/cd.html

Copy

Copy works like a lot of the other file commands.

cp <file/dir> <newfile/dir>

You can also copy all the contents of a folder, but not the folder itself by doing:

cp /dir/* /newdir

A little trick with cp is you can use it to zero out or truncate a file without changing its location or permissions by copying dev/null to it.

cp /dev/null /path/to/big/ass/file

More about this technique can be found here: https://unix.stackexchange.com/questions/305017/cp-large-file-to-dev-null-to-reduce-size-to-zero

Move

Move works the same way as copy

mv /dir /newdir

Rename

For renaming, you need to use move command

mv [original filename] [newfilename]

It is good to use the -T flag when using rename to help with not having folders move to sub folders

mv -T /home/user/oldfolder /home/user/newfoldername

Make a backup

you an do the same for rename, but use cp to make a copy. ex:

cp somethingcfg somethingcfg.backup

Remove

Remove is to delete. It does not go to trash unless you have a trash option configured. be very careful with this command

rm [-option] [file]

Options:

  • -r removes directories with a check at eatch sub directory
  • -f removes without check

Spaces in File and folder names

Usually a space denotes the next chain of commands or options, but if a file or folder has a space, this can be problematic (which is why anything set or changed by you should usually have no spaces, and is best to be all lower case) But you can indicate spaces with \. Here is an example:

mv /home/user/The\ file.rtf /home/user/documents/

Note that you still do the space after the \

Editing from command line

  • sed
  • awk

Disks and Drives

Disk Free

Disk Free shows you how much is free in the main systems.

df [opts] <dir>

Options:

  • -h Shows a human readable format to the complete root system
  • -i shows inodes (not that anybody would ever really need to know this)

You can indicate the directory you want to view, but a good common tools is to use /. for example:

df -h /

Directory Used space

Disk Usage is about seeing what is used in a directory

du [options] <dir>

Options:

  • -s just current dir
  • -h human readable format
  • -d [#] (or –max-depth=x) is set to max depth to display. 0 will show just director, 1 will show all subfolders, 2 will show all sub sub folders, etc.
  • -b [block size] Will show you what size you want to use. M for megabytes
  • -m will show size in Megabytes, but if you do -h it shows the M

Note, simply doing du will list every folder and its size and could take time to show. Here is a very good example how to find the size of a specific folder and all its contents:

du -sB M [/target/directory]

You May want to make a text file with this example. This will not make a loop because it makes the file first from the readout, then places it in that location.

du /home/user > /home/User/dureadout.txt

Another good common example to show you all subfolders sizes in megabytes and sort by size is:

du -h --max-depth=1 /home/user/ | sort -n

File System Table

File system table or fstab, is a file that tells the system what to mount during startup or when reloaded into the overall linux configuration. It is located in /etc/fstab and can be edited. Here is the basic idea behind each line with spaces between each option:

  • <device> <mountpoint> <fstype> <options> <dump> <pass>

Examples:

/dev/sda /mnt/external ext2 ro,nouser 0 2
UUID=<number> /home/ext1 vfat defaults 0 0

Each parameters option is listed here:

  • Device: can be found from fdisk -l, can be file system (dec/sdx/, etc), label or UUID
  • mountpoint: where it mounts to
  • fstype: also found from fdisk -l, (ext2,3,4, use vfat for Fat32)
  • options: See “mount” manual for options, but can usually just do defaults. Also, options are separated by commas. A good one to know is simply user. This will allow the non root user to rw of the folder.
  • dump: to make a backup, but must have dump installed, so usually 0
  • pass (fsck): determines order file systems should be checked. Can be 0,1,2. The root file system should have the highest priority, 1, all other file systems you want to have checked should get a 2. File systems with a <pass> value 0 will not be checked by the fsck utility.

Fstab tips

Once you are done editing the fstab you can restart your computer, or run this command to have the computer mount all that is in it:

mount -a

Spaces in fstab folder or file names are not easily identified with the standard \ charactor. Instead you have to use \040. Example:

  • /shared/the folder/main
  • Should be in fstab as:
  • /shared/the\040folder/main

Universally Unique identifier - UUID

To list all UUID and file types, do:

blkid

Disks and drives

Fdisk is a very good utility to list drives active on the computer, regardless of how they are formatted or mounted.

fdisk [option]

Options:

  • -l lists all drives,

Using -l is a very useful option.

fdisk -l

Hardware readout about drives

One thing you may want to do is figure out if a drive is a Solid state or not. you can find out if it is Rotational with this:

cat /sys/block/sda/queue/rotational

Replace sda with the desired drive. if this command returns a 0, it is not rotational, 1 if it is.

Mount

Mount lets you mount external drives that do not get mounted automatically

mount [options] </device/dir/> </mount/dir>

Options:

  • -a will mount all files systems
  • -t [type] will let you say type such as vfat, hfsplus, ext3. Mount can usually detect what type

Tip: make a directory where you want to mount before mounting. This is often in /mnt. But can really go anywere, baring permissions.

Unmount

Unmount lets you dismount a folder that may have been mounted through the mount command or fstab.

umount [dir]

Options:

  • -a unmounts all

Troubleshooting. if you get the error: umount.nfs: [dir]: device is busy. Try this: http://oletange.blogspot.com/2012/04/umount-device-is-busy-why.html

Disks and images

dd writes data to a disk or file. It does it mostly bit for bit. It is good for creating or writing disk image files from or to disks. It is also very good for wiping free or entire data.

dd if=[input file/folder] of=[output file/folder] [options]

Options:

  • oflag=direct this will avoid disk cash
  • status=[options] use this to see status. you can do status=progress to see trasfer rate and an interval of status

Input and output files are directories but can also be entire partitions and drive. Most dd practices are for writing to entire drives. A common practice is to write bootable linux images or to wipe a drive.

Overwrite entire drive

You can fill a drive with all zeros with dd. This may take a while, as it is making every bit of data 0. You just make the output folder the directory of the drive. You can use fdisk to figure out the drive folder. Here is an example where the external drive is /deve/sdb/:

dd if=/dev/zero of=/dev/sdb bs=1M

If you are wiping your hard drive for security, you should populate it with random data rather than zeros. This is going to take even longer than the first example.

dd if=/dev/urandom of=/dev/sda bs=1M

wipe free space

If you want to just write random data to any free space you can run this command. It is custom and a best practice to wipe just free space. The entire purpose here is the write random data to parts of a disk that are marked for overwriting. In most drive systems, when you delete a file, the actual drive system only marks the area of the drive as available to be written over, but does not actually delete the data. This procedure below will write over all that data and then by deleting the files, allow that space to be written over. This is very helpful because there are packages such as testdisk and photorec that can recover data. Below is an explanation of each command by line:

dd if=/dev/urandom of=r.small.file bs=1024 count=102400
dd if=/dev/urandom of=r.file bs=1024
sync ; sleep 60 ; sync
rm r.small.file
rm r.file

Explanation: If using this method, it creates 2 files, one that will fill the entire empty space, and one as a kind of buffer.

  • Line 1 creates a small file that acts as a buffer. By saying count=102400 you are saying the file can only be that size. The purpose of the small file is to reduce the time when free space is completely zero.
  • Line 2 creates the file that fills the rest of the entire drive with no limit in its size other then what is available.
  • Line 3 the purpose of sync is to make sure the data is actually written.
  • Line 4 removes the small file and gives the Linux drive system room for drive memory to process the next request.
  • Line 5 removes the large file

Remote dd

You can use this command to remote dd with ssh. since dd just makes a copy, it can be joined with a pipe to do other things. In this example it creates a high compressed version with gzip, but for pure copies, you can remove that part:

dd if=/dev/sdb | gzip -c --fast | ssh user@ip 'dd of=/home/user/sdb.img.gz'

See Progress

Just use the option “status=progress” to see the status. For example:

dd if=somefile of=otherfile status=progress

If for some reason that does not work, you can use this work around for seeing the status. Do this command in a different terminal

kill -USR1 [pid of dd]

Combine this command with the command “watch” (see below) and this will give you interval status instead of just once. But this is stupid and you should just use the status option.

Manual cloning

smartctl

Most modern disks have SMART disk diagnostics built into the drive firmware. You can install the smartctl software to see diagnostics and perform tests. Read more here: https://www.thomas-krenn.com/en/wiki/Analyzing_a_Faulty_Hard_Disk_using_Smartctl. This comand shows a lot of info about a target drive.

smartctl -a [dev/sdX]

And if you want to run a test

smartctl -t short [drive]

badblocks

Badblocks is used for checking and indexing bad blocks of a hard drive. This will also attempt to fix bad blocks by writing over them and checking if that data is correct.

badblocks [-options] [target drive]

Some common options are:

  • -n non destructive mode, loads data into memory, then writes it and checks if error
  • -w destructive mode, for writing random data to block, reading it, then writing back and check for errors
  • -s shows progress in percent
  • -v verbose output
  • -o [name of file] output a file which (supposedly) could be used later with e2fsck to build a file system around the bad blocks

example for checking an already formatted drive. This will cause you to loose all your data:

badblocks -wsv -o badblocksindex /dev/sdc

Terminal Tools and Tricks

Bash

Bash is the most commonly used shell command system, though there are other like zsh. Once you have accessed a terminal, you can type in commands with a specific command line format. Almost all Linux based distros are going to use the same language, even with zsh or others.

Custom bash commands

You can easily add a custom command to bash by editing the user bash config file located at ~/.bashrc
Just add lines to bottom of that file

alias [customcommand]='[commmand]'

For example, adding this line will make it so when you type remote host, it will execute the ssh command in single quotes

alias remotehost='ssh user@remotehost.net'

When finished you may need to reload the bash rc for the command to work:

. ~/.bashrc

(yes, this is correct with the . as the first part of the command)
To list paths of scripts/commands can do

echo $PATH

Stop a terminal process

If you are running something in terminal that continues but you you want to stop and go back to command line, you can invoke a command to terminate the child process. In some instances you may want to try other things to exit the process, but in many cases you can always hit ctrl + C. Sometimes this is not a good thing, like if you are running an update. Other times it is perfectly acceptable like if you do a ping without number of pings.

copy and paste in terminal

Different programs offer different ways to copy and past from terminal and into terminal. Many of the terminal programs let you right click or choose from terminal window menus.

up and history

Looking back at previous commands can be helpful. You can use history to shows history of users commands history You can also press the up key to go to the previous commands one at a time.

Common Options

These are common options followed by a command.

  • -h Help
  • -v or -vv verbose
  • -l list

Terminal Operators

These are signals you can include in your command to do certain fuctions. Most of these are Bash or similar type operators.

and then

The symbol && will do a command, then do the next command. This example will display text, wait 5 seconds, do the next echo, then go back to terminal:

echo "hello world" && sleep 5 && echo "your still here?"

You can use this for chaining command together. Not grep or pipe (see below for that)

Wildcards

Wildcards are a kind of regular expression (see next section) and are very usefully when looking for text in a file, or the file name. Not all commands allow wildcards, but here are some examples that do.

Asterisk *

This is a wildcard used to fill in any number of characters, or even none. For example this command will list any file in the current directory that has the .txt format:

ls | grep *.txt

This will list any file in the current dir that start with January

ls | grep January*

You can put them together also.

ls | grep January*2016*

Regular expressions

Regular expressions are special characters for describing a text pattern. These characters are called meta characters. This is a very powerful tool and often used with grep, sed, awk. There is a lot of information about them, but for this tutorial, we can just talk about a few.

Brackets and first example

brackets will search through a certain range of characters. This example will return results where the word might have those 2 options for the letter n as upper case N and lower case n

grep [nN]ame

you can use dashes to define a range. In this example the search will go through all a-z lower and upper, and all 0-9. You do not need to separate commase:

[a-zA-Z0-9]

interval expressions

Curly Brackets tell you how many times to search for the letters

  • Braces (Curly Braces, Curly Brackets) ex:
  • {1,8}
  • This will set it to go through the letters 1-8 characters. ex:
  • [aAeE]{1,2}
  • Will search for all the character combination, where it exists 1 or 2 times. so it can find all of the following:
  • a, A, e, E, aa, aA, ae, aE, Aa, AA, Ae, AE, ea, eA, ee, eE, Ea, EA, Ee, EE

More to come

More about regular expressions to come

references

Tab Auto complete

When typing in terminal you can start typing one or more letter to a command and press tab to either auto complete or get a list of options for auto complete. Note, if you do not have enough information, and depending on the linux system you are using, pressing tab will not always auto complete.

cat

Cat is a way to read out files to terminal, but not actually open them (like with vi). It stands for Concatenate and makes readouts simpler.

cat [file]

Options

  • -n options for showing the line numbers.
  • -b shows all non empty line numbers

This will show you the file, but only the lines containing the grep string:

cat [file] | grep [string]

less

Less works the same as cat but starts with the beginning of the file, and you have to press return to see the next lines, and quite to exit back to command prompt (like reading a manual)

less [file]

cut

Cut is like cat, but lets you remove sections of what is seen

cut [opts] [file]

Options:

  • -c select only these number of characters. as in -c4 will only show the 4th character. Can also do combos like c1-3 or c1,3
  • -d'[delimiter]' sets the delimiter, such as space (-d' ') or comma (-d',')
  • -f sets fields, as in only show these fields which have been determined by the delimiter. if no delimiter, then will treat each line as 1 field

Send To file

Sometimes you may want to print out the processes of the readout. You use the right arrow > character to do this.

[command with operators] > /dir/text.txt

If tou want to append to the end of an existing file, you use two arrows

[command with operators] >> textfile.txt

tee

tell lets you output to a file and show output and write to file. Usually used with pipes.

[command with readouot] | tee [dir/file]

Here is a good example and use for this that will print out the dmesg, but also send it to a file in the local directory named dmesg1

dmesg | tee dmesg1

watch

Watch lets you execute a command at a certain interval. It is usually in most linux systems, but may need to be installed. It is very good for seeing readouts at intervals.

watch [-n #] [command]

Options:

  • -n tells it to watch in seconds
  • You put the command you want to watch at the end

A very good example of this is to watch the status of dd. You can see the command starts with kill:

watch -n [#] kill -USR1 [pidof dd]

screen

Screen allows for you to open sessions as a daemon so the terminal window does not need to be open and you can switch between different terminals. Screen is a very useful tool if you are going to do something that will take a long time, like transferring a lot of data. Note that screen is also a serial terminal program so depending on distributions, it may be different. It is the default for ubuntu.

To start the session:

screen

This starts a new screen session. It will look like regular terminal but is infarct a different screen window. When in screen, to do things you just do:

Ctr+a [option]

Options

  • c creates a new screen window
  • n or p moves to the next/previous screen
  • 0,1,… Switch to that window
  • A rename a window
  • S or | to split the screen horizontally or vertically
  • tab to switch focus
  • Q close all regions but the current one, but does not close the windows
  • X closes the current region
  • k kills current screen
  • \ kills all screens
  • “ lets you select a window to go to (does not always work???)
  • d detaches from screen and brings you back to regular terminal, but those screens are still running.

When back in terminal you can go back aka reattach to the screen sessions with do:

screen -r

Also back in main terminal you can list all the current screen sessions with:

screen -ls

pickup of space reformatting reediting redditing space project

tail

Tail lets you look at the last 10 lines of a file, which can be helpful for looking at log files. Details:http://www.computerhope.com/unix/utail.htm

$tail [-options] [file]

* -f monitors file and shows updates or changes
* -n [x] shows you the last lines based on x, NOTE, this goes after the file part of the command

A usefull example is something like:

$tail -f access.log | grep good

this reads access.log and pipes to grep to only show lines containing good (like status:good)

Sleep

You can wait for something such as the above example &&. The number after sleep is seconds

  • $sleep [number in seconds]
  • example of where this is usufull:
  • $ifdown eth0 && sleep 5 && ifup eth0

Pipes

Pipes send one function to another. read more at:

Should do more research here: http://linux.about.com/od/commands/l/blcmdl1_ln.htm

  • $ln [-options] target [-options] [linkname]
  • -s for symbolic, default is hard links. Symbolic links are more like a shortcut and can go to directories. THis is generally preferred.
  • Hard Links point to the inode and can not be directories.

Environment Variable

Environemtal Variables (ev) This is a variable that persists for the life of a terminal session. You can see them with:

  • $export
  • shows all the current environment variables
  • $export [EV]=[new variable]
  • Do this to change an ev.
  • ex:
  • $export PATH=/usr/local/bin:/usr/bin:/sbin:/usr/sbin:.
  • This will change what path variables are used for commands, and remove /bin. A good example is that since ls lives in /bin/ls then when doing this command, it can not be executed simply with
  • $ls
  • -bash: ls: command not found
  • To append the current ev:
  • $export PATH=“$PATH:[new/path]”


WARNING. Only change the path variable if you know what you are doing. https://www.cs.purdue.edu/homes/bb/cs348/www-S08/unix_path.html

Readouts and info

Status

Linux uses the unix epoch time which you can see here: https://www.epochconverter.com/clock

Date and time

This will display the date and time

date

To see the time and date in a specific format do:

date +[FORMATS]

You can also use some different formats. Here is a link to a list: https://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/
ALso, here is a format you might like:

(%s) %Z %z [%V/52] [%w/7] [%m/12] %a %b %F %T

calendar

  • $cal

free

To read out free memory and memory info

  • $free

top

Top is a simple readout of tasks and performance. for more advanced cli readout, use htop

  • $top

vmstat

vmstat is simple show of some of the processor info.

  • $vmstat [options] [delay#] [count#]
  • You can show it just once by simply doing vmstat with no options. Here are options
  • -S [charactor] changes output to a different bit format. ex:
  • $vmstat -S M
  • This will read out some of the data numbers in terms of Megabytes
  • Delay is how often you want to show readout
  • Count is how many times to show readout. ex:
  • $vmstat 1 20
  • This will show the readout every 1 second, 20 times.

system info

uname

Gives main system readout of kernal information including x86 vs x86_64, etc

  • $uname -a

uptime

  • $uptime
  • Shows you current time, uptime in hours and minutes, and other stuff.
  • $w
  • Shows more details about users and their uptimes

lspci

Simple message of components and system stats

  • $lspci

lsusb

lists usb information

  • $lsusb

lsmod

  • $lsmod
  • shows kernal moduals loaded

information files in Proc

Proc files system is a directory in linux that has files with information. The files in the /proc directory are not real files, but rather hooks to look at information available to the kernel. However you can use cat to get some file information.

  • $cat /proc/[file]

This command shows you the information of that file. here are what many of them will display if you replace [file] with these:

  • cpuinfo - Note that CPU info
  • meminfo - Memory info
  • version - Linux version and other info
  • filesystems - Show the types of filesystems currently in use.
  • partitions - Shows partition information

There are many other readouts you can gather from proc. https://en.wikipedia.org/wiki/Procfs

Specific Versions

Based on distros Ubuntu

lsb_release -d

linux mint

inxi -Fxz

debian

less /etc/debian_version

Fedora and redhat

cat /etc/fedora-release
cat /etc/redhat-release

Puppy

cat /etc/DISTRO_SPECS

Run Paths

Run paths are the locations your compand system (bash, zshel, etc) look in for basic commands. For finding run paths can do

  • $echo $PATH
  • Will be seperated by :'s
  • ex:
  • usr/local/bin:/usr/bin:/bin
  • If you look in /bin, you might find things like dd, rm, su, ls, etc

Advanced system readout

dmesg

dmesg (display message or driver message) is a command on most Unix-like operating systems that prints the message buffer of the kernel.

  • $dmesg
  • Gives full redout
  • $dmesg -wH
  • w is watch live readout
  • H is human readable format

dmidecode

THis is a super complex readout.

  • $dmidecode
  • Gives full readout
  • $dmidecode -t [#]
  • -t tells it to only show that type based on number. Use man dmidecode to see a list of what numbers do what, or:
  • $dmidecode –type [readable format name]
  • Where readable format name could be: memory, processor, system, bios, etc
  • Note, that this is not for non x86 (32 or 64) systems, such as arm

lshw

Also a very good system readout command. Will work on non x86 systems. Probably need to instal

  • $lshw

last

  • $last
  • shows you the last logged info for various things like reboots, etc

Check for sysv or SystemD

  • Check Sys V
  • $pidof /sbin/init && echo “sysvinit” || echo “other”
  • Check for systemd
  • $pidof systemd && echo “systemd” || echo “other”

Searching

Find

Find is the most basic search option. It searches file names and folders, but not actual files, use grep for that

find [directory dirs] [expression] [-options]

Options:

  • Default search in current dir, or can do / for root, or . for current and below.
  • -name '[word]' or -iname Case insensitive
  • -type [x] where x could be: f regular file, d directory, l symbolic link, etc seem man. This is good for just finding files, and not their director, especially if executing a move command (see below for -exec)
  • Expression can be anything, but if you want to combind them you need to put them in \( …expression \)
  • Can do * as a wildcard, or ? as wildcard but only that many symbols, Can use this wild card in any combination
  • Here is an example to find any file that starts with “file” and can end with anything:

find / -name 'file.*' -type f

  • -o is the or command, to do multiple searches. ex:

find *.jpg -o *.jpg

Find -exec

A useful tool is to use the -exec option with find.

find [expression] -exec [bash command] {} \;

Explanation

  • The {} (braces) are what is returned from the find command, you can actually do something like:

find [expression] -exec [bash command] {} [continued bash command]

* the ending \; ends the bash command
* A good example of how to find all of a file type and move to a dir is:
find / -iname *.jpg -type f -exec mv {} /home/user/jpg \;

Locate

Locate is a lot faster then find, but has less options.

locate [expression]

Grep

Grep is a kind of advanced search where it searches INSIDE the file, not file names or folders. See this link for more: http://www.codecoffee.com/tipsforlinux/articles/25.html grep [-options] [search field] [director] Options

  • -A [#] to see the search string and that number of lines after
  • -B [#] to see the search string and that number of lines before
  • -r for recursive to look through files and directories

Another advanced option is:

[command with readout] | grep [-options] [search field]

Examples: If you want to only show NON commented out lines, here is a nice little trick:

[readout command] | grep -v '^$\|^\s*\#'

An alternative way do do this if that does not work is to use egrep (this is not perfectly tested):

[readout command like cat] | egrep -v -E "^\s*(#|$)"

This option works very well for most systems and a trick is to add it to your .bashrc file. For example you would add this line to that file:

alias grepc='grep -v "^$\|^\s*\#"'

Notice that the quotes are a little different because it is being interpreted from a file, rather then direct from command line.

Sort

Sort is like grep is a good way to sort an output.

  • $sort [opts]
  • without options, it sorts by Alphabetically
  • -n sorts by number

Usually used with a pipe. Ex:

  • $ls | sort
  • This will list working directory contents in alphabetically

whereis

  • $whereis [file]
  • Lists where that package with that exact name are. Need to use grep or pipes for better locating tools

System Commands

Here are some main system commands.

shutdown

A very common command for shutting down and restart

shutdown [option] [time]

Options:

  • -h for hault for shutdown
  • -r for restarting
  • Time is to say when, such as 10s for 10 seconds or now for doing it instantly. you can also type words such as now.

This example shutdown command is very simple and will restart the computer immediately after you press enter:

shutdown -r now

systemctl power options

if you have a systemd system:

systemctl [power options]

Here are some power options:

Ntp

If you are online, you can set the ntp server:

ntpdate [server]

A common server to use is us.pool.ntp.org. You can also set it to the hwclock with:

$hwclock --systohc

(this is untested)

timedatectl

For fedora to see avialable time zones:

timedatectl list-timezones

Find the timezone you want to use exactly as outputed in list. This is case sensative and you can use the / icon. You can also tab autocomlete.

timedatectl set-timezone [zone]

Edit Text

vi

Vi is a powerful text editing tool in prety much every linux distro. Originally pronounced Vee-Eye.

  • $vi [file or /dir/file]

Vi does not work like popular word processors and you should refer to various web pages on how to use it.

There are three Modes:

  • Navigation - When you are moving around and doing key commands
  • Edit - When in nav mode, pressing i or a
  • Command - When in nav mode, pressing : and then entering a command
  • hjkl to navigate left, up, down, right respectivly
  • 1G Move to the first line of the file
  • G Move to the last line of the file
  • H Move to top of screen
  • M Move to middle of screen
  • L Move to botton of screen
  • ?[text] to find text
  • When finding text, n to move to next entry, N to move back

Edit

  • i to insert, a to append
  • esc to exit editing text
  • x to delete a character
  • dd to delete a line, or #dd to delete that many lines
  • o open a new line after cursor

Copy and Paste

In Nav mode:

  • #yy to copy that many lines
  • yw to yank from cursor to end of word
  • y$ to yank from cursor to end of line
  • p to paste below cursor

Find and replace

Another way to find things is:

  • :s/[string]
  • This only does on the line you are on.
  • :s/[string]/[newstring]/
  • This is for replacing, but only on the line you are on
  • :%s/[string]/[newstring]/
  • Be carefull, but this will replace all

Command Mode / Saving

When NOT in text mode you can type colon : to do certain commands such as:

  • :q! to exit without saving
  • :wq to save

There are many other commands, but those are the ones I have memorized and will probably be enough to get you around/get you in trouble.

Nano

A much easier text editing tool. Still learning

General

  • Ctrl+O Write (output) the current contents of the text buffer to a file.
  • Ctrl+G Get help; this provides information on navigating through files and common keyboard commands.
  • Ctrl+X Exit the editor. If you've edited text without saving, you'll be prompted as to whether you really want to exit.

Editing

  • Select: Ctl + ^ OR Alt + a This start the selection mode, press again to end selection mode.
  • Copy: Alt ^ Must do during selection mode
  • Cut: Ctrl k Must do during selection mode
  • Paste: Ctrl u
  • Ctrl+W Find a word or phrase. At the prompt, use the cursor keys to go through previous search terms, or hit Ctrl+R to move into replace mode. Alternatively you can hit Ctrl+T to go to a specific line.
  • Alt+U and Alt+E for undo and redo
  • Ctl+k to remove a line (not in selction mode)

sed

stream editor. You can use this to change text. you can play with this by piping from cat or echo (examples below) But here are the basics:

  • $sed [option]/old/new/
  • s for substitution
  • adding /g at end will be recursive

Note: for when you want / in the word you can use a different character for the /, such as | or _. As in:

  • $sed s/old/new/
  • $sed s|old|new|
  • Those do the same thing

There are many other tools for sed, but you can play with them from pipes.

  • $echo hello world | sed s/hello/goodbye/
  • THis should then readout:
  • goodbye world

Now you can see that you can change words in documents with sed and by using the > to export to a document. For example say you had a document you wanted to change all the words, like instead of saying “John” you wanted it to say “Mary”

  • $cat document.txt | sed s/John/Mary/ > newdocument.txt

You can also combine with grep so that it will only edit hte lines grep finds:

Users

An easy way to see all users is look in the Home directory and see the list of users.

User Tools

To see users view file in /etc/shadow. This shows you system users also so can be a bit big. For regular users you might just look in home dir for home users. Here are some other user commands:

  • $adduser [new user]
  • To add a user with the default basic privilages
  • $userdel [user]
  • Delete a user
  • $usermod
  • To change things about a user, such as groups with the -Ga option
  • $su [user]
  • Substitute user, helpful when ssh

sudo

Sudo means super user do. This is what you use when you are a basic user but want to do something with elevated privileges. Usually you will be promoted with a password of the super user. To edit the sudoers file use visudo:

  • $visudo

Groups

  • $groups [user]
  • will list what group a user is in
  • $groupadd [new group name]

Readouts

To see all users, even non people users

/etc/shadow
Debian $ ??

Security and Encryption

passwd

Can use passwd to change your password.

  • $passwd [user]
  • This will prompt you for the password.
  • You can do it without user and it will change the password of the current user.

File encryption

chmod

To see a list of all the permissions of a file do:

  • $ls -l [file]
  • you dont have to do the file, it will just show all files in dir if that part is absent

THe most powerful tool for viewing and changing permissions is chmod:

  • chmod [options] [file]

Keys pgp gpg

Some packages, software, etc need keyes you may use gpg as a tool. in this example we will copy a key from another place using a pipe . like a debian key from the main debian keyring, to the apt-key list.

  • $gpg –keyring /usr/share/keyrings/debian-keyring.gpg -a –export 21E764DF | sudo apt-key add -

Network

Network Interfaces

Network interfaces have a name and many commands will default to an interface, or require you to say which interface you want to use. There are a few different ways to list all interfaces and their status. one common ways is:

ifconfig -a

Here are some examples:

  • lo Loopback interface. Important for internal network communications
  • eth0 First Ethernet interface. additional will be eth1, eth2, etc.
  • wlan0 First Wireless interface.
  • bridge0 a representation of a bridged interface, usually consisting of multiple physical or virtual interfaces
  • vlan0 a vlan interface
  • ath0 sometimes represented in openwrt distros or from interfaces of the atheros brand
  • e* is predictable network interface names. Starting with System D version 197 the name of hardware devices was changed to represent a more physical location name, for example enp2s0. You can still use this name just like everything else. For more info on this: https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/

ifconfig

With linux networking, ifconfig allows you to make some changes and get some info about the networking. For a readout:

ifconfig

You can temporarily set the IP by using:

ifconfig [iface] [ipaddress] netmask [255.255.255.0] up

Then you need to set the gateway with route:

route add default gw 192.168.99.254

These settings will be lost once the computer restarts

network interface config file

Of course, you may want to set the ip manually. to do this you need to edit /etc/network/interfaces with something like vi. Here is an example script:

auto lo
iface lo inet loopback
auto eth0
#allow-hotplug eth0
#iface eth inet dhcp
iface eth0 inet static
address 192.0.2.7
netmask 255.255.255.0
gateway 192.0.2.254
dns-nameservers 8.8.8.8 8.8.4.4

Here is a description of each item

  • auto [interface aka iface] this tells the interface to be brought up during boot time
  • allow-hotplug [iface] is in case auto does not work.
  • lo - This is the loopback interface auto
  • iface says what interface is used
  • inet - This tells it that interface is ipv4
  • dns-nameservers [ip] - For setting dns. Note that this is new and changes from the /etc/resolv.conf method
  • eth0 is the ethernet interface. below are ways to list interface
  • dhcp - means auto dhcp
  • static means it is static and you need to have:
  • address, netmask and gateway (with their respective addresses as suggested above)
  • # - means that line is skipped

Normally after changing this you can do this to reset it. There are a few ways to do this.

restart network interface

There are a few different ways to restart the interface and depending on your system you may need to use different ones. this is also very helpful for running scripts.

ifdown and ifup

This command is a simple way to enable and disable and interface

  • $ifdown [options] [iface]
  • $ifup [options] [iface]

A trick if you are remote accessing is running:

  • $ifdown eth0 && ifup eth0

Or another is to use the -a for all

  • $ifdown -a && ifup -a

Sometimes ifdown and ifup do not work so you may need to do other commands to restart the network service.

Debian systemctl

This is for systems that use systemctl, but it does not always work:

 sudo systemctl restart networking.service

invoke-rc.d

This will perform a restart at the high level of processes. it is effective

invoke-rc.d networking restart

Network RHLE

Redhat, centos, etc have a bit different method

  • modify /etc/sysconfig/network-scripts/ipcfg[device]
  • Optional: Modify /etc/hosts file if defined
  • $service network restart
  • Other things you can modify in that:
  • ONBOOT=on/off for changing it to turn on the eth on boot

Bonding with Debian

This is a more advanced setup and usually made for using multiple interfaces on a server for things like redundancy or increased performance. You will be editing the same debian network config file located at:

/etc/network/interfaces

Kernal Modual

You need to have this kernal module installed: bonding If you dont have it you can use modprobe to set for the session or set it in the modules file. See in this wiki the section about modprobe

Modes

There are several modes you can set. You will need to define the bondoing mode in the config file. You can use either the number or the name in the config file

https://www.ibm.com/support/knowledgecenter/en/linuxonibm/com.ibm.linux.z.l0wlcb00/l0wlcb00_bondingmodes.html

Here are some modes that are most commonly used:

  • Active-backup policy: Only one slave in the bond is active. A different slave becomes active if, and only if, the active slave fails. The bond's MAC address is externally visible on only one port (network adapter) to avoid confusing the switch. This mode provides fault tolerance. The primary option affects the behavior of this mode.

Config file

The basics for the config file is to define an bond as an interface. First you define the interfaces, then you define the bond interface.

# The primary network interface
auto eth0
iface eth0 inet manual
	bond-master bond0
	bond-primary eth0
# The secondary network interface
auto eth1
iface eth1 inet manual
	bond-master bond0
	
# The bond with bond configurations and first IP address
auto bond0
iface bond0 inet static
    address 192.168.1.99
    netmask 255.255.255.0
    gateway 192.168.1.1
    bond-mode 1
    bond-miimon 100
    bond-updelay 200
    bond-downdelay 200

Config properties for bonding

  • bond-mode - As mentioned above you can use the name or number. In this example we are saying active-backup
  • bond-miimon [milliseconds] - time for MII checking link (media-independent interface is a type of daemon that checks status of interfaces)
  • bond_primary: choose the primary slave iface_name (used with mode active-backup).
  • bond-downdelay/updelay - Has to do with failover. A note about updelay and downdelay, both of these values must be multiples of the miimon value otherwise the system will round down.

Vlan with Debian

You will be editing the same debian network config file located at:

/etc/network/interfaces

Kernal Modual

You need to have this kernal module installed:

8021q

Config file

This is an example of how to use Vlan 700. It is simple. There are other ways of doing it

auto eth0.700
iface eth0.700 inet static
 address 10.100.10.77
 netmask 255.255.255.0

Links

Bonding and Vlan Combo

Using the above information, here is an example of how to have 2 different IP addresses on a bond interface of a server, and one of the IPs with a VLAN.

## beginning of interface config file
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eno1
iface eno1 inet manual
	bond-master bond0
	bond-primary eno1
# The secondary network interface
auto eno2
iface eno2 inet manual
  bond-master bond0
  
# The bond with bond configurations and first IP address
auto bond0
iface bond0 inet static
    address 1.2.3.4
    netmask 255.255.255.0
    gateway 1.2.3.1
    bond-mode 1
    bond-miimon 100
    bond-updelay 200
    bond-downdelay 200
    bond-slaves none

# Vlan and what bond to belong to:
auto vlan670
iface vlan670 inet static
    address 192.168.0.253
    netmask 255.255.255.0
vlan-raw-device bond0

Links

Checking network configuration

this page indicates some checks that could be done after configuring the interfaces file. https://www.tecmint.com/network-nic-bonding-teaming-in-debian-linux/2/
Mainly it suggests to perform these checks:

  • ifconfig to look that interfaces are configured properly
  • mii-tool (not sure how this will work)

ip

Use ip to show / manipulate routing, devices, policy routing and tunnels. Many of the things you do with ifconfig can now be done with ip. Here is a good link, and some examples that will be expanded uppon later.
https://www.tecmint.com/ip-command-examples/

  • $ip link set eth1 up
  • To set a link up or down

DNS

There are a Few ways to change DNS:

resolve.conf

Need to edit the file:

/etc/resolv.conf

The file should contain:

nameserver [DNS server IP]
domain [Domain Name of local host]
search [Which Domain to search]
  • Please note that DNS is Auto generated via network manager. So if you change it but have nm running, it will override any changes to this file upon the next reboot.

Network Manager Service

Network manager or nm is a very common service running to manage networks and give an easy to use gui for network releated operations in linux. It is very common with many distros and perfect for easily connectng wifi or changing IP.

nmcli

There is a cli for nm that can be usufull.

interfaces config file

Located at /etc/network/interfaces. This file tells ifup,ifdown, etc how to control various interfaces. it is looked at during nm startup so some network configs can be put there. example:

  • line: iface wlan0 inet manual
  • This will say that the interface wlan0 (first wireless card) is controlled manually and not by nm. Very good if you have a failing internal wireless card and want to just use the usb adapter.

nm-online

nm-online is a wait for connection service that runs during start with nm. to disabled during bootup with its target file:

  • $systemctl disable NetworkManager-wait-online.service

If you want to modify the confiruation of how this is run during startup, you may need to edit a startup file for nm located at:

  • /lib/systemd/system/NetworkManager-wait-online.service

Edit to say:

[Service]
Type=oneshot
ExecStart=/usr/bin/nm-online -s -q --timeout=30

For more info: https://askubuntu.com/questions/615006/ubuntu-15-04-network-manager-causing-slow-boot

Network Tools

Ping

Can do ping just like in windows:

  • $ping [address]

Tip: here is how to ping with timestamp

  • $ping 8.8.8.8 | while read pong; do echo “$(date): $pong”; done

traceroute

Very similar to windows tracert.

hostname

to see host infomration

hostname

Options

  • -i to see local loopback address
  • -I to see IP address

nslookup

For DNS info. if not on system, should install package dnsutils

  • $nslookup [address]
  • Will return what your DNS server being used is, and what the IP of the domain you are looking up.

iwconfig

iwconfig is like ifconfig for wireless. you can do a lot of different things, including finding the status and name of your wireless card.

  • $iwconfig

iwlist

This tool helps with seeing the different SSIDs

  • $iwlist [interface] [option]
  • interface is usually something like wlan0
  • Some options are:
  • scan will print scanned ssid
  • frequency will list available frequencies for wireless card

There are many other options, if you do iwlist -h it shows you a nice list.
A very good example to scanning is

  • iwlist wlan0 scan | grep ESSID

rfkill

nmap

Used to see ports and see Ip addresses

  • $nmap [options] [target]
  • Options/scan types:
  • -sn(newer) pings just all ips in that range, most usefull. ex:
  • $nmap -sn 192.168.1.0/24
  • -O Detect operating system but very noisy
  • -sA ACK scan
  • -sT TCP conect scan (ports?), Noisy and long but reliable
  • -sS Stealthy SYN scan, Partial TCP three way handshake, Fast but good?
  • -sP -PP Get around ICMP packets,

tcpdump

Used to show a log of Packet Headers info, not packet itself.

tcpdump

This just gives simple readout of packets. Can be a lot and show you a lot

  • -i [interface] to show just that interface. This can sometimes be very imporant. While there is a default interface, it helps to choose one to listen to.
  • -D to list available interfaces
  • -n does not resolve name servers, useful if in a slow devices
  • -e Shows Mac address
  • src [ip] / dst [ip] shows you lines with that source or desitnation ip respectivly.
  • proto [protocal] for types of packages
  • and [options] for chaining options
  • not [options] this will show you everything except that
  • -w [file] to write to a file in standard tcpdump file format, also able to read in wireshark.
  • -r [file] to read a tcpdump file. NOTE: this file is written in some kind of weird format and can

This example will only show you packets sent from that address.

tcpdump src 192.168.1.1 

You can combine src, dst, and nots to get things

tcpdump -i eth0 src not

To sort by mac on ethernet 0 (fist port usually)

sudo tcpdump -i eth0 ether host aa:bb:cc:11:22:33

This is an excellent page for more options: https://danielmiessler.com/study/tcpdump/#gs.lU0pRcE

dig

Use dig to easily lookup what the DNS entry of an ip is.

dig [host] [options]

Some options

  • +short to just show the IP address entry

Example to look up what IP google has and what Ip it returns:

User@mend:~#dig google.com +short
142.251.46.238

SSH

ssh stands for secure shell. It is a service that allows for you to remote access a terminal using encryption. It is very universally standard and exists on most linux systems or can be easly installed

ssh [-p <port>] [username]@[ip]

For example, if you want to access a server at 1.2.3.4 with the username admin:

ssh admin@1.2.3.4

Then you would be prompted to enter a password. There may be other security gates you will need to authenticate with. Once you have acccess the computer you are in the terminal for that host and can do commands as if you were there.

Change SSH settings

Config file in /etc/ssh/sshd_config

  • Change port: edit line that says: Port [port number]
  • Listen address lets you choose wich addresses it will listen to those ports. ex:
  • ListenAddress 0.0.0.0
  • This will listen to any address

When done with config, do:

service sshd restart

Last logged in

Part of the ssh program is a package called last. You can use it to see the last people logged in, or current logged in users just do:

last

Known hosts

Known hosts is a file in your current user's directory that is for when you ssh into something that is unknown. You can edit this however you want. It is located at

~/.ssh/known_hosts

Offending key

Sometimes when you reboot devices you may get a warning as the key will be different. Here is a link on how to fix it:

http://www.thegeekstuff.com/2010/04/how-to-fix-offending-key-in-sshknown_hosts-file/

You can edit this file with nano or vi, or use sed to modify it simply this way:

sed -i '[#]d' [/dir/known_hosts]

Where # is the located in the error message line that says :

"Offending key in [dir/known_hosts]: #"

The directory is usually either

~/.ssh/known_hosts or /.ssh/known_hosts

Different cipher

the remote host might not allow you access due to how the encryption works. In this example here is the error:

$ssh user@host
Unable to negotiate with host port 22: no matching key exchange method found. Their offer: diffie-hellman-group1-sha1

To fix this edit ~/.ssh/config and add at the end:

KexAlgorithms +diffie-hellman-group1-sha1

Note that the file named config may not exist. Just create one and ssh will look at it for that type of configuration, even if it just has one line.

ssh keys

for better security you can create a public private key relationship. Here is an example of how to do this with ssh. Taken from https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2

Create an RSA key pair

First, create the key pair on your client machine, as in our computer, NOT where you want to ssh into. Also, it does not matter what user you created. Note: Once you create the key on your local machine, that public key can be copied to multiple machines. So you can skip this step and move the public key to server.

ssh-keygen

Options

  • -t says type. rsa is a common good type and is the default

When you execute this command, it will give you some steps you need to follow. It wants to create the public key file at ~/.ssh/id_rsa

  • Enter Passphrase (can be blank)
  • File is created for public Key
  • fingerprint is created
  • keys random images is created

Move Public key to server

There are 2 ways. The main way, and the manual way. Here is the primary best practice:

ssh-copy-id [user]@[server address]

Here is the manual way. This is just an example and certain lines would need to substituted:

cat ~/.ssh/id_rsa.pub | ssh user@123.45.56.78 "mkdir -p ~/.ssh && cat >>  ~/.ssh/authorized_keys"

You can see in the manual way it just takes the public key file and puts it in the .ssh directory.

Test and login

Now you should test the ssh login. You should then be prompted with a password that is the password of the key you created, NOT the host nor client password

Tips

  • Supposedly you can copy your public Key to another local machine and use it to access the same remote server.

Telnet

Telnet works the same as ssh, but dont put the password.

ftp

ftp is a command line utility for accessing via ftp. An FTP client is usually easier but certain things may need it for access.

ftp [host]

Then you Should be prompted with user name and password. Once you are in ftp, you will have a prompt that looks like:

ftp>

Here are some commands you can use there. This turns it into binary transfer, which is the preferred method for transferring files:

ftp>bin

This will put a file from the directory you initiated ftp and put it in the host.

ftp>put [file]

this will get a file from the host, to the directory you are

ftp>get [file]

To exit

ftp>bye

Note, that some systems have probes that will execute a command such a firmware update, if a certain file name or type is put in the host.

sftp

sftp is a ssh protocal that allows you to access file systems from within a file manager.
NOTE:These instructions are NOT for command line, but rather from the address bar of a file manager like PCmanFM or other linux file managers. Simply type in the address bar something like:

  • (address bar)$sftp://192.168.1.99:99
  • Press enter
  • You may be prompted by an SSH warning which you can approve at your own dicretion
  • Then you can browse and work as admin (baring privilages) as if you have control over that folder.

Uncomplicated Firewall

ufw or Uncomplicated firewall is a front end for iptables that makes it much easier to set up rules. A really great page with commands can be found here:

https://help.ubuntu.com/community/UFW

Basically you use the ufw command to do certain things. This command shows the status

ufw status

Some system modifiers are:

  • enable/disable - to turn the firewall on or off
  • status, status verbose - Gives some good basic information
  • show added - this shows the commands of added rules, but not the same readout as status
  • show raw - This will show you the full iptables readout
  • logging on/off - To turn on or off logging. The log is most likely in /var/log/ufw*
  • reset - this will reset your ufw rules to defualt

allow/deny

Doing allow or deny is the main syntax of how you add rules. for example if you want to allow port 22 you would do:

ufw allow 22

you can also tell it protocols

ufw deny 53/tcp

You can also allow or deny ip addresses and subnets with from and the proper ip format. Here is the basics

ufw allow from <target> to <destination> port <port number>

And some examples

allow from 192.168.1.0/24
ufw allow from 192.168.0.4 to any port 22 proto tcp

To Delete a rule simply add it to the same rule as before:

ufw delete deny 80/tcp

ufw best practices

These are not necessarily industry standard but may be some good rules to add.
IMPORTANT: keep in mind the status of ufw. It is possible to enable a rule that will block you out. it is best to disable ufw, create rules, and then enable it. Here we will deny all incoming and allow outgoing

ufw default deny incoming
ufw default allow outgoing

Then we definitly want to allow port 22 either from all, or an ip:

ufw allow from [ip] to any port 22

You can use that rule to add other ports, as most systems will just need that rule.
Some reference articles:

iptables

iptables has a wrapper called ufw that is becoming more popular, especially in the ubuntu/debian community. See above for more about ufw

  • $iptables [options, many]
  • $iptables –list
  • To list current settings for IP tables

Location of the iptables startup script:

  • /etc/sysconfig/iptables
  • But not reccomended for use (can run with vi)
Custom iptable script
  • Create a script (can use the example on from the above link)
  • Add custom port rules, or ip rules, or both
  • (can just copy some of the rules in the list)
  • Now, see other sections on how to change to executable and run script, run at startup, etc
  • Important note about the script: In the example script, there is a line:
  • /sbin/services iptables save
  • This line will actually change the /etc/sysconfig/iptables file based on what is in the script. If you want to NOT save them, and continue to use whatever is set in the default (or changed) config file, you need to remove that command

ethtool

Would probably usually have to download with apt-get. More can be found here: http://www.linuxjournal.com/content/fun-ethtool

  • $ethtool [ethX]
  • Gives information about ethX
  • $ethtool -s [options]
  • speed #### - to set the speed to 100 or 1000, or other respectivly
  • there are many others

socat

Trying to get this to work to talk over sserial from console port on ubnt edgemax to eth0 on linux lappy

wget

resource: http://www.labnol.org/software/wget-command-examples/28750/

  • $wget [-opts] [url]
  • This will pull that file location and dl it into the curretn working dir. otherwise do:
  • ‐‐directory-prefix=[dir] [url]

scp

scp is secure copy and is a way to send files from one computer to the other. typically it is:

  • $scp [user]@[remotehost]:[/remotedir/remotefile] [/localdir/localfle]
  • This is how you copy a remote host file to your local directory
  • To send a file, you do the same, but reversed. It is that easy
  • $scp [/localdir/localfile] [user]@[remotehost]:[/remotedir/remotefile]

This information was from:http://www.hypexr.org/linux_scp_help.php

WinSCP

Winscp is a nice gui that lets you access linux servers from windows. You can do SFTP, SCP and others. Remember that sftp will only work if ftp on device is active. Winscp is easy to use if you understand how ssh works.

pscp

pscp is for windows and can be run from the comand line. But using WinSCP is easiest
You can download it here:http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
Also, here is the best tuturial:http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter5.html
It is a file that needs to be ran from dos. The easiest thing is to run it from the directory it is in:

  • Remember, in windows you have to do CD for the cd command
  • in this example $ is replaced with >
  • To Transfer from Linux to windows
  • C:\workingdir>pscp [options] [user@]host:source target
  • Example:
  • C:\Users\Hstar\Putty>pscp admin@10.1.1.1:/var/log/messages C:\Users\Hstar\Putty
  • You then have to put in your password.
  • To go from windows to linux just reverse it:
  • >pscp [options] source [source…] [user@]host:target
  • example:
  • >pscp c:\documents\foo.txt fred@example.com:/tmp/foo

nmcli

nmcli is good ifyou have network-manager installed but dont want to use the gui, and use cl instead. Here is a good tuturial, even though it is device specific, it should still work the same:
http://docs.getchip.com/chip.html#wifi-connection

Public IP lookup

Its easy enough to google whats my ip to find your public IP, but for very small clients, you may not be able to do this. Here is a workaround for how to check if the public IP:

This will call up a website that can display your ip in a simple way. The operators for wget are just quiet and save to a standard file which is then echoed with the echo command.

SNMP

Simple Network Management Protocol allows you to get information from network devices. You can also use it to control, but this is not the typical way it is used.

  • OID is the address of a specific date point that can be used when querying a network device with SNMP.
  • MIB is the Management information base, and is usual a file formatted a specific way to indicated what OIDs mean what. The device itself does not necessarily have identifiers of what each OID means.
  • ASN1 is Abstract Syntax Notation One. This is the Syntax that MIBs are in.
  • Agent - Is the device queering the client device for data using various types of SNMP gathering software
  • Community - The agent uses the community string to Authenticate that it can gather information from devices. Note that this is a low security model in version 1 and 2c of snmp
  • SNMP version 1 and 2c. Version 2c allows for more expansion of what data can be passed with SNMP, and some other improvements, It is very common
  • SNMP version 3 adds extra security and authorization beyond just knowing the community

To install the suite of packages

  • The package name in ubuntu/debian is snmp
  • The package name in Centos/RHEL is net-snmp

snmpstatus

To get the status of if snmp and if it is running on a device you need to know the IP of the host, the community string, and the version snmpstatus -c [community] -v [version] [host] For example snmpstatus -c public -v 2c 192.168.0.99

snmpwalk and snmpget

snmpwalk is a tool to scan for snmp. To install you you just install the package called snmp

snmpwalk [opts] -c [community] [ip address] [OID]

Options

  • -c says what community to use. Not
  • -v[X] says what version, as in -v 1, -v 2c -v 3 (most systems are going to have v2c, and many use v1)
  • -m ”[mib file]“ This will tell snmp walk to look up the mib file. The defualt MIB search path is several paths defined by the : /home/btowne/.snmp/mibs
  • You can put in the OID address at the end to just get data from that data point

snmpwalk will go through every sub OID possible from the highest point you are calling it from. This is very good when discovering sets of data, but if you want to find the specific information of a specific OID, you want to try snmpget. This is very hepful because if you use walk for what you think is specific OID. It might append a 1 or another number on the end. For example, lets say that oid.99.500.3.5 is supposed to tell you how may foos are in the network device so you run:

snmpwalk -v1 -c public 192.168.1.99 oid.99.500.3.5

You might get a return of:

SNMPv2-SMI::oid.99.500.3.5.1 = INTEGER: 33, 

You can see that in the readout it shows the oid with a .1 on the end. So if you did snmpget instead:

snmpget -v1 -c public 192.168.1.99 oid.99.500.3.5

This might be a return readout:

SNMPv2-SMI::oid.99.500.3.5 = No Such Instance currently exists at this OID

If you do walk, you may see that the o

snmpd

To allow your linux device to act as a simple snmp agent you can install snmpd

apt install snmpd

You need to edit a few things in

nano /etc/snmp/snmpd.conf

First you have to edit the Agent Behavior section so snmpd will listen to all connections, rather then local

#  Listen for connections from the local system only
#agentAddress  udp:127.0.0.1:161   #<<<<This is the line to comment out
#  Listen for connections on all interfaces (both IPv4 *and* IPv6)
agentAddress udp:161,udp6:[::1]:161   #<<<<This is the line to UNCOMMENT

If you want to change the community, edit the Access Control Section. You just need to change the word public to what community you want

 rocommunity public  default    -V systemonly

You may also want to change the location and contact under System Information section

sysLocation    Sitting at the dock on the bay
sysContact     Me <me@example.com>

You should then restart the snmp daemon

systemctl restart snmpd  <<<ubuntu
service snmpd restart <<<<RHEL

Also check to make sure it will start up with the computer

systemctl enable snmpd  <<<ubuntu
service snmpd enable <<<<RHEL

Advanced snmp config file

snmpd supports the View-Based Access Control Model (VACM) as defined in RFC 2575, to control who can retrieve or update information. You The above instructions for editing just the community is very basic, but if you want more access you need to modify the config file to include some things. First we want to map a community the the VCAM module. Here is the format and an example. SECNAME, or security name is arbitrary. In this example we define 2 SECNAMES as local and mynetwork. So only SNMP will go to those networks.

#com2sec [-Cn CONTEXT] SECNAME SOURCE COMMUNITY
com2sec local localhost  foocommunitylocal
com2sec mynetwork 1.2.3.4/24  foocommunity

Then we need to define groups based on the SECNAME and what they are capable of doing. In this example we are defining a group called myRWGroup and saying it can use snmp v1 with the local security name

# group NAME MODEL SECNAME
group   MyRWGroup       v1      local

View defines what OIDs are accessible by what group. This example says that all groups can view all snmp oid available. More detials about the masks can be found in the link below. By saying .1 it will allow anything that starts with .1, which all snmp oid should. By saying 80, we are doing something about limiting the hex code (but not sure what it is??)

#view NAME TYPE SUBTREE [MASK]
view    all     included        .1      80

Last, we define access for the groups. Below is a simple best practice way.

#access NAME CONTEXT MODEL LEVEL PREFX READ WRITE NOTIFY
access  MyRWGroup ""      any       noauth    exact  all none none

With all this combined, here is a simple script that can be used. This is very critical when you have advanced monitoring systems like zenoss trying to access things like hard drive space, CPU, etc.

# Replace things appropriate, mostly things that start with foo
# This is The View Access Control Model and it maps the commuinity to the VACM module
#com2sec SECNAME SOURCE COMMUNITY
com2sec local           localhost       foocommunitylocal
com2sec mynetwork       1.2.3.4/24		foocommunity
# Groups define paramiters groups have access to
#group GROUPNAME MODEL SECNAME
group   MyRWGroup       v1      local
group   MyRWGroup       v2c     local
group   MyRWGroup       usm     local
group   MyROGroup       v1      mynetwork
group   MyROGroup       v2c     mynetwork
group   MyROGroup       usm     mynetwork
# View defines what is accessable by what group
#view NAME/ALL TYPE SUBTREE [MASK]
view    all     included        .1      80
# This is for creating the access:
#access GROUPNAME CONTEXT MODEL LEVEL PREFX READ WRITE NOTIFY
access  MyROGroup ""      any       noauth    exact  all none none
access  MyRWGroup ""      any       noauth    exact  all all none
# SNMP identification paramiters 
syslocation fooSittingonadockatthebay
syscontact fooperson@cool.net

iperf

Iperf is a way of transferring bulk benign files to see pure transfer rates. Here is a good tuturial: http://openmaniak.com/iperf.php#iperf-w

iperf [mode] [host] [options]

Options

  • -c runs in client mode
  • -s runs in server mode, at default port 5001
  • In client mode host needs to be a active iperf server. there are public ones out there.
  • In Server mode you do not need a host, because the local machine will act as the host
  • -t [time] in seconds to test, default is 10
  • -i [interval] in seconds to show test. If running for 10 seconds, then -i 1 will show you results 10 times at each second
  • -f [k,m,K,M] kilobits Megabits Kilobytes megabytes, respectively. Default is m for megabits
  • -b [bandwidth allocation]. Not really sure what the results show for this
  • -u uses udp instead of tcp, best for pinging website
  • -p [port] to set port

Example to run iperf as a client and the target host of iperf.he.net

iperf -c iperf.he.net

netstat

Netstat tool is more for services, but is very related to network. It lists all the open network streams, ports and IPs for specific PID/services.

  • $netstat [options]
  • One of the best options to do is -tulpn
  • -t tcp
  • -u udp
  • -l shows listening
  • -p shows PID
  • -n shows numerical addresses instead of trying to determine symbolic host, port, or user names

SSL Cert

A secure socket layer certificate is used to authenticate a website with various authorities that give out certificates. It allows for a url to operate has https within a browser or any service that accesses that domain. This is not really a network tool, but rather a security tool for domains within a network.

Cert basics and location

A certificate is a file with an encryption key. It uses public key cryptography between the web client, like your browser, and the server, or the website you are trying to access with https. The file is located in a few different locations depending on what system you have. Ultimately though you will be defining where the file is with the httpd.conf file. More about that below

Obtain cert

First you must obtain the cert. Certs can be found with some hosting providers like godadd, or with a free service like https://letsencrypt.org/

conf file

There is an ssl.conf file but that just has to do with paramiters of how you want ssl to work. You will need to locate the httpd.conf file and make sure that your cert files are pointed to the right locations. here is an example /etc/httpd/conf/httpd.conf configuration:

SSLCertificateFile /root/sslcerts/ca.crt
SSLCertificateKeyFile /root/sslcerts/ca.key
SSLCertificateChainFile /root/sslcerts/ca.bundle

Kernal

Some aspects of linux involve modifying the kernal moduals that are loaded.

Modprobe

This will add, remove or modify modules for the kernal.

modprobe [module]

A simple way to see if a specific module is installed is to do:

lsmod | grep [module]

Processes and Services

Init management is how linux manages processes and when they start, such as at startup. There is a lot of complexity and difference in this system, but the 2 most common commands for managing startup are:

  • systemctl
  • chkconfig

systemctl

This is the main command for controlling upstart services and can be found in ubuntu and many debian based distros.

  • $systemctrl [otpions] [unit]
  • list-units shows all known units
  • list-units-files shows the list of targets, might be better then before
  • can pipe grep .service to see just services. ex:
  • $systemctl list-units | grep .service
  • status [service]
  • to list the info about a specific service. ex:
  • $systemctl status httpd
  • start/stop/restart [unit] to do those
  • enable/disable to enable or disable from starting during startup.
  • –failed shows any failures and the error services and messages
  • daemon-reload will completely reload the upstart daemon

chkconfig

This is the main command for controlling systemd services and can be found in most RHEL distros.

  • $chkconfig
  • –list to see a list of services and if they will start or not
  • $chkconfig [service] [option]
  • options are: on to startup, (run levels 2,3,4,5) off to disable startup
  • ex:
  • $chkconfig httpd off
  • This will make it so httpd will not load during start up

Types of init

There are a few different popular systems that manage this. Common types are:

  • systemd - most common systems these days, such as debian and RHEL
  • upstart - a common init used in ubuntu based systems for a while, but most switched to systemd
  • sysv - A mostly depreciated system

Processes have different names for different systems:

  • units - for systemd
  • services - a more universal name
  • daemon - sysv, and a more common name for all Unix
  • processes - a common name, and what windows calls them

More about init below

Service Command

Most systems have the service command and can do many things:

  • $service [a service] [action]
  • [service] can be any service like sshd, dropbox, nagios, etc
  • [action] is ususally: start, stop, restart, status
  • $service –status-all
  • Shows a status of what services are running

systemd

System D is becoming the most common. Many systems may run an older system like sysv or upstart in conjuntion.

systemd command

You can run systemd a command to get some information:

  • $systemd [options]
  • –version. To find out what version you have

journalctl

to see the log

  • $journalctl [opts]
  • -b for entries for this recent boot

Directories for systemd

  • /lib/systemd/system all units config file that can run with sytemd
  • /etc/systemd/system services that do run at startup

sys V

System V is becoming an older service

Editing services run level manually

Below this section is a lot of info about editing init run levels manually, but it is much easier to do it with chkconfig and systemctl outlined above.

  • rcconf
  • sysv-rc-conf

I am not sure which is better or depending on the system you have. but they will make editing run levels much easier. Its worth adding them as packages

rc.local

This is a simple startup script file that automatically executes at each run level. For simple scripts during startup, you can put them here. It is located in:

  • /etc/rc.local

Some notes:

  • Be sure to put a & at the end of commands that continuously run, like a daemon or script
  • Try to NOT put things in this script and find the config file, manipulate an existing system, or create a new system for startup. Here is a good article on what should or should not go here: http://bencane.com/2011/12/30/when-its-ok-and-not-ok-to-use-rc-local/

update-rc.d

This is a very complicated service that you should not mess with unless you know what you are doing.Applicable to debian and sysvinit. Generally you will use:

  • $update-rc.d [service] [options][runlevel] . [other options][other run level]
  • ex:
  • $update-rc.d mediatomb default
  • Service has to be located in /etc/init.d/[service]

runlevels:

  • start means those are the start scripts
  • stop those are the stop scriptes
  • default -Auto creates scripts with default run levels
  • default -Also makes it 2,3,4,5 but you can do it like this:
  • $rcupdate-rc.d mediatomb start 20 2 3 4 5 . stop 80 0 1 6 .
  • the . Seperates which scripts (start vs stop in this case) go to which script level in the etc/rcX.d
  • 20 and 80 are sequence codes which decides in what order of precedence the scripts in the /etc/init.d/ directory should be started or stopped.
  • Start will add S (ex S01plex) Stop will add K (ex: KS01plex)
  • remove Completely removes all rcX.d/[S/K][##runlevel][service] Scripts

Remove a service

  • $update-rc.d -f [service] remove
  • -f stands for force is mandatory
  • This command will only disable the service until next time the service is upgraded. If you want to make sure the service won't be re-enabled upon upgrade, you should also type the following:
  • $update-rc.d apache2 stop 80 0 1 2 3 4 5 6 .

More details about the scripts themselves:

  • Services are located in the /etc/init.d/ and/or the /etc/rc.d directories.
  • There is also services run levels in /etc/rcX.d where X is the different run levels.

To list services in a specific run level, you can do:

  • ls -l /etc/rc[x].d

There are generally 2 kinds of symbolic Links (can only have either S or K, not both):

  • S##(0-99) denotes priority of ENABLE
  • K##(0-99) denotes priority of DISABLE
  • Lower the number, higher priory

Run levels are:

  • Can edit or create lines with ln (see in this doc) like this example
  • $ cd /etc/rc5.d/
  • $ ln -s /etc/init.d/apache2 S20apache2

Processes

Processes are the processes running currently. There are some tools you can use to identify processes and resources

init

init is the parent of all processes.

compgen

This lists available commands

  • $compgen [option]
  • -c all available commands
  • -a all alieses
  • -b all builtins

top

Show live process report

  • q to quit
  • $ top -b -n1 > $dir/[name].log
  • To save process to a log file

pstree

  • $pstree
  • Shows a tree of processs, also for the same:
  • $ps -ejH
  • $ps axjf
  • Prints ps like a tree but with PIDs

ps

Print status of running processes

  • $ps [commands]
  • will just show basic functions
  • axu shows full comprehensive processes
  • -A or -ef Shows all running processes
  • -l Does a very long list
  • -u [user] for showing that users processes
  • A very good example for showing a specific process is to use grep
  • $ps -ef | grep [string]

For more: http://www.cyberciti.biz/faq/show-all-running-processes-in-linux/

PID

Stands for Process ID

  • $pidof [command]
  • or
  • $pgrep [command]
  • ex:
  • $pidof mediatomb
  • readout:####
  • or
  • $ps aux | grep [command]
  • For more comprehensive readout of that command
  • also can do auwx
  • aux means: all user x (if just x, will do all)

inittab

This has to do with runlevels and what run levels are activated in different stages and based on different type sof logins. For more info, go here: http://www.computerworld.com/article/2693438/unix-how-to-the-linux-etc-inittab-file.html

Auto StartX

This is what I did: http://forums.debian.net/viewtopic.php?t=29333 \\This is for a Minimal Debian install on a pink laptop I am working on

  • edit /etc/inittab and change comment out line that says:
  • 1:2345:respawn:/sbin/getty 38400 tty1
  • so it should say
  • #1:2345:respawn:/sbin/getty 38400 tty1
  • Then add line:
  • 1:2345:respawn:/bin/login -f YOUR_USER_NAME tty1 </dev/tty1 >/dev/tty1 2>&1
  • Then edit or add file
  • /home/UserDir/.bash_profile
  • To have this line:
  • if [ -z “$DISPLAY” ] && [ $(tty) == /dev/tty1 ]; then
  • startx
  • fi

End Processes

There are many different ways to end a process that is malfunctioning. Primaryily you will be using kill but here is a link that needs to be read and entered into this process
http://www.makeuseof.com/tag/6-different-ways-to-end-unresponsive-programs-in-linux/

kill
  • $kill [signal] [process/pid]
  • You do not need to enter a signal option for a basic kill. ex:
  • $kill iceweasel
  • or
  • $killall iceweasel
  • Doing just kill or killall is hte best way to kill something

Signal options can either be the command or the number. Example:

  • $kill -9 9999
  • -SIGHUP (-1) - Hangup detected on controlling terminal or death of controlling process. Use SIGHUP to reload configuration files and open/close log files.
  • -SIGKILL (-9) - Kill signal. Use SIGKILL as a last resort to kill process. This will not save data or cleaning kill the process.
  • -SIGTERM (-15) - Termination signal. This is the default and safest way to kill process.

Other:

  • -HUP will restart if process hangs
  • $kill -l
  • To list all the different types of kill

System recovery

Sometimes Linux systems do crash. here are some ways of recovering depending on the sevarity of the issue:

switch and kill

First Switch to a console window. Al + Ctrl + F1-F6. F7 is your GUI. <br> then Find what process is causing problems. <br> Top can help you see processes: http://mendiculus.info/doku.php?id=linux#top

$top

Where x is an app you suspect. This Can help with determining what processes are active: http://mendiculus.info/doku.php?id=linux#ps

$ ps aux | grep x

These 2 commands are a way of finding the process ID, and then killing it. It is very simple but does not always work.

$pidof [name of process]
$kill [pid]

Switch and restart x

  • you can also switch to a console window and run:

$sudo restart gdm

magic SysRq Key

The supposedly simple and smoothest way is:

  • Press and Hold Alt + SysRq
  • Type reisub (while holding alt sysrq)
  • This does a supposed clean and gentle linux restart. Each letter does something different. you can read about it here:

Cron tab

Cron Table is a time based job scheduler daemon. You can edit the various cron files (/ext/cron*) but it is best to run:

  • $crontab [option]
  • -e to edit in default editor (nano, vi)
  • -l to list users file, (like doing cat)

The first time you run this, it will create a temp file and have you choose what editor you want to use to edit the user cron job file (vi, nano, etc.) Then once you save the file, the crontab will add it to a custom folder in /var/spool/cron/crontab/<userfile> You may have to take into consideration users. Also, once you edit the file and save and exit, it will auto update the cron job system so you do not need to reload the service
The system works by adding lines with instructions. Their are some alternatives, but here are the basics:

#This is an example cron file and lines
#as usual, the pound comments out
#minute hour dayofmonth month dayofweek command
02 15 * * * echo 'hello world' > /home/[user]/Documents/crontest.txt

This line will echo those words and then export it to a text file in that location. It will do it every day at 3:02 pm. All the Time parameters have a logic behind it, but basically ones filled in will do it at that interval. The more you fill in, the more specific in infrequent the interval.

Packages

Packages are the files that make up a program programs. A package may also have dependencies, other packages that are needed to run the specific program. For example a drawing program, and a office composing program may require the same graphics package. Most modern linux systems use a package manager that helps with installing and making sure all the dependencies are met. Advanced package manager, apt, and Redhat Package manager, rpm, are 2 of the most common forms of package mangers. APT can be used with a front end gui such as the very common Synaptic.
You can of course run these from command line

  • $apt [install command] [options] [package]
  • $yum [install command] [options] [package]

Here is an example

  • $apt install nano

This will check the repository Database structure and system. The repository, is like a list of all the programs available through apt. apt will also gather and check things like where to download them, what the dependencies are, How to install them, version info, and other information. You will get some prompts about what apt is going to do to install the package (in this example, nano) and then it will install it. Read more about software repositories at: http://en.wikipedia.org/wiki/Software_repository.

Package Files

If you want to see how a package file structure will work, you can download the package (.deb or a zip of some sort) and see how the strucutre is in there. But this will not run some scripts or changes you might need to execute. However, in the root of that open/extracted package should be some kind of script for other changes that might need to be changed.

apt

This is the command for debian based systems to do various package thigns. newer stuff is just apt

  • $apt-get [operator] [options] [package]
  • operators
  • options:
  • install = installation
  • remove = remove/uninstall
  • –purge = removes config files
  • package, you can do multiple packages such as ex:
  • $apt-get install package1 package2

apt-get update/update

You can use apt-get after changing /etc/apt/sources.list or /etc/apt/preferences. This is where the packages and sources are listed. Usually you do update to update the lists from repositories and then upgrade to actually upgrade the software.

  • $apt-get update
  • $apt-get upgrade

To do a search for packages with keywords:

  • $apt-cache search [keyword]

yum and dnf

yum is for RHLE, or centos

yum [options] [commands] [package]

Options

  • -y Answer “yes” to every question in the transaction.
  • –showduplicates list [package] will let you see versions available for a package

Commands

  • check-update will check to see what needs to be updated
  • update will update all packages
  • update [package] will just update said package
  • install [package] will install said package

dnf is a new version and can mostly work the same as yum.

Manage Repositories

You can do this to add custom repositories. Generally you want to add them to a file in any file ending in .list located in /etc/apt/sources.list.d directory. But the main file for listing repository links is located in etc/apt/sources.list file. There is a tool to add repositires.

apt

  • add-apt-repository ppa:whatever/ppa

if you dont have add-apt-repository, you can add it with one and/or of these:

  • apt-get install software-properties-common
  • apt-get install python3-software-properties
  • apt-get install python-software-properties

rpm

rpm is part of the RHEL package manager system. It has to do with updating and upgrading packages. Also part of manageing repositories

  • $rpm [-opts] [address]
  • This will add a repository to the system
  • -U to upgrade or install
  • -v verbos
  • -h for hash (why?)

For seeing what is installed:

  • $rpm -qa
  • -q to queery
  • -a all installed packages

More good info:https://www.tecmint.com/20-practical-examples-of-rpm-commands-in-linux/

http repositories

Sometimes you have to manyallyt add a repository link, but if it is in the http format, you need a special pacakge

  • $apt-get install apt-transport-https

Remove ppa

You can just remove PPAs by deleting the .list files from /etc/apt/sources.list.d directory. or do:

  • add-apt-repository –remove ppa:whatever/ppa

As a safer alternative use ppa-purge (may need to install)

  • $ppa-purge ppa_name

these 2 methods won't uninstall packages that were on the PPA but not on tha official repositories. If you want to remove them, you should tell it to apt:

  • $apt-get purge package_name

Last but not least, you can also disable or remove PPAs from the “Software Sources” section in Ubuntu Settings with a few clicks of your mouse (no terminal needed).

Key ID

In addition to doing this. You may want to check the keys and remove them Located in /etc/apt/trusted.gpg

  • $apt-key list
  • $apt-key del [keyID]
  • Key ID is found in list as: pub [numer]/[keyid] [Date]

Defaults

There are lines that represent defualt packages to do certain things. For example the line x-www-browser will point to a specific browsing package such as firefox or lynx. There are 2 ways of changing defaults.

Config file

Many packages have configuration files located in /etc. These config files an be in other places also. They often have the .conf extension. Often you will need to configure a package by editing the config file. Keep in mind to always look at other files the config file is refering to, as their may be more configuration to be done with a package other then its main .conf file.

Debug mode

If you are having issues with a a program or service, you may want to look into how to debug it. Some programs have an option to invoke or a simple test config command. For example, radiusd lets you invoke this command: radiusd -X This will actually start he service and show you the entire processes of what is is doing like checking config files, showing handshakes, showing keys, etc. It can be very helpful to show you errors when they occur.

Packages General Purpose

Terminator

Terminator is a very common and powerful terminal emulator. Here are some shortcuts:

  • Ctrl + Shift+ O Split terminals horizontally
  • Ctrl + Shift+ E Split terminals vertically
  • Ctrl + Shift+ W Close current Panel
  • Ctrl + Shift+ X enlarge current pannel/go back to split pannel view
  • Alt + ↑,↓,←,→ Move to the terminal in the direction fo the arrow
  • Ctrl + Shift+ T new tab
  • Ctl + Shift + C to copy
  • Ctl + Shift + V to paste

tar

tar is like a zip program

tar [options] [archive-file] [file or directory to be archived]

Options

  • -C tells it to go to that directory, but otherwise it will go into current
  • tar does not need a dash (-) for options and you can chain them together such as:
  • x extract
  • v operates verbosely (gives output)
  • z tells it that is is compressed with gzip
  • f tells it that the next action (usually the archive) will be named the next option, or go into the next location if done with -C
  • c says you will be creating an archive

There are many combinations and you should use them wisley but the most common to extract a tar.gz into the current folder is:

  • xzvf

tar does not natively unzip .zip files so you may need to install:

apt-get install unzip
unzip [file.zip] [-d /dir]

fsck

Check for disk errors

  • fsck /dev/[hd#]

mail or mailx

Mail or mailx are command line tools to send emails. They are Message User agents (MUA) Not to be confused with Mail transfer agent (MTA) such as sendmail or postfix. An MTA must be running in order to send mail (mostly.)

  • $mail [options] [reciever]
  • -s [subject] for the subject
  • receiver should be the receiver.
  • there are different ways to send messages, but the simplest is to use echo. ex:
  • $echo “this is the body” | mail -s “this is the subject” reciever@mail.com

One helpful troubleshooting is the log, located in /var/log/mail(something like .log or .err)

ClamAV

you can install clam av via command line:

  • $apt-get install clamav
  • you can also update the database by running:
  • $sudo freshclam
Clamav errors

You do need to go to /etc/freshclam.conf and putt a # before Example to make it not part of the conf file
A common error for fresh clam is:

  • Can't change dir to /usr/local/share/clamav (or some other dir)


So you need to change the /etc/freshclam.conf file to say what directory the db will go in
Then you can do this to change ownership of that directory:

  • $mkdir -p /usr/local/share/clamav
  • $chown clamav:clamav /usr/local/share/clamav
  • /usr/local/bin/freshclam -v

Wine

Wine is a kind of vitalization that allows you to install software meant for microsoft windows systems. It does not always work perfectly, but can allow you to run a lot of programs:

Other packages

There are a lot of other packages and information that was moved away from this page. here is the link Supplemental linux information

Lynx

For a good time with a simple command line browser, install lynx: http://lynx.isc.org/ This is in the debian repository To Change the startup page:

  • $vi /etc/lynx-cur/lynx.cfg
  • Find and edit:
  • WWW_HOME='homepage'

Packages GUI

xrandr/arandr

  • $xrandr
  • gives readout of the types of displays connected. Probably LVDS or LVDS1
  • $xrandr –output [display] [options]
  • Some options:
  • Display brightness
  • $xrandr –output [display ex:LVDS] –brightness [level, 1=100%, .5=50%]
  • ex:
  • $xrandr –output LVDS1 –brighness 1
  • to clone:
  • $xrandr –output LVDS –auto –output VGA –auto –same-as LVDS
  • To turn off minotor needs to be:
  • $xrandr –output [monitor] –mode [widthxheighth]
  • To turn on and off monitor:
  • $xrandr –output [display] –off
  • $xrandr –output [display] –auto

General Windows

Time

Redo timezone:debian:

  • dpkg-reconfigure tzdata

Audio

  • Why does my volume become 'muted' on every boot?
  • Append these lines to your /etc/rc.d/rc.local or /etc/init.d/alsasound
  • /usr/bin/amixer set Master 50 unmute >/dev/null 2>&1
  • /usr/bin/amixer set PCM 50 unmute >/dev/null 2>&1
  • /dev/null /usr/bin/amixer set CD 50 unmute >/dev/null 2>&1

Desktop managers

Here is some information about common desktop managers:

Cinnamon

A good tool to use after installing cinnamon is dconf-editor (previously dconf-tools)

XFCE

Changing windows (when cant grab corners)

  • move Alt + mouse click,
  • change size Alt+space, then r
  • change size alternative Alt+f8

OpenBox

My Favorite minimal desktop manager is Open box. Here is a good debian article on it: https://wiki.debian.org/Openbox

Autostart script

NOTE: This ONLY applies to open box.http://openbox.org/wiki/Help:Autostart
Note in that document at the bottom that t here are 2 places for the autostart, one for a user, which may not be generated in a stripped down system, and one for any open box instance. Some examples of things to add in a very stripped down version of Openbox. Remember, these are just EXAMPLES and you may want to do more reasearch if you do not know what something means

  • xterm #to start an xterm session window
  • feh #for setting the background..need to work on
config file

rc.xml is the file and it is Located in either: /etc/xdg/openbox/ or: ~/.config/openbox/

Menu file is located in ~/home/[user]/.config/openbox/menu.xml It should be very easy to follow the xml type syntax to add a menu item.

Mate

In mate you can move your curose close to the corners of a window and then hold the alt key and press the right click on the mouse. This will let you easily resize a window. This is helpful when using mate because the tollerence for the edge when you want to move your mouse to resize is so small its hard to find where you can resize.

File Mangers

There are a few different file managers like PCFman, Nemo, Nautelus, etc.

Start Dir

To change the start directory for at least Nemo and Nautelus

  • Find file /usr/share/applications/<fm name various>.desktop
  • open with vi
  • Find line that says:
  • Exec=<fm name> $dir
  • Change dir to desired
  • Example:
  • Exec=nemo /home/jamesdoe/Documents
  • Making the line like this will always start nemo in that folder instead of the user folder
  • note, things like %U means main user directory.

Packages Hardware

Mouse and Keyboard

xinput

xinput helps you find information about your mouse and keyboard inputs

  • $xinput
  • this will list all inputs
  • there is more to this input, but per a website, here is one useful tool:
  • $xinput set-prop ”[Exact name of device with spaces]“ “Device Enabled” [0,1]
  • This was helpful for disabling trackpoint with a thinkpad

Keyboard Shortcuts

In Linux Mint you can do a lot of the same keyboard shortcuts as in Microsoft Windows by replacing the Windows key with Ctl+Alt. Ex:

  • Ctl+Alt+L Lock screen
  • Ctl+Alt+T Bring up terminal

Serial Connection

A way to connect to some unix based systems is via a serial connection. There are many ways to do this but here are some helpful commands.

  • On the host you want to serial (before serial) you can run this to learn about serial:
  • $dmesg | grep 'serial'
  • This will return some lines like:
  • serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
  • This can be interpreted in a few different ways but it is best to refer to your device manual to understand how to use serial for your system
  • ttyS[x] is the different serial console device and may need to be called up with /dev/ttyS[x]

usb to ttl

There are a lot of adapters, but a common one is the adafruit adapter: https://www.adafruit.com/products/954
The pinout is:

  • red power,
  • black ground,
  • white RX into USB port, and
  • green TX out of the USB port.

If you are trying to connect to something like the Pocket Chip, then you want to keep in mind that the RX cable on the USB UART wire gets plugged into the TX port on the client device

Screen

Screen is a simple program to use when attaching a serial connect. once you have it connected here is what you do:

  • To find what dev it is connected to, it helps to do:
  • $dmesg | grep tty
  • This will give you a readout with something like
  • ttyS1 connexted to /dev/ttyUSB0
  • Then use that to run screen
  • $screen [dev] [baud]
  • ex:
  • $screen /dev/ttyUSB0 115200

Call up - cu

This is a simple way to access serial

  • cu

Picocom

  • $picocom [opts] [dev]
  • -b [baudrate]
  • -d [databits]
  • -p [parity] (o for odd, e for even, n for none)
  • -f [flow] (

Example:

  • $picocom -b 9600 -d 8 -p n -f /dev/tty02

To exit:

  • Ctl + a + x (can do as holding ctl, press a, release a, press x)

Remove computer beep

Using modprobe to remove the computer beep: http://www.thinkwiki.org/wiki/How_to_disable_the_pc_speaker_(beep!)

modprobe beep

Packages Services

The following are very popular packages used in many Linux administration systems.

VNC

VNC stands for virtual network computing. It allows you to remote access a computer and its interface. There are several pacakges, but Here we will talk about realvnc.
http://www.realvnc.com
On that web page, if you want the open server, you need to navigate to the bottom and choose legacy software.

vncserver

This is the command to start the server. it is actually a wrapper for the main command xvnc4. But it has some better system setups

  • $vncserver [opts]
  • -desktop [desktop-name] for naming (such as user name specific)
  • -rfbport [port] sets the port, defualt is 5900 + desktop number
  • ex:
  • $vncserver
  • Doing this simple command will start the first desktop

One way to see if vnc is running is this command:

  • $ps aux | grep Xvnc4
  • This will show you a line showing which user is running it, and after the Xvnc4 entry, info about the desktop, like :1 -desktop, etc
  • Be sure to capitalize.

To stop a vnc:

  • $vncserver -kill :[desktop number]
  • This does not seem to work very well, but this does:
  • kill -15 [pidof Xvnc4]
  • Note: remember that if you kill all instances of Xvnc4, and then run the above command to check it, you will see an entry for the ps grep command.

vncpasswd

When starting vncserver, it created a password file at ~/.vnc/passwd.

  • $vnspasswd

multiple vnc users

One way to create multiple users to to edit the auto start config file.

x11vnc

Is a simple server that will allow vnc to existing x11 session. here are basic steps:

  • Best to run in basic user. Here is an example
  • $xllvnc -display :1
  • Then you can access with a vnc viewer with the address and port corresponding (5901 in this example)

Apache

Apache is a very common webserver. It acts as a daemon that directs network traffic to website files, such as html files.

Config File

Located at /etc/httpd/conf/httpd.conf

Allowing directories

Example whitelist for a folder:

<Directory [$dir of local]>
Order allow,deny
Allow from [ip]
</Directory>

htpasswd

This is the command used to change userinformation for various website authentication. For example, if you use nagios, it accesses this system for for authentication to access the nagios webpage. Though a service like nagios has a level of permissions, the main user authentication is done through apache.

  • $htpasswd [options] [file] [user]
  • -n pormpts for a new password but does not actually change it, just gives you a display of the encrypted pass you gave
  • -c creates or appends file. (it is hard to find, but when dealing with multiple users, doing this will probably not create a new file with only that user…but make a backup just in case, a standard practice)

Postfix

Postfix is a mail transfer agent. It is a daemon that runs in the background and manages mail. It is often used on mail servers but can also be ran on smaller or local machine and allows you to send mail as if you are running a server. You can also use to to send mail as a smtp relay, using another smtp server. This is very helpful when writing scripts where you want notifications. The smtp relay is to prevent mail being spammed in some situations. Here are some basic instructions: https://help.ubuntu.com/community/Postfix

Postfix requires additiona packages that may not be dependencies, such as:

  • Debian needs: libsasl2-2, sasl2-bin and libsasl2-modules
  • RHEL needs: cyrus-sasl-sql cyrus-sasl-plain cyrus-sasl-lib

Note that in some RHEL distros, such as centos, the default configuration will work as long as the service is running.

To send an email use the command “mail” or “mailx” See above for this

avahi

avahi is a zero conf daemon that will make it easier to connect to devices
https://en.wikipedia.org/wiki/Avahi_(software) You can do this by editing the host file. Then you can add the device and set its parameters.

mysql

MySQL is a database system, below is info about postgres, another very popular database server. It is a daemon that runs on a system and can be accessed by other programs. It is a kind of relational database and there are many other types, but mysql is probably the most popular. Side note, some commands will say schema, this just means database.

mysq comands

mysqladmin [options]

This is for doing things like setting up passwords

mysqldump [options]

Main command for doing dumps

mysql [options]

This command will bring you into the mysql terminal. Here are its options:

  • -u [user] to indicate what user
  • -p indicates you are going to prompt for a password, this is needed
  • -e [statement] This will let you execute a statment from your linux shell without entering the mysql shell
  • here is an example:
  • $mysql -u root -p -e 'show databases;'

Here is a basic example of how to enter the mysql terminal

localhost$mysql -u root -p
Enter password:[for password]
Copyright information
mysql>

mysql command prompt

A great list of common commands: http://​www.zbeanztech.com/​blog/​important-mysql-commands
Now you can enter different commands for mysql. Most commands need a semicolon after to end the command. Otherwise you hit enter for other lines of a command, such as entering data for the schema. Here are some examples:
This will give you some good list of common commands

mysql>help

To leave the mysql console

>exit;

This will ist all the current databases

>show databases;

This will now bring you into the database to view and edit. Note that the new command prompt does not indicate what db you are in.

>use [db];

This shows you all the tables of that db.

>show tables;

To create a database

>create database [db name];

This will allow for that db to accessed by the localhost, or whatever server you want, and identified by the password

>grant [levels] on [db].* to [db]@localhost identified by "password"

There are many commands to edit the database. here are some basics, but their is a lot of complex syntax that could fill an entire page like this one. But here are some basic stuff. A very good tool is MySQL workbench made by Oracle and for Windows and Linux that lets you edit the system like you would edit a excel spreadsheet.

postgres

Here are some basics about postgres and how to access the postgres command prompt

psql [options]

Some options are

  • -U [user]
  • -d [database]
  • -h [host]
  • -W force password prompt, should happen automatically

Then issue these commands as needed:

  • \? – Shows help
  • \du – Lists users
  • \l – Lists databases
  • \dn – Lists schemas
  • \conninfo – Displays information about current connection
  • \q – Quit/Exit

Own cloud

Own Cloud is open source server software that creates a system similar to dropbox or one drive. You can create accounts, quotas and a lot of other things for a local file storage platform. Here is a very easy guide on how to install owncloud on CentOS. It can be installed on debian systems, but this link and secontion on Mendiculus will be the centos way:

Setup

Once you have installed you should check to make sure that Apache and mysql are on and will startup.

chkconfig httpd on
chkconfig mysqld on

THere is a note I have about the speeds of owncloud and you may need to modify the http config to allow more then 2 megs per second
Also I had to install some php and other moduals manuall. But not sure the instructions for that

php

You may need to update php. Here is how you do it on Centos: https://www.zerostopbits.com/how-to-upgrade-php-5-3-to-php-5-6-on-centos-6-7/

Apache

Owncloud does not have its own service, but rather runs in concurrent with it. So if you want to do service actions for owncloud you can just use apache. The service is called httpd, but you can use apachectl. The simple commands are things like:

apachectl [start|stop|graceful]

The graceful command is the best command for rebooting properly

Mysql

You can see above the information on how to log into mysql. usually you only need to do this once to create the DB per the instructions.
You need to make sure mysql starts automatically

service mysqld start

The mysql db port used is 3306. The defualt user is root
If you need to view or modify the users list you can execute these commands once you are logged into the mysql server. Here is also how to log into mysql:

mysql -u root -p
[enter password]
use owncloud;
select * from oc_users;

If you need to update a user, like copy from one db to another, this is how you can add a line to that table:

update oc_users set password = '1|[Password key goes here without brackets]' where uid = '[user goes here without brackets]';

System info

For some good system info see this file in the owncloud root dir:

/var/www/owncloud/config/config.php

This will help with finding what sql version you have

Directories

Owncloud is usually located in:

/var/www/owncloud
/var/www/html/owncloud

The user data is in:

/var/www/html/owncloud/data/[user]/files

A good way to see how much all your users are using is to use du, show in megabytes, and sort by size:

du -h --max-depth=1 -B M /var/www/html/owncloud/data/ | sort -n

occ maintenance

occ is a command for doing general owncloud maintenance. More info about it can be found here: https://doc.owncloud.org/server/10.2/admin_manual/configuration/server/occ_command.html#file-operations
IN some instances you have to run it as the apache user, and do the entire command:

sudo -u apache php /var/www/html/owncloud/occ [action]

Some very good actions for it are:

  • maintenance:mode [–on/–off] - This turns on and off the maintenance mode. Owncloud will still be an active service, but it will not sync any files or perform any actions. This is really good practice to do when performing maintenance actions, or backing up the owncloud system. Sometimes you may want to stop the web server with:

apachectl -k graceful If you are having errors with files you can try to run this:

  • files:cleanup - This tidies up the server’s file cache by deleting all file entries that have no matching entries in the storage table. Note that you can not need to be in maintenance mode to do this.

Backup and restore

Need to backup in the own cloud folder

  • /config
  • /data
  • The data base (see below)

More details:

Config and data can be backed up manually to hopefully an external source. When you do the backups, please make sure to put it in maintenance mode, and Remember to take it out afterwards

sudo -u apache php /var/www/html/owncloud/occ maintenance:mode --on
sudo -u apache php /var/www/html/owncloud/occ maintenance:mode --off

Posix error

If there is an error about posix you may need to update a php package and modify a config file. Here is the command that creats the error:

sudo -u apache php /var/www/html/owncloud/occ maintenance:mode --on
[sudo] password for support: 
The posix extensions are required - see http://php.net/manual/en/book.posix.php

The info on how to fix is: https://framasphere.org/p/400627
What you want to do is put in main mode, then install these packages:

yum install php-process

After install you will need to configure it by uncomment the call to the extension in the file /etc/php/php.ini:

extension=posix.so

Then restart apache

apachectl graceful

The database is a little bit more complicated.

Database backup

Here are mysql instructions for backing up manually the database:

  • $mysqldump –single-transaction -h [server] -u [username] -p[password] [db_name] > owncloud-dbbackup_`date +”%Y%m%d“`.bak

Move Directory

IF you need to change the directory of the data, here are some basics of the steps. This is the main link that follows this process:

https://doc.owncloud.org/server/10.0/admin_manual/maintenance/manually-moving-data-folders.html

It is best to put the owncloud in maintenance mode and then backup your data, config file, and database. Or better yet, create a snapshot of your vm. Then you need to stop apache which can be done with:

apachectl stop

You need to copy your data to the new directory and make sure the permissions and ownership carry over. There are some ways to do this, but here is a simple command. If you have space issues, you may need to use the move command instead.

cp -rp /olddir/data/ /newdir/data/

Once you have moved the data over, you now need to change a config file and edit some database strings. Please do all this before restarting the service. The config file line you need to edit is this:

'datadirectory' => '/newdir/data/',

You also need to enter mysql to edit the database. There are 2 main things you need to edit. This is an example of the queries you can run:

UPDATE oc_storages SET id='local::/newdir/data/' WHERE id='local::/olddir/data/';
UPDATE oc_accounts SET home = REPLACE(home, '/olddir/data/', '/newdir/data/');

That should be it. Now you can start apache back up. You should run some tests like moving a file into a local computers owncloud folder and making sure it is updated in the right place server.

Delete and lock errors

Sometimes when deleting files there is an error and you can not delete it. It is likely because it is locked or there is a cached problem. The best thing to do is run occ with the “files:cleanup” command. See above. If you want to look at the locked or problematic files you can do this:

mysql -u root -p
[enter password]
use owncloud;

We want to view the files locks tables. You can view the table headins with

show columns from oc_file_locks;

We want to see the files that have more then 0 in the lock column

select * from oc_file_locks where `lock` < 0 ;

Conditionals and Variables

You can use conditionals and variables in the bash command line to execute complex commands. Much of this can be helpful when writing complex bash scripts.

multiple commands

There are 3 main ways to put together commands:

  • with a semicolon
  • direct function in bash
  • with a script

Semicolin

This might be the easiest way to start of, as it works like a pipe. The semicolon is meant to string conditionals together. It is best to show it as an example with if then conditionals:

while true ; do echo hello world ; sleep 2 ; done

Direct function in bash

That is a single command but when in batch you can see how to type it without the semicolons. In this following I will show indicators for command prompts and how it works. Bash detects a conditional like 'while' and will then allow you to write more commands, and press enter after each line of the function. Then you will be able to write on a new line indicated by the > symbol. As long as the syntax is correct, it will end with the correct command, such as done. The following does the exact same thing as the example command with semicolons:

comandprompt$while true
>do echo hello world
>sleep 2
>done

As a script

We will discuss a lot more about scripts below, but here is what the script would look like to do the exact same command above:

#!/bin/bash/
while true
do echo hello world
sleep 2
done

Note that all three of these versions would continue until you stop with ctrl+c.

Conditionals

If then while etc

if, then, and while are common programing syntax. you can read more about it here: https://ryanstutorials.net/bash-scripting-tutorial/bash-loops.php
This is an example of using while to show a readout of a command, instead of using watch. This below example will echo hello every 2 seconds until you stop the command with ctrl c.

Variables

To Read variables us the $ sign before the variable name. Note that the script will check for all variables before it runs through the commands. Here are examples on how to set commands.

[VARNAME]=[value]

It is case sensitive and best practices to have VARNAME be capital. Example:

BIG=123456

then do:

echo $BIG

will return 123456 Here are some other options:

  • Setting a variable with single quotes '' takes it literally.
  • Setting a variable with double quotes ”“ allows substitution
  • Command substitution uses $ and ()

Example:

NOW=$(date +"%Y-%m-%d")

This will store NOW as the date format indicated so if you run:

echo $NOW

It will return the date

Control operators

https://unix.stackexchange.com/questions/159513/what-are-the-shells-control-and-redirection-operators/159514#159514

  • && do a second comand
  • || will only do the second command if the first one fails
  • &> will send the information in stdout and stderr to the file

read value

Read promps for input

read [options][varname]

This will make a prompt.

  • -p gives a promp in quotes, such as 'Username: '
  • -s makes it silent like for passwords
  • -sp combines so you can do something like 'Password: '

example script:

#!/bin/bash
echo what is your name
read -p 'Name: ' varname
echo hello $varname

Then run:

commandprompt$./scriptname
what is your name?
Name: [prompt to enter name, press enter]
hello [name]

Make sense right?

Scripts

There are a lot of ways to write a script. It really depends on what you want to accomplish and how you want the script to run or be interpreted. Below is how to make bash scripts. Scipts often rely on conditionals and variable as outlined in the above section. Sometimes you have to ask if you really need a script, or can just make a long command with semi colins. Almost everything you can do in a script will run commands as if you were typing them in the bash command line. The advantage is you make a file that can be executed and edited easily. Other ways to write scripts are with python, or C. But these will need an interpreter that can run those scripts.

Bash Scripts

Basic beginer script:

  • Make a document with a text editor, vi, nemo, etc.
  • in document it should read:
#!/bin/bash
# [any comments, # is ignored]
[bash commands]

example hello world:

#!/bin/bash
#Script by Jack heart
echo Hello World

Then to make your script executable do:

chmod +x [script name]

Run the script

sh [Script name, location/scriptname]

Also can do ./[scrip]
ex: ./myscript not to be confused with .file, which is a hidden file or folder

Example Start up Script

bin files

You can run bin files by doing:

  • $./[filename].bin

Compile

Make

usually use Make (this article is a work in progress)

  • extract package
  • look at readme and
  • INSTALL

basic steps to compile and make

  • -run config with:
  • $./configure
  • Many config files give other options. Can usually find by doing
  • #[configfile] –help
  • then can add those to the end of the configure command
  • $make
  • This does the compiling
  • Then
  • $make install
  • This installs the package

Example For hstar ipt-netflow-2.1 example

  • may want to do: SHOULD NOT BE ROOT
  • $./configure –enable-natevents –enable-promisc –enable-macaddress –enable-vlan

Bootloader - Grub

GRUB is a boot loader that can direct a computer which OS or tool to load.

Grub cfg file and editing

You can edit some of the grub settings by editing the file /etc/default/grub.

boot selection

edit the default boot selection you can change the string that says:

  • GRUB_DEFAULT=X
  • where X is the line item order. If you want to change the order, there is more edditing.
  • Remember that the first entry is 0, so if you want to change to the 5th entry, you change X to 4

Splace screen

You can change the splash screen in linux by changing the line:

  • GRUB_CMDLINE_LINUX_DEFAULT=x
  • profile will not load regular pretty screen

invoke grub

If you have grub silent or a 0 for timeout, you can invoke grub by pressing shift

Update grub

When done editing, you need to run:

  • $update-grub

Reload grub

Sometimes you may want to reload grub due to a change in hard drive order. you could edit the cfg file but an easy way to update it is:

Grub command line

These instructions were from booting puppy on a very old system that was difficult to install and properly load the OS. This section is not complete and might not make too much sense
Grub Commands:

  • $root (hd0,<tab>
  • Lists drives, find linux partition
  • $root (hd0,1)

Load kernel (not linux)

  • $kernel /boot/vml<tab>
  • Lists vml kernals found in that directory
  • $kernel /boot/vmlinuz root=/dev/partition
  • NOTE: after vml, you can put parramiters like root=/dev/partition and other paramiters specific to the kernel

Grub for puppy

  • Psubdir
  • Specifies ({device}{type}/path) the location of boot files in the boot partition. If not specified init will look in / and /boot, order-unknown. As of puppy 3.00 this parameter also restricts the boot search for pup_save files: only those in a matching directory will used. This can be used to allow multiple-puppy installation on the same partition to be used without risk of using the wrong one in error. Also see Psubok.
  • Example- psubdir=boot/puppy216

Psubok

  • Using psubok=TRUE implies all boot files are in Psubdir AND init may search deeper than one directory level.
  • Without this setting init's search for the base sfs will be one directory level deep at most.
  • In a multiboot scenario, not using this option may lead to an “sfs not found” error. This can be confusing because a user may have their base-sfs file in the same (sub)directory as vmlinuz (kernel) and initrd.


So the boot command would be:

  • kernel /boot/vmlinuz root=/dev/sda2 psubdir=/
  • (may not need Psubok)
  • initrd /boot/init<tab>
  • lists initrd files found in directory for booting
  • initrd /boot/init

Linux Distributions

Basically, you need to download the ISO. Might eventually try to use arch and follow how to links to build up OS.

Live USB from windows

From Windows: use tool “pen drive linux”

live usb from linux

From Linux

mint

For installing Linux mint with DD see this article: http://community.linuxmint.com/tutorial/view/744
Basically the command is:

  • $sudo dd if=/Dir/[linuxmint].iso of=/dev/sdx oflag=direct bs=1048576

banana pi

Mostly the same as mint, but your card should be fat 32

Filling in Assumptions Gap

Here are some basic descriptions and links to the above list of assumptions, outlined here:

1 Basic Internet searching

You know how to search things for understanding, rather then just copying what is written here.

2 Linux Installation

You have installed linux or have a live version of linux.

3 Command Line acceptance

You are not afraid of command line, or learning command line.

4 Understanding Linux Mendiculus

You understand that these are basic simple notes, not extensive tutorials.

linux.txt · Last modified: 2022-03-28 T 19:07 by admin