Table of Contents
Ubuntu Usage
man pages
Help to a command can be retrieved by doing:
man command
To search within the man page you can use regular expression. Type the Expresion by adding a prefix '/'
/[Oo]ption /My case sensitive search String
File structure
The important locations are the following:
/etc/init.d/* | contains startup scripts for services. Typical parameters are “start, stop, restart” |
/etc/default/* | parameters for the startup scripts, in /etc/init.d/* |
/var/lib/*/info/ | all infos for one program |
Essencial Commands
The essencial linux commands are listed under: http://docstore.mik.ua/orelly/linux/run/ch04_01.htm
ls # list contents of a directory. ls -a show hidden files. sudo # "super-user do" (grants administrator rights) sudo -i -u <username> # login as another user e.g. root cd # change directory chmod # change file access permissions chown # change file and group ownership cp # copy mv # move rm # remove cat # concatenate files (dump to screen) emacs # text editor fdisk # partition table manipulator free -h # show free storage df -h # disk free (remaining / used disk space, disk size in percent, human readable) ncdu # disk space visualizer sudo du -h ./* | sort -h # folders and files size in current dir. Sorted. users # users currently logged in useradd # add a user usermod # modify existing user userdel # delete user uname # show system data (try uname -a) cat /etc/*-release # shows the release name and other infos about the installed distribution cat /proc/version # shows distribution version, name etc. cat /proc/version # shows mode distribution data top # show current running processes kill # kill process by PID touch # create new, empty, file passwd # change user password ping -c [COUNT] # ping a network device or location (ping -c 1 google.com) more # show output one screen at a time locate # indexed search ("sudo updatedb" updates indexed database) find # search in the current directory and subdirectories ln -s myFile.txt myLink.txt # symbolic link unzip /path/to/file.zip -d /targetdir # unzip nohup # executes a command from shell, so that it keeps on running after shell exits. & # When placed at the end of a command, execute that command in the background. "vim &" fg # bring theprocess back to the foreground. jobs; fg %1 # enable the NTP server for time syncronization sudo timedatectl set-ntp on timedatectl # clear /var/log/journal/ files, leave only data of last 10d sudo journalctl --vacuum-time=10d # show 10 largest folders, sorted by size, in human friendly size scala du -sh * | sort -hr | head -n10 4.2G var 2.0G usr 914M snap 339M opt 133M data 119M boot 6.1M etc 1.4M home 1.1M run 364K root # example of finding files in 2 subfolders # and writing a cat command with them into the file replace.sh # man pages https://man7.org/linux/man-pages/man1/find.1.html find . \ -name "DocxToPdf.bat" -path "*/Certificates/*" -o \ -name "DocxToPdf.ps1" -path "*/Zeugnisse/*" \ -exec echo "cat /mnt/Temp/DocxToPdf.bat > \"{}\"" > replace.sh \; # you can invert the path with "-not" find . \ -name "DocxToPdf.bat" -not -path "*/Certificates/*" -o \ -name "DocxToPdf.ps1" -not -path "*/Zeugnisse/*" \ -exec echo "cat /mnt/Temp/DocxToPdf.bat > \"{}\"" > replace.sh \;
grep
grep can search a text for a pattern
echo "hello world" | grep w
pipe
| can pass something from programm to programm. In this example the list with files is searched for everything with “x”
ls | grep x
get from file <
< causes commands to take their input from a file. But most commands allow you to specify input files on their command lines anyway, so this “input redirection” is rarely necessary.
{programm} < {filename}
send to file >
> can pass something from a programm to a file.
{programm output} > {filename} ls > file.txt
append to file >>
» can append something from a programm to a file.
{programm output} >> {filename} ls >> file.txt
send output to file incl. errors &>
>& sends the output INCL. ERRORS
{programm output} >& {filename_with_errors}
send just output errors 2>
2> sends the output errors to a file
{programm output} 2> {just_errors}
Restart Services
The services are saved as files in /etc/init.d/
The syntax to restart a service is sudo /etc/init.d/[SERVICENAME] restart
Restart Apache server:
sudo /etc/init.d/apache2 restart
Restart Webmin:
sudo /etc/init.d/webmin stop sudo /etc/init.d/webmin start
sudo a2ensite www.example.com
Activate a Site, which was previously created as a File
/etc/apache2/sites-available/www.example.com
containing e.g.
# # Example.com (/etc/apache2/sites-available/www.example.com) # <VirtualHost *> ServerAdmin webmaster@example.com ServerName www.example.com ServerAlias example.com # Indexes + Directory Root. DirectoryIndex index.html DocumentRoot /home/www/www.example.com/htdocs/ # CGI Directory ScriptAlias /cgi-bin/ /home/www/www.example.com/cgi-bin/ <Location /cgi-bin> Options +ExecCGI </Location> # Logfiles ErrorLog /home/www/www.example.com/logs/error.log CustomLog /home/www/www.example.com/logs/access.log combined </VirtualHost>
Performance Monitoring commands
how to diagnose a mysterious process that’s taking too much CPU, memory, IO, etc?
The diagram below illustrates helpful tools in a Linux system.
vmstat # reports information about processes, memory, paging, block IO, traps, and CPU activity. iostat # reports CPU and input/output statistics of the system. netstat # displays statistical data related to IP, TCP, UDP, and ICMP protocols. lsof # lists open files of the current system. pidstat # monitors the utilization of system resources by all or specified processes, including CPU, memory, device IO, task switching, threads, etc.
Credit: Diagram by Brendan Gregg
Logs
Logs are located in /var/log/… . Use grep to get logs for a special theme:
grep udev /var/log/syslog grep transmission /var/log/syslog grep sda1 /var/log/syslog ...
The syslogs are rotated
/var/log/syslog $ sudo cat /etc/logrotate.d/rsyslog { rotate 7 daily missingok notifempty delaycompress compress postrotate /usr/lib/rsyslog/rsyslog-rotate endscript }
as in https://www.tecmint.com/install-logrotate-to-manage-log-rotation-in-linux/
- daily means that the tool will attempt to rotate the logs on a daily basis. Other possible values are daily and monthly.
- rotate 7 indicates that only 3 rotated logs should be kept. Thus, the oldest file will be removed on the fourth subsequent run.
- size=10M sets the minimum size for the rotation to take place to 10M. In other words, each log will not be rotated until it reaches 10MB.
- compress and delaycompress are used to tell that all rotated logs, with the exception of the most recent one, should be compressed.
One can open the logs even when zipped via
zless /var/log/syslog.1.gz
Useful tools
Nano text editor
Default text editor.
Cmd | Descr |
---|---|
ALT+A | select text |
ALT+6 | copy text |
ALT+K oder F9 | cut text |
ALT+U | paste text |
ALT+Shift+8 | goto the TOP of file |
ALT+Shift+7 | goto the END of file |
STRG+6 | Set copy mark |
^G (F1) Display this help text ^X (F2) Close the current file buffer / Exit from nano ^O (F3) Write the current file to disk ^J (F4) Justify the current paragraph ^R (F5) Insert another file into the current one ^W (F6) Search for a string or a regular expression ^Y (F7) Go to previous screen ^V (F8) Go to next screen ^K (F9) Cut the current line and store it in the cutbuffer ^U (F10) Uncut from the cutbuffer into the current line ^C (F11) Display the position of the cursor ^T (F12) Invoke the spell checker, if available M-\ (M-|) Go to the first line of the file M-/ (M-?) Go to the last line of the file ^_ (F13) (M-G) Go to line and column number ^\ (F14) (M-R) Replace a string or a regular expression ^^ (F15) (M-A) Mark text at the cursor position M-W (F16) Repeat last search M-^ (M-6) Copy the current line and store it in the cutbuffer M-} Indent the current line M-{ Unindent the current line ^F Go forward one character ^B Go back one character ^Space Go forward one word M-Space Go back one word ^P Go to previous line ^N Go to next line ^A Go to beginning of current line ^E Go to end of current line M-( (M-9) Go to beginning of paragraph; then of previous paragraph M-) (M-0) Go just beyond end of paragraph; then of next paragraph M-] Go to the matching bracket M-- (M-_) Scroll up one line without scrolling the cursor M-+ (M-=) Scroll down one line without scrolling the cursor M-< (M-,) Switch to the previous file buffer M-> (M-.) Switch to the next file buffer M-V Insert the next keystroke verbatim ^I Insert a tab at the cursor position ^M Insert a newline at the cursor position ^D Delete the character under the cursor ^H Delete the character to the left of the cursor M-T Cut from the cursor position to the end of the file M-J Justify the entire file M-D Count the number of words, lines, and characters ^L Refresh (redraw) the current screen ^Z Suspend the editor (if suspend is enabled) (M-X) Help mode enable/disable (M-C) Constant cursor position display enable/disable (M-O) Use of one more line for editing enable/disable (M-S) Smooth scrolling enable/disable (M-P) Whitespace display enable/disable (M-Y) Color syntax highlighting enable/disable (M-H) Smart home key enable/disable (M-I) Auto indent enable/disable (M-K) Cut to end enable/disable (M-L) Long line wrapping enable/disable (M-Q) Conversion of typed tabs to spaces enable/disable
Emacs - Best Console Editor
Main Commands:
C-x means: hold CTRL and push x, release both
M-x means: hold ALT and push x, release both
Befehl | Wirkung |
---|---|
C-x C-s | save |
C-x C-c | save and exit |
C-SHIFT-_ | undo |
C-s | search |
M-w | copy |
C-w | cut |
C-y | paste |
C-SPACE | mark modus |
Elinks - console Browser
Befehl | Wirkung |
---|---|
g | Open address bar |
up/down arrows | To scroll up and down - Use them if you don't have a scroll wheel |
left arrow | Previous page |
u | Next page |
t | Open new tab |
c | Close current tab |
CTRL+R | Reload current page |
q | Exit ELinks |
Permissions
There are three types of “users” in Linux:
1) owners 2) groups 3) others
Owners: The username of the person who owns the file or folder. By default, the user who creates the file or folder will become its owner.
Groups: The usergroup that owns the file or folder. All users who belong in the same group as the user who created the file or folder will have the same access permissions to that file or folder. This is useful if, for example, you have a folder that requires a bunch of different users to be able to access files within it, while others can't. In this case, you'd add all the users who need to access the files to the same user group, making sure the required files and folders are owned by that group, and then set the file's group permissions accordingly.
Others: A user who isn't the owner of the file or folder and doesn't belong in the same group the file or folder does. In other words, if you set a permission for the “other” category, it will affect everyone else by default.
List File Permissions
ls -l -rw-r----- 1 syslog adm 542223 2009-11-12 21:15 messages -rw-r----- 1 syslog adm 483710 2009-11-08 06:45 messages.0 drwxr-s--- 2 mysql adm 4096 2009-11-07 17:22 mysql -rw-r----- 1 mysql adm 0 2009-11-07 17:22 mysql.err -rw-r----- 1 mysql adm 0 2009-11-07 17:22 mysql.log drwxrwsr-x 2 mythtv mythtv 4096 2009-11-07 18:28 mythtv drwxr-sr-x 2 news news 4096 2009-11-07 17:05 news drwxr-xr-x 2 ntp ntp 4096 2009-05-13 22:10 ntpstats -rw-r--r-- 1 root root 0 2009-11-07 17:07 pycentral.log drwxr-x--- 3 root adm 4096 2009-11-08 01:00 samba
1 2 3 4 - rw- r-- ---
- The first part signifies whether it's a directory or not a directory. In this example mysql.err is not a directory. Contrast this with the last line in the file list above, samba. This one IS a directory since it is denoted with a “d”.
- The second section (rw-) shows the permissions for the file “owner”. You can see the owner has read permssion, write permission but not execute permission to this file.
- The third section (r–) denotes the “group” permissions. Here the group has read but not write or execute permission.
- The fourth section is the permissions for “other” users. Here “other” users have no access at all to the file.
Changing Permissions
sudo chmod u=rw testfolder
Where who is either the user, group, others or all three of them and permissions is read, write or execute
chmod who=permissions filename/folder
Changing Ownership
sudo chown theowner:usergroup file_foldername sudo chown username file1 sudo chown -R username file1
Groups and members
list groups of user "root" groups root list groups: cat /etc/group list users: cat /etc/passwd group members of a group members groupname add a new user: adduser username add a new group: groupadd usergroup add an existing user to a some groups: usermod -a -G group1,group2,group3 username
Making files executable
Before a shellscript may be started it must be made executable first
sudo chmod a+x mysript.sh
CPU utilization
How to handle the system processes.
Listing process
Getting all processes
ps ax ps auxwww
Can be filtered by a suspicious name, e.g. “java”
ps auxwww | grep java
Can be filtered by a suspicious name, e.g. “java”
ps auxwww | grep java
Listing the arguments of the processes, for example to find the right “java” process
ps -fe
Getting top 10 process which are eating the CPUs
ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10
Killing process
# kill option PID A number of signal options can be sent as words or numbers, but most are of interest only to programmers. One of the most common ones you will use is this: This tells the process with PID to stop; you supply the actual PID.
$ sudo kill PID
THis is the signal for kill (9 is the number of the SIGKILL signal); use this combination when the plain kill shown previously does not work:
$ sudo kill -9 PID
This is the signal to “hang up”—stop—and then clean up all associated processes, too. (Its number is -1.)
$ sudo kill -SIGHUP PID
Crontab usage
Makes it possible to run scripts regulary. Hourly, weekly, at system start etc. Every user has its own crontab table which is editable as:
crontab -e //or "sudo crontab -e" to get roots crontab
Debugging Crontab
tail -f /var/log/syslog
Run commands at startup / autostart
1. possibility is rc.local
To execute something at startup use the file /etc/rc.local.
Everything, what is written there will be executed at startup.
Use “sudo -u username” to execute commands with appropriate rights.
sudo -u ubuntu /usr/bin/python "/home/ubuntu/bin/dropbox.py" start >> /home/dropbox.log
2. possibility is crontab -e
Use the following syntax in the crontab to execute something at every startup:
@reboot /usr/bin/python ~/bin/myscript.py
3. logging
When scheduling a script one should log the output into the syslog.
A unique tag helps to discover the logs quickly.
clearOldDokuWikiLogs.sh 2>&1 | /usr/bin/logger -t CRONOUTPUTscheduleBackupToS3"
Output
Sep 28 03:00:01 ip-172-31-5-28 CRONOUTPUTscheduleBackupToS3: wiki.localhost/
Textmode
Textmode - is when the X-Server (the GUI is not started).
STRG + ALT + F1 STRG + ALT + F2 ... starts a new console session. Useful when the first one is blocked.
Samba widows fileshare server
After installing Samba use the following configuration file to share a file */mnt/storage* for everybody.
This configuration requres a user user.
The configuration file is stored under /etc/samba/smb.conf
# # Sample configuration file for the Samba suite for Debian GNU/Linux. # # # This is the main Samba configuration file. You should read the # smb.conf(5) manual page in order to understand the options listed # here. Samba has a huge number of configurable options most of which # are not shown in this example # # Some options that are often worth tuning have been included as # commented-out examples in this file. # - When such options are commented with ";", the proposed setting # differs from the default Samba behaviour # - When commented with "#", the proposed setting is the default # behaviour of Samba but the option is considered important # enough to be mentioned here # # NOTE: Whenever you modify this file you should run the command # "testparm" to check that you have not made any basic syntactic # errors. # A well-established practice is to name the original file # "smb.conf.master" and create the "real" config file with # testparm -s smb.conf.master >smb.conf # This minimizes the size of the really used smb.conf file # which, according to the Samba Team, impacts performance # However, use this with caution if your smb.conf file contains nested # "include" statements. See Debian bug #483187 for a case # where using a master file is not a good idea. # #======================= Global Settings ======================= [global] ## Browsing/Identification ### # Change this to the workgroup/NT-domain name your Samba server will part of workgroup = WORKGROUP netbios name = S1 guest account = nobody null passwords = true # server string is the equivalent of the NT Description field server string = %h server (Samba, Ubuntu) # Windows Internet Name Serving Support Section: # WINS Support - Tells the NMBD component of Samba to enable its WINS Server # wins support = no # WINS Server - Tells the NMBD components of Samba to be a WINS Client # Note: Samba can be either a WINS Server, or a WINS Client, but NOT both ; wins server = w.x.y.z # This will prevent nmbd to search for NetBIOS names through DNS. dns proxy = no # What naming service and in what order should we use to resolve host names # to IP addresses ; name resolve order = lmhosts host wins bcast #### Networking #### # The specific set of interfaces / networks to bind to # This can be either the interface name or an IP address/netmask; # interface names are normally preferred ; interfaces = 127.0.0.0/8 eth0 # Only bind to the named interfaces and/or networks; you must use the # 'interfaces' option above to use this. # It is recommended that you enable this feature if your Samba machine is # not protected by a firewall or is a firewall itself. However, this # option cannot handle dynamic or non-broadcast interfaces correctly. ; bind interfaces only = yes #### Debugging/Accounting #### # This tells Samba to use a separate log file for each machine # that connects log file = /var/log/samba/log.%m # Cap the size of the individual log files (in KiB). max log size = 1000 # If you want Samba to only log through syslog then set the following # parameter to 'yes'. # syslog only = no # We want Samba to log a minimum amount of information to syslog. Everything # should go to /var/log/samba/log.{smbd,nmbd} instead. If you want to log # through syslog you should set the following parameter to something higher. syslog = 0 # Do something sensible when Samba crashes: mail the admin a backtrace panic action = /usr/share/samba/panic-action %d ####### Authentication ####### # "security = user" is always a good idea. This will require a Unix account # in this server for every user accessing the server. See # /usr/share/doc/samba-doc/htmldocs/Samba3-HOWTO/ServerType.html # in the samba-doc package for details. security = share # You may wish to use password encryption. See the section on # 'encrypt passwords' in the smb.conf(5) manpage before enabling. encrypt passwords = true # If you are using encrypted passwords, Samba will need to know what # password database type you are using. passdb backend = tdbsam obey pam restrictions = yes # This boolean parameter controls whether Samba attempts to sync the Unix # password with the SMB password when the encrypted SMB password in the # passdb is changed. unix password sync = yes # For Unix password sync to work on a Debian GNU/Linux system, the following # parameters must be set (thanks to Ian Kahan <<kahan@informatik.tu-muenchen.de> for # sending the correct chat script for the passwd program in Debian Sarge). passwd program = /usr/bin/passwd %u passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* . # This boolean controls whether PAM will be used for password changes # when requested by an SMB client instead of the program listed in # 'passwd program'. The default is 'no'. pam password change = yes # This option controls how unsuccessful authentication attempts are mapped # to anonymous connections map to guest = bad user ########## Domains ########### # Is this machine able to authenticate users. Both PDC and BDC # must have this setting enabled. If you are the BDC you must # change the 'domain master' setting to no # ; domain logons = yes # # The following setting only takes effect if 'domain logons' is set # It specifies the location of the user's profile directory # from the client point of view) # The following required a [profiles] share to be setup on the # samba server (see below) ; logon path = \\%N\profiles\%U # Another common choice is storing the profile in the user's home directory # (this is Samba's default) # logon path = \\%N\%U\profile # The following setting only takes effect if 'domain logons' is set # It specifies the location of a user's home directory (from the client # point of view) ; logon drive = H: # logon home = \\%N\%U # The following setting only takes effect if 'domain logons' is set # It specifies the script to run during logon. The script must be stored # in the [netlogon] share # NOTE: Must be store in 'DOS' file format convention ; logon script = logon.cmd # This allows Unix users to be created on the domain controller via the SAMR # RPC pipe. The example command creates a user account with a disabled Unix # password; please adapt to your needs ; add user script = /usr/sbin/adduser --quiet --disabled-password --gecos "" %u # This allows machine accounts to be created on the domain controller via the # SAMR RPC pipe. # The following assumes a "machines" group exists on the system ; add machine script = /usr/sbin/useradd -g machines -c "%u machine account" -d /var/lib/samba -s /bin/false %u # This allows Unix groups to be created on the domain controller via the SAMR # RPC pipe. ; add group script = /usr/sbin/addgroup --force-badname %g ########## Printing ########## # If you want to automatically load your printer list rather # than setting them up individually then you'll need this # load printers = yes # lpr(ng) printing. You may wish to override the location of the # printcap file ; printing = bsd ; printcap name = /etc/printcap # CUPS printing. See also the cupsaddsmb(8) manpage in the # cupsys-client package. ; printing = cups ; printcap name = cups ############ Misc ############ # Using the following line enables you to customise your configuration # on a per machine basis. The %m gets replaced with the netbios name # of the machine that is connecting ; include = /home/samba/etc/smb.conf.%m # Most people will find that this option gives better performance. # See smb.conf(5) and /usr/share/doc/samba-doc/htmldocs/Samba3-HOWTO/speed.html # for details # You may want to add the following on a Linux system: # SO_RCVBUF=8192 SO_SNDBUF=8192 # socket options = TCP_NODELAY # The following parameter is useful only if you have the linpopup package # installed. The samba maintainer and the linpopup maintainer are # working to ease installation and configuration of linpopup and samba. ; message command = /bin/sh -c '/usr/bin/linpopup "%f" "%m" %s; rm %s' & # Domain Master specifies Samba to be the Domain Master Browser. If this # machine will be configured as a BDC (a secondary logon server), you # must set this to 'no'; otherwise, the default behavior is recommended. # domain master = auto # Some defaults for winbind (make sure you're not using the ranges # for something else.) ; idmap uid = 10000-20000 ; idmap gid = 10000-20000 ; template shell = /bin/bash # The following was the default behaviour in sarge, # but samba upstream reverted the default because it might induce # performance issues in large organizations. # See Debian bug #368251 for some of the consequences of *not* # having this setting and smb.conf(5) for details. ; winbind enum groups = yes ; winbind enum users = yes # Setup usershare options to enable non-root users to share folders # with the net usershare command. # Maximum number of usershare. 0 (default) means that usershare is disabled. ; usershare max shares = 100 # Allow users who've been granted usershare privileges to create # public shares, not just authenticated ones usershare allow guests = yes #======================= Share Definitions ======================= # Un-comment the following (and tweak the other settings below to suit) # to enable the default home directory shares. This will share each # user's home director as \\server\username ;[homes] ; comment = Home Directories ; browseable = no # By default, the home directories are exported read-only. Change the # next parameter to 'no' if you want to be able to write to them. ; read only = yes # File creation mask is set to 0700 for security reasons. If you want to # create files with group=rw permissions, set next parameter to 0775. ; create mask = 0700 # Directory creation mask is set to 0700 for security reasons. If you want to # create dirs. with group=rw permissions, set next parameter to 0775. ; directory mask = 0700 # By default, \\server\username shares can be connected to by anyone # with access to the samba server. Un-comment the following parameter # to make sure that only "username" can connect to \\server\username # The following parameter makes sure that only "username" can connect # # This might need tweaking when using external authentication schemes ; valid users = %S # Un-comment the following and create the netlogon directory for Domain Logons # (you need to configure Samba to act as a domain controller too.) ;[netlogon] ; comment = Network Logon Service ; path = /home/samba/netlogon ; guest ok = yes ; read only = yes # Un-comment the following and create the profiles directory to store # users profiles (see the "logon path" option above) # (you need to configure Samba to act as a domain controller too.) # The path below should be writable by all users so that their # profile directory may be created the first time they log on ;[profiles] ; comment = Users profiles ; path = /home/samba/profiles ; guest ok = no ; browseable = no ; create mask = 0600 ; directory mask = 0700 [printers] comment = All Printers browseable = no path = /var/spool/samba printable = yes guest ok = no read only = yes create mask = 0700 # Windows clients look for this share name as a source of downloadable # printer drivers [print$] comment = Printer Drivers path = /var/lib/samba/printers browseable = yes read only = yes guest ok = no # Uncomment to allow remote administration of Windows print drivers. # You may need to replace 'lpadmin' with the name of the group your # admin users are members of. # Please note that you also need to set appropriate Unix permissions # to the drivers directory for these users to have write rights in it ; write list = root, @lpadmin # A sample share for sharing your CD-ROM with others. ;[cdrom] ; comment = Samba server's CD-ROM ; read only = yes ; locking = no ; path = /cdrom ; guest ok = yes # The next two parameters show how to auto-mount a CD-ROM when the # cdrom share is accesed. For this to work /etc/fstab must contain # an entry like this: # # /dev/scd0 /cdrom iso9660 defaults,noauto,ro,user 0 0 # # The CD-ROM gets unmounted automatically after the connection to the # # If you don't want to use auto-mounting/unmounting make sure the CD # is mounted on /cdrom # ; preexec = /bin/mount /cdrom ; postexec = /bin/umount /cdrom [STORAGE] comment = Storage path = /mnt/storage read only = no guest ok = yes available = yes public = yes writable = yes guest account = nobody force user = user workgroup = WORKGROUP
Environment Variables
The environment variables are set in multiple locations:
/etc/profile -> /etc/profile.d/* (in alphabetical order) -> ~/.profile -> ~/.bashrc
/etc/profile | global profile |
/etc/profile.d/* | global shell scripts, to modify the environment |
~/.profile | personal variables |
~/.bashrc | personal. more than setting variables |
Mounting USB Devices
Manually
First find out which devices are connected but not mounted
sudo fdisk -l $~ dev/sdb1 .....
Then create the folder, which will contain the Data of the mounted disk
sudo mkdir /media/250G
At least mount the device to the folder, which was created
# sudo mount /dev/sdb1 /media/250G -t auto
ACHTUNG: What is mounted under “media” appears in the filemanager list “nautilus” automagically!
Enabling or disabling services
The tool update-rc.d is used to enable / disable services. It adds/removes scripts to the folder /etc/init.d/.
The scripts are added on different “runlevels” http://en.wikipedia.org/wiki/Runlevel
To find out on which leveles the script is autostarted do: pi@raspberrypi /etc/transmission-daemon $ ls -l /etc/rc?.d/*transmission-daemon lrwxrwxrwx 1 root root 29 Nov 14 19:00 /etc/rc0.d/K02transmission-daemon -> ../init.d/transmission-daemon lrwxrwxrwx 1 root root 29 Nov 14 19:00 /etc/rc1.d/K02transmission-daemon -> ../init.d/transmission-daemon lrwxrwxrwx 1 root root 29 Jan 19 00:07 /etc/rc2.d/S03transmission-daemon -> ../init.d/transmission-daemon lrwxrwxrwx 1 root root 29 Jan 19 00:07 /etc/rc3.d/S03transmission-daemon -> ../init.d/transmission-daemon lrwxrwxrwx 1 root root 29 Jan 19 00:07 /etc/rc4.d/S03transmission-daemon -> ../init.d/transmission-daemon lrwxrwxrwx 1 root root 29 Jan 19 00:07 /etc/rc5.d/S03transmission-daemon -> ../init.d/transmission-daemon lrwxrwxrwx 1 root root 29 Nov 14 19:00 /etc/rc6.d/K02transmission-daemon -> ../init.d/transmission-daemon // autostarted on levels 0,1,2,3,4,5,6
//disable the script sudo update-rc.d -f transmission-daemon remove // reenable the script on default runlevel sudo update-rc.d transmission-daemon defaults
Disable X
sudo nano /etc/default/grub
Find out this line:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
Change it to:
GRUB_CMDLINE_LINUX_DEFAULT="text"
Update Grub:
sudo update-grub
You will still be able to use X by typing startx after you logged in.
startx
Eval
Eval receives a String. Constructs a command and evaluates it. The quotes are interpreted by eval. Details: https://unix.stackexchange.com/questions/23111/what-is-the-eval-command-in-bash
Example 1: store the name of the variable “foo” in “x”. Receive content of a variable “foo” by reading its name from “x”
#!/bin/bash foo=10 x=foo y='$'$x echo $y # $foo eval y='$'$x echo $y # 10
Example2: Iterating variables in array. Get its content.
#!/bin/bash module1=sound module2=grafics module3=cpu modules=( "module1" "module2" "module3" ) for i in "${modules[@]}" do # eval takes arguments, resolves the quotes to areguments, varcontent=`eval echo '$'$i` echo "Inner $i $varcontent" done;
Example3: Storing quoted arguments in a variable. Use eval to execute the command, passing quoted arguments as one.
#!/bin/bash ARGUMENTS="-executors 1 -description \"The Host\" -master http://localhost:80/jenkins/ -username jenkins -password password -name Host -labels host -mode exclusive " # error: parameter Host" is not allowed java -jar swarm-client.jar $ARGUMENTS # works fine with eval eval java -jar swarm-client.jar $ARGUMENTS
Service declaration in Ubuntu
How to define a Service with Ubuntu. Systemd is used beginning with Ubuntu 16+.
Here is an easy way to go:
# create start script echo -e '#!/bin/bash' >> /etc/init.d/rundeck_start.sh echo -e 'nohup java -Dserver.http.port=4440 -Ddefault.user.name=admin -Ddefault.user.password=admin "-Dserver.http.host=10.0.0.10" -jar /opt/rundeck/rundeck-launcher.jar &' >> /etc/init.d/rundeck_start.sh # make it executable chmod +x /etc/init.d/rundeck_start.sh # Make sure this script get executed every time Linux starts update-rc.d rundeck_start.sh defaults 100
Parse JSON in Shell with JQ
Assume you have teh following output, from which you need the KeyName
{ "Stacks": [ { "StackId": "arn:aws:cloudformation:eu-central-1:851117451134:stack/MyStack/181111f0-f63e-11e7-aea9-503f2ad2e5fe", "Parameters": [ { "ParameterValue": "subnet-0b893560", "ParameterKey": "ServerSubnet" }, { "ParameterValue": "environment_management_demo", "ParameterKey": "KeyName" }, { "ParameterValue": "vpc-1e52bb75", "ParameterKey": "MyVpcId" }, { "ParameterValue": "eu-central-1a", "ParameterKey": "MyAvailibilityZone" }, { "ParameterValue": "m3.large", "ParameterKey": "InstanceTypeParameter" } ], "Tags": [], "Outputs": [ { "Description": "WebServer's PublicIp Address", "OutputKey": "PublicIp", "OutputValue": "18.195.125.183" } ], "CreationTime": "2018-01-10T19:40:21.993Z", "StackName": "MyStack", "NotificationARNs": [], "StackStatus": "CREATE_COMPLETE", "DisableRollback": false } ] }
You can use jq to select the right argument:
cat jsonfile | jq '.Stacks[] | .Parameters[] | select(.ParameterKey == "KeyName" ) | .ParameterValue'
Set Vim background to dark
create the following file, which is a vim configuration, to have the vim settings suitable for the dark BG
~/.vimrc
set t_Co=256 set background=dark
Connect to other hosts via SSH
Fr the connection you will have to store the private key on the linux machine and tell the machine where to find it:
It is important that the rights are set correctly, that only the current user can read the file, otherwise connection wont work
cp key.pem ~/.ssh/key.pem chmod 600 ~/.ssh/key.pem
Tell where to find the key, create the config file.
echo "IdentityFile ~/.ssh/RheinEnergy.pem" > ~/.ssh/config chmod 600 ~/.ssh/config
Explicitely mention the username and the private key.
ssh 34.242.178.166 -l ubuntu -i /home/vagrant/.ssh/DevOpsBootCamp.openssh.ppk
SSH Connect via a HTTP proxy
For that you need a tool, like a Corkscrew.
Then,
if the proxy ip is 194.138.0.33
if the proxy port is 9400
and you wish to connect via ssh to 52.17.66.234
username “ubuntu”
apt-get -y install install corkscrew ssh -o ProxyCommand='/usr/bin/corkscrew 194.138.0.33 9400 %h %p' ubuntu@52.17.66.234 -i /mnt/c/1REPOS/infrastructure-platform/infrastructure-multiaccount/ssh/ec2key.priv.openssh.ppk
SSH Agent
SSH agent allow to store the ssh keys into a session. http://blog.joncairns.com/2013/12/understanding-ssh-agent-and-ssh-add/
# start the agent eval $(ssh-agent -s) # add the ssh key into the session ssh-add "/home/MyName/.ssh/jump-host.private.ssh.ppk" ssh-add "C:\Users\MyName\.ssh\internal.private.ssh.ppk" # list the keys in session ssh-add -l # via -A use AGENT FORWARDING to pass keys added into the session and being able to use them on the jump-host # ssh -A -i /home/MyName/.ssh/jump-host.private.ssh.ppk jumphostuser@jumphostdns.com -v # you will be on the jumphost # [jumphostuser@jumphostdns.com ~]$ # now you can do the next jump, without specifying the internal.private.ssh.ppk ssh ec2-user@10.1.250.12 # you will be on the jumphost # [ec2-user@10.1.250.12 ~]$
Ubuntu install
From USB
Installing Ubuntu from USB Stick is described here: http://www.ubuntu.com/download/help/create-a-usb-stick-on-windows. <fc #FF0000>Do not forget to configure the Network during the installation! Or you will have to do it manually.</fc>
Remote Desktop
Install Remote Desktop Server to control Ubuntu by using Windows remote desktop, as described here: http://www.liberiangeek.net/2012/05/connect-to-ubuntu-12-04-precise-pangolin-via-windows-remote-desktop/
sudo apt-get install xrdp
After installation, if the Tab-Autocomplete does not work in the Terminal:
Use STRG+I instead.
To fix it Edit ~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-keyboard-shortcuts.xml find the line <property name=”<Super>Tab” type=”string” value=”switch_window_key”/> and change it to <property name=”<Super>Tab” type=”empty”/> Reboot!
Jira
5. download and unpack Jira and MySql Connector
cd /tmp/ wget http://www.atlassian.com/software/jira/downloads/binary/atlassian-jira-6.2.3-x64.bin wget http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.30.tar.gz tar -xvzf mysql-connector-java-5.1.30.tar.gz chmod +x *.bin
6. install mysql
sudo apt-get install mysql-server mysql-client // you will be promted for a root password. Type "123Yourpassword" or what ever you like sudo /etc/init.d/mysql start
7. Install JIRA
mysql -u root -p CREATE DATABASE jiradb CHARACTER SET utf8 COLLATE utf8_bin; GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER,INDEX on jiradb.* TO 'jirauser'@'localhost' IDENTIFIED BY '123Yourpassword'; FLUSH PRIVILEGES; QUIT //be sure to change 123yourpassword to the password of your choice cd /tmp/ sudo ./atlassian-jira-6.2.3-x64.bin //* Accept all defaults (standard settings, port 8080, etc.) cd /tmp/mysql-connector-java-5.1.30 sudo cp *.jar /opt/atlassian/jira/lib/ sudo /etc/init.d/jira stop sudo /etc/init.d/jira start
8. Redirect port 8080 to port 80
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
9. COnfigure your new JIRA installation
Open a web browser and go to http://<the_ip_of_your_vps>; to configure JIRA. Database type: MySQL Database host: localhost Database user: jirauser Database user password: <your password> Database: jiradb
Open ssh server
# sudo apt-get install openssh-server openssh-client
Debugging an app which does not work
You can use strace to trace the calls for the libs on the OS level in Linux. Do the following to debug “sqlplus” command
strace sqlplus
The output will be:
execve("/rdbms/app/12.1.0.2/bin/sqlplus", ["sqlplus", "/ as sysdba"], [/* 30 vars */]) = 0 brk(0) = 0xab0000 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fe27ec7b000 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) open("/rdbms/app/12.1.0.2/lib/tls/x86_64/libsqlplus.so", O_RDONLY) = -1 ENOENT (No such file or directory) stat("/rdbms/app/12.1.0.2/lib/tls/x86_64", 0x7fff731bb930) = -1 ENOENT (No such file or directory) open("/rdbms/app/12.1.0.2/lib/tls/libsqlplus.so", O_RDONLY) = -1 ENOENT (No such file or directory) stat("/rdbms/app/12.1.0.2/lib/tls", 0x7fff731bb930) = -1 ENOENT (No such file or directory) open("/rdbms/app/12.1.0.2/lib/x86_64/libsqlplus.so", O_RDONLY) = -1 ENOENT (No such file or directory) stat("/rdbms/app/12.1.0.2/lib/x86_64", 0x7fff731bb930) = -1 ENOENT (No such file or directory) open("/rdbms/app/12.1.0.2/lib/libsqlplus.so", O_RDONLY) = 3 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\374\1\0\0\0\0\0"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0644, st_size=1546540, ...}) = 0 mmap(NULL, 3112424, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fe27e983000 mprotect(0x7fe27ea6d000, 2093056, PROT_NONE) = 0 mmap(0x7fe27ec6c000, 61440, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xe9000) = 0x7fe27ec6c000 close(3) = 0 open("/rdbms/app/12.1.0.2/lib/libclntsh.so.12.1", O_RDONLY) = 3 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\233N\0\0\0\0\0"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0644, st_size=58793741, ...}) = 0 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fe27e982000 mmap(NULL, 50056080, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fe27b9c5000 mprotect(0x7fe27e580000, 2097152, PROT_NONE) = 0 mmap(0x7fe27e780000, 1974272, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2bbb000) = 0x7fe27e780000 mmap(0x7fe27e962000, 129936, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fe27e962000 mprotect(0x7fff731bc000, 4096, PROT_READ|PROT_WRITE|PROT_EXEC|PROT_GROWSDOWN) = 0 close(3) = 0 open("/rdbms/app/12.1.0.2/lib/libclntshcore.so.12.1", O_RDONLY) = -1 ENOENT (No such file or directory) open("/etc/ld.so.cache", O_RDONLY) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=40181, ...}) = 0 mmap(NULL, 40181, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fe27b9bb000 close(3) = 0 open("/lib64/tls/x86_64/libclntshcore.so.12.1", O_RDONLY) = -1 ENOENT (No such file or directory) stat("/lib64/tls/x86_64", 0x7fff731bb8d0) = -1 ENOENT (No such file or directory) open("/lib64/tls/libclntshcore.so.12.1", O_RDONLY) = -1 ENOENT (No such file or directory) stat("/lib64/tls", {st_mode=S_IFDIR|0555, st_size=4096, ...}) = 0 open("/lib64/x86_64/libclntshcore.so.12.1", O_RDONLY) = -1 ENOENT (No such file or directory) stat("/lib64/x86_64", 0x7fff731bb8d0) = -1 ENOENT (No such file or directory) open("/lib64/libclntshcore.so.12.1", O_RDONLY) = -1 ENOENT (No such file or directory) stat("/lib64", {st_mode=S_IFDIR|0555, st_size=12288, ...}) = 0 open("/usr/lib64/tls/x86_64/libclntshcore.so.12.1", O_RDONLY) = -1 ENOENT (No such file or directory) stat("/usr/lib64/tls/x86_64", 0x7fff731bb8d0) = -1 ENOENT (No such file or directory) open("/usr/lib64/tls/libclntshcore.so.12.1", O_RDONLY) = -1 ENOENT (No such file or directory) stat("/usr/lib64/tls", {st_mode=S_IFDIR|0555, st_size=4096, ...}) = 0 open("/usr/lib64/x86_64/libclntshcore.so.12.1", O_RDONLY) = -1 ENOENT (No such file or directory) stat("/usr/lib64/x86_64", 0x7fff731bb8d0) = -1 ENOENT (No such file or directory) open("/usr/lib64/libclntshcore.so.12.1", O_RDONLY) = -1 ENOENT (No such file or directory) stat("/usr/lib64", {st_mode=S_IFDIR|0555, st_size=36864, ...}) = 0 writev(2, [{"sqlplus", 7}, {": ", 2}, {"error while loading shared libra"..., 36}, {": ", 2}, {"libclntshcore.so.12.1", 21}, {": ", 2}, {"cannot open shared object file", 30}, {": ", 2}, {"No such file or directory", 25}, {"\n", 1}], 10sqlplus: error while loading shared libraries: libclntshcore.so.12.1: cannot open shared object file: No such file or directory ) = 128 exit_group(127) = ? +++ exited with 127 +++
SSH reset fingerprint
Resetting the ssh fingerprint via console
ssh-keygen -R [hostname] ssh-keygen -R [ip_address] ssh-keygen -R [hostname],[ip_address] ssh-keyscan -H [hostname],[ip_address] >> ~/.ssh/known_hosts ssh-keyscan -H [ip_address] >> ~/.ssh/known_hosts ssh-keyscan -H [hostname] >> ~/.ssh/known_hosts
Dig
List the name servers by domain using googles DNS server
dig -t NS google.com @1.1.1.1 ANY
Upgrading packages
Python3
# install new version of python sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update sudo apt install python3.12 # setting the python12 as default version of python sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1 # gives python3.12.2 python3 --version