Friday, July 27, 2012

vi Editor Commands

General Startup
 To use vi: vi filename
 To exit vi and save changes: ZZ   or  :wq
 To exit vi without saving changes: :q!
 To enter vi command mode: [esc]


Counts
        A number preceding any vi command tells vi to repeat that command that many times.


Cursor Movement


h       move left (backspace)
j       move down
k       move up
l       move right (spacebar)
[return]   move to the beginning of the next line
$       last column on the current line
0       move cursor to the first column on the current line
^       move cursor to first nonblank column on the current line
w       move to the beginning of the next word or punctuation mark
W       move past the next space
b       move to the beginning of the previous word or punctuation mark
B       move to the beginning of the previous word, ignores punctuation
e       end of next word or punctuation mark
E       end of next word, ignoring punctuation
H       move cursor to the top of the screen
M       move cursor to the middle of the screen
L       move cursor to the bottom of the screen


Screen Movement
G        move to the last line in the file
xG       move to line x
z+       move current line to top of screen
z        move current line to the middle of screen
z-       move current line to the bottom of screen
^F       move forward one screen
^B       move backward one line
^D       move forward one half screen
^U       move backward one half screen
^R       redraw screen  ( does not work with VT100 type terminals )
 ^L       redraw screen  ( does not work with Televideo terminals )


Inserting
r         replace character under cursor with next character typed
R        keep replacing character until [esc] is hit
i          insert before cursor
a         append after cursor
A        append at end of line
O        open line above cursor and enter append mode


Deleting
x         delete character under cursor
dd       delete line under cursor
dw      delete word under cursor
db       delete word before cursor


Copying Code
        yy      (yank)'copies' line which may then be put by the p(put) command. Precede with a count for multiple lines.


Put Command
        brings back previous deletion or yank of lines, words, or characters
P       bring back before cursor
p       bring back after cursor


Find Commands


?      finds a word going backwards
/      finds a word going forwards
f      finds a character on the line under the cursor going forward
F      finds a character on the line under the cursor going backwards
t      find a character on the current line going forward and stop one character before it
T      find a character on the current line going backward and stop one character before it


 ; repeat last f, F, t, T


Miscellaneous Commands


 .      repeat last command
 u      undoes last command issued
 U      undoes all commands on one line
 xp    deletes first character and inserts after second (swap)
 J      join current line with the next line
 ^G    display current line number
 %      if at one parenthesis, will jump to its mate
 mx    mark current line with character x
 'x      find line marked with character x


NOTE: Marks are internal and not written to the file.


Line Editor Mode
 Any commands form the line editor ex can be issued upon entering line mode.


 To enter: type ':'
 To exit: press[return] or [esc]


ex Commands
 For a complete list consult the UNIX Programmer's Manual


READING FILES
 copies (reads) filename after cursor in file currently editing


 :r filename


WRITE FILE
 :w  saves the current file without quitting


MOVING
 :# move to line #
 :$ move to last line of file


SHELL ESCAPE
executes 'cmd' as a shell command.
:!'cmd'

Tuesday, December 28, 2010

Enabling and Disabling Security Enhanced Linux in Fedora


Use the /usr/sbin/getenforce or /usr/sbin/sestatus commands to check the status of SELinux. The getenforce command returns Enforcing, Permissive, or Disabled. The getenforce command returns Enforcing when SELinux is enabled 

$ /usr/sbin/getenforce
Enforcing

The getenforce command returns Permissive when SELinux is enabled, but SELinux policy rules are not enforced, and only DAC rules are used. The getenforce command returns Disabled if SELinux is disabled.
The sestatus command returns the SELinux status and the SELinux policy being used: 
 
$ /usr/sbin/sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   enforcing
Mode from config file:          enforcing
Policy version:                 23
Policy from config file:        targeted

SELinux status: enabled is returned when SELinux is enabled. 
Current mode: enforcing is returned when SELinux is running in enforcing mode.  
Policy from config file: targeted is returned when the SELinux targeted policy is used.

Enabling SELinux

On systems with SELinux disabled, the SELINUX=disabled option is configured in /etc/selinux/config

Also, the getenforce command returns Disabled
 
