How do I lock the desktop screen via command line?











up vote
78
down vote

favorite
13












How can I lock my desktop screen from the command line?










share|improve this question




















  • 1




    For newer versions of Ubuntu, you can try: askubuntu.com/questions/457204/…
    – Wilf
    Jan 2 '15 at 18:31

















up vote
78
down vote

favorite
13












How can I lock my desktop screen from the command line?










share|improve this question




















  • 1




    For newer versions of Ubuntu, you can try: askubuntu.com/questions/457204/…
    – Wilf
    Jan 2 '15 at 18:31















up vote
78
down vote

favorite
13









up vote
78
down vote

favorite
13






13





How can I lock my desktop screen from the command line?










share|improve this question















How can I lock my desktop screen from the command line?







command-line lock-screen






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 23 '11 at 3:46









Isaiah

42.6k20118138




42.6k20118138










asked Oct 16 '10 at 19:04









Renjith G

1,49341822




1,49341822








  • 1




    For newer versions of Ubuntu, you can try: askubuntu.com/questions/457204/…
    – Wilf
    Jan 2 '15 at 18:31
















  • 1




    For newer versions of Ubuntu, you can try: askubuntu.com/questions/457204/…
    – Wilf
    Jan 2 '15 at 18:31










1




1




For newer versions of Ubuntu, you can try: askubuntu.com/questions/457204/…
– Wilf
Jan 2 '15 at 18:31






For newer versions of Ubuntu, you can try: askubuntu.com/questions/457204/…
– Wilf
Jan 2 '15 at 18:31












7 Answers
7






active

oldest

votes

















up vote
78
down vote



accepted










You can lock the computer by running gnome-screensaver-command with the -l flag like so:



gnome-screensaver-command -l


However this will only work if you have Gnome Screensaver running for your session (should be - unless you've disabled it) you can start that from the commandline with:



gnome-screensaver





share|improve this answer



















  • 9




    cinnamon-screensaver-command -l for cinnamon! ;))
    – user128334
    Jan 5 '14 at 17:28






  • 3




    Likewise, use mate-screensaver-command -l for MATE desktop.
    – alanaktion
    Jun 13 '16 at 19:41


















up vote
27
down vote













gnome-screensaver-command -l


If you're in a different (desktop) session (e.g. virtual console, switched to another login, SSH), specify the display explicitly (:0 is the default display):



DISPLAY=:0 gnome-screensaver-command -l


To unlock, use the -d (--deactivate) option.






share|improve this answer



















  • 1




    can you clarify me the DISPALY=:0 option?
    – Renjith G
    Oct 16 '10 at 19:29






  • 1




    Sorry for my English... DISPLAY is a variable and define in which display an X Window System program renders to (it can be another monitor). In TTY this variable is empty. By default, first display is ":0.0". So use DISPLAY=:x before your command, indicates in which display run the command. And use DISPLAY=:0 or DISPLAY=:0.0 indicates: run the program in the first display.
    – Snip
    Oct 16 '10 at 20:46










  • @RenjithG this is quite clear if you think about it, also please do not be rude to the author of the previous comment.
    – hexafraction
    May 2 '12 at 18:02










  • This works perfect, thx! Now I can add a keyboard shortcut to fix my broken lock screen command.
    – domih
    Feb 8 '16 at 12:57


















up vote
18
down vote













gnome-screensaver-command --lock will do it.