To enable SELinux:
  1. Use the rpm -qa | grep selinux, rpm -q policycoreutils, and rpm -qa | grep setroubleshoot commands to confirm that the SELinux packages are installed. This guide assumes the following packages are installed: selinux-policy-targeted, selinux-policy, libselinux, libselinux-python, libselinux-utils, policycoreutils, setroubleshoot, setroubleshoot-server, setroubleshoot-plugins. If these packages are not installed, as the Linux root user, install them via the yum install package-name command. The following packages are optional: policycoreutils-gui, setroubleshoot, selinux-policy-devel, and mcstrans.
  2. Before SELinux is enabled, each file on the file system must be labeled with an SELinux context. Before this happens, confined domains may be denied access, preventing your system from booting correctly. To prevent this, configure SELINUX=permissive in /etc/selinux/config:
    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #       enforcing - SELinux security policy is enforced.
    #       permissive - SELinux prints warnings instead of enforcing.
    #       disabled - No SELinux policy is loaded.
    SELINUX=permissive
    # SELINUXTYPE= can take one of these two values:
    #       targeted - Targeted processes are protected,
    #       mls - Multi Level Security protection.
    SELINUXTYPE=targeted
    
  3. As the Linux root user, run the reboot command to restart the system. During the next boot, file systems are labeled. The label process labels all files with an SELinux context:
    *** Warning -- SELinux targeted policy relabel is required.
    *** Relabeling could take a very long time, depending on file
    *** system size and speed of hard drives.
    ****
    
    Each * character on the bottom line represents 1000 files that have been labeled. In the above example, four * characters represent 4000 files have been labeled. The time it takes to label all files depends upon the number of files on the system, and the speed of the hard disk drives. On modern systems, this process can take as little as 10 minutes.
  4. In permissive mode, SELinux policy is not enforced, but denials are still logged for actions that would have been denied if running in enforcing mode. Before changing to enforcing mode, as the Linux root user, run the grep "SELinux is preventing" /var/log/messages command as the Linux root user to confirm that SELinux did not deny actions during the last boot. If SELinux did not deny actions during the last boot, this command does not return any output. Refer to Chapter 7, Troubleshooting for troubleshooting information if SELinux denied access during boot.
  5. If there were no denial messages in /var/log/messages, configure SELINUX=enforcing in /etc/selinux/config:
    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #       enforcing - SELinux security policy is enforced.
    #       permissive - SELinux prints warnings instead of enforcing.
    #       disabled - No SELinux policy is loaded.
    SELINUX=enforcing
    # SELINUXTYPE= can take one of these two values:
    #       targeted - Targeted processes are protected,
    #       mls - Multi Level Security protection.
    SELINUXTYPE=targeted
    
  6. Reboot your system. After reboot, confirm that the getenforce command returns Enforcing:
    $ /usr/sbin/getenforce
    Enforcing
    
  7. As the Linux root user, run the /usr/sbin/semanage login -l command to view the mapping between SELinux and Linux users. The output should be as follows:
    Login Name       SELinux User           MLS/MCS Range
    
    __default__      unconfined_u           s0-s0:c0.c1023
    root             unconfined_u           s0-s0:c0.c1023
    system_u         system_u               s0-s0:c0.c1023
    
If this is not the case, run the following commands as the Linux root user to fix the user mappings. It is safe to ignore the SELinux-user username is already defined warnings if they occur, where username can be unconfined_u, guest_u, or xguest_u:
  1. /usr/sbin/semanage user -a -S targeted -P user -R 
    "unconfined_r system_r" -r s0-s0:c0.c1023 unconfined_u
    
  2. /usr/sbin/semanage login -m -S targeted -s "unconfined_u" 
    -r s0-s0:c0.c1023 __default__
    
  3. /usr/sbin/semanage login -m -S targeted -s "unconfined_u" 
    -r s0-s0:c0.c1023 root
    
  4. /usr/sbin/semanage user -a -S targeted -P user -R 
    guest_r guest_u
    
  5. /usr/sbin/semanage user -a -S targeted  -P user -R 
    xguest_r xguest_u
    
 Source: Fedora Project