share|improve this answer




























    up vote
    4
    down vote













    Under KDE dm-tool lock will work (for me on Kubuntu 15.04).






    share|improve this answer




























      up vote
      2
      down vote













      In newer versions of GNOME 3, gnome-screensaver is no more.



      The generic solution seems to be



      xdg-screensaver lock


      You also can call dbus directly instead (source):



      dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock


      It also seems they have taken away the possibility to unlock the screen from the command line.






      share|improve this answer























      • yes. very good answer. one command to rule them all ! :-)
        – solsTiCe
        Nov 21 at 15:43




















      up vote
      1
      down vote













      Here is a long way of accomplishing it (adding a quicker way below):



      to make it even easier to lock, you can add an executable script to an executable path, call it "lock" and then the locking of your screen will be as easy as typing "lock" in cli



      Here's how to do it:



      mkdir ~/bin


      vim ~/bin/lock



      #!/bin/bash
      gnome-screensaver-command -l


      save and quit



      chmod +x ~/bin/lock


      don't forget to add ~/bin to your ~/.profile - note the dot at the beginning of the file name
      one word of caution about this, make sure you add it to the right file. Here is what the .profile says about it:



      # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
      # exists.


      therefore you want to first check if the above-mentioned files exist in your home directory, you should add it there, if they don't exist, then add the path to bin to the .profile



      vim ~/.profile # or one of the the other files if they exist


      append the following at the end:



      # set PATH so it includes user's private bin directories
      PATH="$HOME/bin:$HOME/.local/bin:$PATH"


      at this point you can launch the following shortcut from cli



      you@yourUbuntu:~$ lock


      [EDIT] Here is the quick and easy way to do it:
      add an alias to your ~/.bashrc file, which is executed every time a shell is opened, thus ensuring Alias persists:



      vim ~/.bashrc
      # set lockscreen
      alias lock="gnome-screensaver-command -l"


      the result is the same, but quicker






      share|improve this answer






























        up vote
        0
        down vote













        There are also many minimalistic utilities that really only lock, they don't do anything else. (This can be more secure as in general keeping software simple leads to less bugs.)



        For example:




        • slock


        • i3lock







        share|improve this answer





















          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f7776%2fhow-do-i-lock-the-desktop-screen-via-command-line%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          7 Answers
          7






          active

          oldest

          votes








          7 Answers
          7






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          78
          down vote



          accepted










          You can lock the computer by running gnome-screensaver-command with the -l flag like so:



          gnome-screensaver-command -l


          However this will only work if you have Gnome Screensaver running for your session (should be - unless you've disabled it) you can start that from the commandline with:



          gnome-screensaver





          share|improve this answer



















          • 9




            cinnamon-screensaver-command -l for cinnamon! ;))
            – user128334
            Jan 5 '14 at 17:28






          • 3




            Likewise, use mate-screensaver-command -l for MATE desktop.
            – alanaktion
            Jun 13 '16 at 19:41















          up vote
          78
          down vote



          accepted










          You can lock the computer by running gnome-screensaver-command with the -l flag like so:



          gnome-screensaver-command -l


          However this will only work if you have Gnome Screensaver running for your session (should be - unless you've disabled it) you can start that from the commandline with:



          gnome-screensaver





          share|improve this answer



















          • 9




            cinnamon-screensaver-command -l for cinnamon! ;))
            – user128334
            Jan 5 '14 at 17:28






          • 3




            Likewise, use mate-screensaver-command -l for MATE desktop.
            – alanaktion
            Jun 13 '16 at 19:41













          up vote
          78
          down vote



          accepted







          up vote
          78
          down vote



          accepted






          You can lock the computer by running gnome-screensaver-command with the -l flag like so:



          gnome-screensaver-command -l


          However this will only work if you have Gnome Screensaver running for your session (should be - unless you've disabled it) you can start that from the commandline with:



          gnome-screensaver





          share|improve this answer














          You can lock the computer by running gnome-screensaver-command with the -l flag like so:



          gnome-screensaver-command -l


          However this will only work if you have Gnome Screensaver running for your session (should be - unless you've disabled it) you can start that from the commandline with:



          gnome-screensaver






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 10 '14 at 22:22









          Seth

          33.5k26109159




          33.5k26109159










          answered Oct 16 '10 at 19:10









          Marco Ceppi

          36.7k24152190




          36.7k24152190








          • 9




            cinnamon-screensaver-command -l for cinnamon! ;))
            – user128334
            Jan 5 '14 at 17:28






          • 3




            Likewise, use mate-screensaver-command -l for MATE desktop.
            – alanaktion
            Jun 13 '16 at 19:41














          • 9




            cinnamon-screensaver-command -l for cinnamon! ;))
            – user128334
            Jan 5 '14 at 17:28






          • 3




            Likewise, use mate-screensaver-command -l for MATE desktop.
            – alanaktion
            Jun 13 '16 at 19:41








          9




          9




          cinnamon-screensaver-command -l for cinnamon! ;))
          – user128334
          Jan 5 '14 at 17:28




          cinnamon-screensaver-command -l for cinnamon! ;))
          – user128334
          Jan 5 '14 at 17:28




          3




          3




          Likewise, use mate-screensaver-command -l for MATE desktop.
          – alanaktion
          Jun 13 '16 at 19:41




          Likewise, use mate-screensaver-command -l for MATE desktop.
          – alanaktion
          Jun 13 '16 at 19:41












          up vote
          27
          down vote













          gnome-screensaver-command -l


          If you're in a different (desktop) session (e.g. virtual console, switched to another login, SSH), specify the display explicitly (:0 is the default display):



          DISPLAY=:0 gnome-screensaver-command -l


          To unlock, use the -d (--deactivate) option.






          share|improve this answer



















          • 1




            can you clarify me the DISPALY=:0 option?
            – Renjith G
            Oct 16 '10 at 19:29






          • 1




            Sorry for my English... DISPLAY is a variable and define in which display an X Window System program renders to (it can be another monitor). In TTY this variable is empty. By default, first display is ":0.0". So use DISPLAY=:x before your command, indicates in which display run the command. And use DISPLAY=:0 or DISPLAY=:0.0 indicates: run the program in the first display.
            – Snip
            Oct 16 '10 at 20:46










          • @RenjithG this is quite clear if you think about it, also please do not be rude to the author of the previous comment.
            – hexafraction
            May 2 '12 at 18:02










          • This works perfect, thx! Now I can add a keyboard shortcut to fix my broken lock screen command.
            – domih
            Feb 8 '16 at 12:57















          up vote
          27
          down vote













          gnome-screensaver-command -l


          If you're in a different (desktop) session (e.g. virtual console, switched to another login, SSH), specify the display explicitly (:0 is the default display):



          DISPLAY=:0 gnome-screensaver-command -l


          To unlock, use the -d (--deactivate) option.






          share|improve this answer



















          • 1




            can you clarify me the DISPALY=:0 option?
            – Renjith G
            Oct 16 '10 at 19:29






          • 1




            Sorry for my English... DISPLAY is a variable and define in which display an X Window System program renders to (it can be another monitor). In TTY this variable is empty. By default, first display is ":0.0". So use DISPLAY=:x before your command, indicates in which display run the command. And use DISPLAY=:0 or DISPLAY=:0.0 indicates: run the program in the first display.
            – Snip
            Oct 16 '10 at 20:46










          • @RenjithG this is quite clear if you think about it, also please do not be rude to the author of the previous comment.
            – hexafraction
            May 2 '12 at 18:02










          • This works perfect, thx! Now I can add a keyboard shortcut to fix my broken lock screen command.
            – domih
            Feb 8 '16 at 12:57













          up vote
          27
          down vote










          up vote
          27
          down vote









          gnome-screensaver-command -l


          If you're in a different (desktop) session (e.g. virtual console, switched to another login, SSH), specify the display explicitly (:0 is the default display):



          DISPLAY=:0 gnome-screensaver-command -l


          To unlock, use the -d (--deactivate) option.






          share|improve this answer














          gnome-screensaver-command -l


          If you're in a different (desktop) session (e.g. virtual console, switched to another login, SSH), specify the display explicitly (:0 is the default display):



          DISPLAY=:0 gnome-screensaver-command -l


          To unlock, use the -d (--deactivate) option.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 23 '11 at 8:50









          Lekensteyn

          119k47262354




          119k47262354










          answered Oct 16 '10 at 19:20









          Snip

          27124




          27124








          • 1




            can you clarify me the DISPALY=:0 option?
            – Renjith G
            Oct 16 '10 at 19:29






          • 1




            Sorry for my English... DISPLAY is a variable and define in which display an X Window System program renders to (it can be another monitor). In TTY this variable is empty. By default, first display is ":0.0". So use DISPLAY=:x before your command, indicates in which display run the command. And use DISPLAY=:0 or DISPLAY=:0.0 indicates: run the program in the first display.
            – Snip
            Oct 16 '10 at 20:46










          • @RenjithG this is quite clear if you think about it, also please do not be rude to the author of the previous comment.
            – hexafraction
            May 2 '12 at 18:02










          • This works perfect, thx! Now I can add a keyboard shortcut to fix my broken lock screen command.
            – domih
            Feb 8 '16 at 12:57














          • 1




            can you clarify me the DISPALY=:0 option?
            – Renjith G
            Oct 16 '10 at 19:29






          • 1




            Sorry for my English... DISPLAY is a variable and define in which display an X Window System program renders to (it can be another monitor). In TTY this variable is empty. By default, first display is ":0.0". So use DISPLAY=:x before your command, indicates in which display run the command. And use DISPLAY=:0 or DISPLAY=:0.0 indicates: run the program in the first display.
            – Snip
            Oct 16 '10 at 20:46










          • @RenjithG this is quite clear if you think about it, also please do not be rude to the author of the previous comment.
            – hexafraction
            May 2 '12 at 18:02










          • This works perfect, thx! Now I can add a keyboard shortcut to fix my broken lock screen command.
            – domih
            Feb 8 '16 at 12:57








          1




          1




          can you clarify me the DISPALY=:0 option?
          – Renjith G
          Oct 16 '10 at 19:29




          can you clarify me the DISPALY=:0 option?
          – Renjith G
          Oct 16 '10 at 19:29




          1




          1




          Sorry for my English... DISPLAY is a variable and define in which display an X Window System program renders to (it can be another monitor). In TTY this variable is empty. By default, first display is ":0.0". So use DISPLAY=:x before your command, indicates in which display run the command. And use DISPLAY=:0 or DISPLAY=:0.0 indicates: run the program in the first display.
          – Snip
          Oct 16 '10 at 20:46




          Sorry for my English... DISPLAY is a variable and define in which display an X Window System program renders to (it can be another monitor). In TTY this variable is empty. By default, first display is ":0.0". So use DISPLAY=:x before your command, indicates in which display run the command. And use DISPLAY=:0 or DISPLAY=:0.0 indicates: run the program in the first display.
          – Snip
          Oct 16 '10 at 20:46












          @RenjithG this is quite clear if you think about it, also please do not be rude to the author of the previous comment.
          – hexafraction
          May 2 '12 at 18:02




          @RenjithG this is quite clear if you think about it, also please do not be rude to the author of the previous comment.
          – hexafraction
          May 2 '12 at 18:02












          This works perfect, thx! Now I can add a keyboard shortcut to fix my broken lock screen command.
          – domih
          Feb 8 '16 at 12:57




          This works perfect, thx! Now I can add a keyboard shortcut to fix my broken lock screen command.
          – domih
          Feb 8 '16 at 12:57










          up vote
          18
          down vote













          gnome-screensaver-command --lock will do it.






          share|improve this answer

























            up vote
            18
            down vote













            gnome-screensaver-command --lock will do it.






            share|improve this answer























              up vote
              18
              down vote










              up vote
              18
              down vote









              gnome-screensaver-command --lock will do it.






              share|improve this answer












              gnome-screensaver-command --lock will do it.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Oct 16 '10 at 19:08









              mgunes

              8,21223541




              8,21223541






















                  up vote
                  4
                  down vote













                  Under KDE dm-tool lock will work (for me on Kubuntu 15.04).






                  share|improve this answer

























                    up vote
                    4
                    down vote













                    Under KDE dm-tool lock will work (for me on Kubuntu 15.04).






                    share|improve this answer























                      up vote
                      4
                      down vote










                      up vote
                      4
                      down vote









                      Under KDE dm-tool lock will work (for me on Kubuntu 15.04).






                      share|improve this answer












                      Under KDE dm-tool lock will work (for me on Kubuntu 15.04).







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Sep 7 '15 at 16:11









                      pbhj

                      1,255923




                      1,255923






















                          up vote
                          2
                          down vote













                          In newer versions of GNOME 3, gnome-screensaver is no more.



                          The generic solution seems to be



                          xdg-screensaver lock


                          You also can call dbus directly instead (source):



                          dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock


                          It also seems they have taken away the possibility to unlock the screen from the command line.






                          share|improve this answer























                          • yes. very good answer. one command to rule them all ! :-)
                            – solsTiCe
                            Nov 21 at 15:43

















                          up vote
                          2
                          down vote













                          In newer versions of GNOME 3, gnome-screensaver is no more.



                          The generic solution seems to be



                          xdg-screensaver lock


                          You also can call dbus directly instead (source):



                          dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock


                          It also seems they have taken away the possibility to unlock the screen from the command line.






                          share|improve this answer























                          • yes. very good answer. one command to rule them all ! :-)
                            – solsTiCe
                            Nov 21 at 15:43















                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          In newer versions of GNOME 3, gnome-screensaver is no more.



                          The generic solution seems to be



                          xdg-screensaver lock


                          You also can call dbus directly instead (source):



                          dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock


                          It also seems they have taken away the possibility to unlock the screen from the command line.






                          share|improve this answer














                          In newer versions of GNOME 3, gnome-screensaver is no more.



                          The generic solution seems to be



                          xdg-screensaver lock


                          You also can call dbus directly instead (source):



                          dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock


                          It also seems they have taken away the possibility to unlock the screen from the command line.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 21 at 15:40

























                          answered Nov 21 at 15:34









                          Marian

                          1212




                          1212












                          • yes. very good answer. one command to rule them all ! :-)
                            – solsTiCe
                            Nov 21 at 15:43




















                          • yes. very good answer. one command to rule them all ! :-)
                            – solsTiCe
                            Nov 21 at 15:43


















                          yes. very good answer. one command to rule them all ! :-)
                          – solsTiCe
                          Nov 21 at 15:43






                          yes. very good answer. one command to rule them all ! :-)
                          – solsTiCe
                          Nov 21 at 15:43












                          up vote
                          1
                          down vote













                          Here is a long way of accomplishing it (adding a quicker way below):



                          to make it even easier to lock, you can add an executable script to an executable path, call it "lock" and then the locking of your screen will be as easy as typing "lock" in cli



                          Here's how to do it:



                          mkdir ~/bin


                          vim ~/bin/lock



                          #!/bin/bash
                          gnome-screensaver-command -l


                          save and quit



                          chmod +x ~/bin/lock


                          don't forget to add ~/bin to your ~/.profile - note the dot at the beginning of the file name
                          one word of caution about this, make sure you add it to the right file. Here is what the .profile says about it:



                          # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
                          # exists.


                          therefore you want to first check if the above-mentioned files exist in your home directory, you should add it there, if they don't exist, then add the path to bin to the .profile



                          vim ~/.profile # or one of the the other files if they exist


                          append the following at the end:



                          # set PATH so it includes user's private bin directories
                          PATH="$HOME/bin:$HOME/.local/bin:$PATH"


                          at this point you can launch the following shortcut from cli



                          you@yourUbuntu:~$ lock


                          [EDIT] Here is the quick and easy way to do it:
                          add an alias to your ~/.bashrc file, which is executed every time a shell is opened, thus ensuring Alias persists:



                          vim ~/.bashrc
                          # set lockscreen
                          alias lock="gnome-screensaver-command -l"


                          the result is the same, but quicker






                          share|improve this answer



























                            up vote
                            1
                            down vote













                            Here is a long way of accomplishing it (adding a quicker way below):



                            to make it even easier to lock, you can add an executable script to an executable path, call it "lock" and then the locking of your screen will be as easy as typing "lock" in cli



                            Here's how to do it:



                            mkdir ~/bin


                            vim ~/bin/lock



                            #!/bin/bash
                            gnome-screensaver-command -l


                            save and quit



                            chmod +x ~/bin/lock


                            don't forget to add ~/bin to your ~/.profile - note the dot at the beginning of the file name
                            one word of caution about this, make sure you add it to the right file. Here is what the .profile says about it:



                            # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
                            # exists.


                            therefore you want to first check if the above-mentioned files exist in your home directory, you should add it there, if they don't exist, then add the path to bin to the .profile



                            vim ~/.profile # or one of the the other files if they exist


                            append the following at the end:



                            # set PATH so it includes user's private bin directories
                            PATH="$HOME/bin:$HOME/.local/bin:$PATH"


                            at this point you can launch the following shortcut from cli



                            you@yourUbuntu:~$ lock


                            [EDIT] Here is the quick and easy way to do it:
                            add an alias to your ~/.bashrc file, which is executed every time a shell is opened, thus ensuring Alias persists:



                            vim ~/.bashrc
                            # set lockscreen
                            alias lock="gnome-screensaver-command -l"


                            the result is the same, but quicker






                            share|improve this answer

























                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              Here is a long way of accomplishing it (adding a quicker way below):



                              to make it even easier to lock, you can add an executable script to an executable path, call it "lock" and then the locking of your screen will be as easy as typing "lock" in cli



                              Here's how to do it:



                              mkdir ~/bin


                              vim ~/bin/lock



                              #!/bin/bash
                              gnome-screensaver-command -l


                              save and quit



                              chmod +x ~/bin/lock


                              don't forget to add ~/bin to your ~/.profile - note the dot at the beginning of the file name
                              one word of caution about this, make sure you add it to the right file. Here is what the .profile says about it:



                              # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
                              # exists.


                              therefore you want to first check if the above-mentioned files exist in your home directory, you should add it there, if they don't exist, then add the path to bin to the .profile



                              vim ~/.profile # or one of the the other files if they exist


                              append the following at the end:



                              # set PATH so it includes user's private bin directories
                              PATH="$HOME/bin:$HOME/.local/bin:$PATH"


                              at this point you can launch the following shortcut from cli



                              you@yourUbuntu:~$ lock


                              [EDIT] Here is the quick and easy way to do it:
                              add an alias to your ~/.bashrc file, which is executed every time a shell is opened, thus ensuring Alias persists:



                              vim ~/.bashrc
                              # set lockscreen
                              alias lock="gnome-screensaver-command -l"


                              the result is the same, but quicker






                              share|improve this answer














                              Here is a long way of accomplishing it (adding a quicker way below):



                              to make it even easier to lock, you can add an executable script to an executable path, call it "lock" and then the locking of your screen will be as easy as typing "lock" in cli



                              Here's how to do it:



                              mkdir ~/bin


                              vim ~/bin/lock



                              #!/bin/bash
                              gnome-screensaver-command -l


                              save and quit



                              chmod +x ~/bin/lock


                              don't forget to add ~/bin to your ~/.profile - note the dot at the beginning of the file name
                              one word of caution about this, make sure you add it to the right file. Here is what the .profile says about it:



                              # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
                              # exists.


                              therefore you want to first check if the above-mentioned files exist in your home directory, you should add it there, if they don't exist, then add the path to bin to the .profile



                              vim ~/.profile # or one of the the other files if they exist


                              append the following at the end:



                              # set PATH so it includes user's private bin directories
                              PATH="$HOME/bin:$HOME/.local/bin:$PATH"


                              at this point you can launch the following shortcut from cli



                              you@yourUbuntu:~$ lock


                              [EDIT] Here is the quick and easy way to do it:
                              add an alias to your ~/.bashrc file, which is executed every time a shell is opened, thus ensuring Alias persists:



                              vim ~/.bashrc
                              # set lockscreen
                              alias lock="gnome-screensaver-command -l"


                              the result is the same, but quicker







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jun 14 at 17:14

























                              answered Jun 13 at 15:25









                              George Mogilevsky

                              134




                              134






















                                  up vote
                                  0
                                  down vote













                                  There are also many minimalistic utilities that really only lock, they don't do anything else. (This can be more secure as in general keeping software simple leads to less bugs.)



                                  For example:




                                  • slock


                                  • i3lock







                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote













                                    There are also many minimalistic utilities that really only lock, they don't do anything else. (This can be more secure as in general keeping software simple leads to less bugs.)



                                    For example:




                                    • slock


                                    • i3lock







                                    share|improve this answer























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      There are also many minimalistic utilities that really only lock, they don't do anything else. (This can be more secure as in general keeping software simple leads to less bugs.)



                                      For example:




                                      • slock


                                      • i3lock







                                      share|improve this answer












                                      There are also many minimalistic utilities that really only lock, they don't do anything else. (This can be more secure as in general keeping software simple leads to less bugs.)



                                      For example:




                                      • slock


                                      • i3lock








                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 6 '16 at 3:38









                                      Alois Mahdal

                                      417512




                                      417512






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Ask Ubuntu!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f7776%2fhow-do-i-lock-the-desktop-screen-via-command-line%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          How to send String Array data to Server using php in android

                                          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                                          Is anime1.com a legal site for watching anime?